首页 > 解决方案 > 如何将安全数组从 python 传递到 COM?

问题描述

我需要将布尔值列表作为 VT_ARRAY 传递给 COM。

from win32com.client import Dispatch, VARIANT
import pythoncom

api = Dispatch("MyAPI")
controller = api.CreateInterface()
first = VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_BOOL, [True, False, True])
second = VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_BOOL, [True, False, True])
controller.MyMethod(first, second) 

COM接口的C++代码为:

if ( first->vt == ( VT_ARRAY | VT_BOOL ) && second->vt == ( VT_ARRAY | VT_BOOL ) )
    {
        CComSafeArray<BOOL> first_safe( first->parray ); //fails here
        CComSafeArray<BOOL> second_safe( second->parray );

在 python 端出现 COM 异常失败:

pywintypes.com_error: (-2147023170, 'The remote procedure call failed.', None, None)

标签: pythonpython-2.7win32compythoncom

解决方案


推荐阅读