调用C++函数,使用一些变量指针作为参数,用Python返回一个值

2024-04-25 09:02:45 发布

您现在位置:Python中文网/ 问答频道 /正文

<>我有一个COM函数,C++中使用了一些变量指针作为参数来返回一个值。 我想用Python调用该函数,但我没有解决方案

我试图使用pywin32和pythonnet python库中的变量(pythoncom.VT_VARIANT | pythoncom.VT_BYREF,value),但没有成功

以下是COM函数的语法:

object.ExtendedGetSourceName(int iSource, VARIANT* ovSourceName) as Boolean

与:

object: a COM object
iSource: index of the source (starting from 0)
ovSourceName: name of the source

ovSourceName获取返回值

import win32com.client
import pythoncom

xmpmapPath = r"C:\LM.Irradiance.1.xmp"
XMPViewer = win32com.client.Dispatch("XMPViewer.Application") # START INSTANCE
XMPViewer.OpenFile(xmpmapPath)# Opens XMP file
iNbSource = XMPViewer.ExtendedGetNbSource #Returns the number of sources
for sourceindex in range(0, iNbSource):
    sSourceName = win32com.client.VARIANT(pythoncom.VT_VARIANT | pythoncom.VT_BYREF, 1)

    RetVal = XMPViewer.ExtendedGetSourceName(sourceindex,  sSourceName) #Returns TRUE if the call has succeeded, FALSE otherwise.
    print(str(RetVal))
    print(str(sSourceName))

Visual Studio代码返回

True
win32com.client.VARIANT(16396, 1)

您知道如何生成COM变量指针,然后用Python返回它吗