Python 将字符串引用传递到 COM (ActiveXCtrl) 的方法中

问题描述 投票:0回答:1

我正在尝试使用 ocx 文件中存在的方法。

该方法的定义为(解析自VS - c#) VS屏幕

[DispId(17)]
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
int EcrCmd([MarshalAs(UnmanagedType.BStr)] string Command, [MarshalAs(UnmanagedType.BStr)] ref string pResult);

我的Python代码

self.frame = wx.Frame(parent=None, id=wx.ID_ANY,size=(500,500), title='Python Interface to COECRCOM')
self.gd = wx.lib.activex.ActiveXCtrl(self.frame, "COECRCOM.CoEcrComCtrl.1")
status = self.gd.ctrl.Status
print(status)

error = ""
cmd = self.gd.ctrl.EcrCmd("nofis apri", error)
print(cmd)
print(error)

访问 self.gd.ctrl.Status 没问题。

我无法将正确类型的参数传递给 self.gd.ctrl.EcrCmd 方法

Traceback (most recent call last):
  File "...lib\site-packages\comtypes\automation.py", line 855, in Invoke
    byref(result), byref(excepinfo), byref(argerr))
_ctypes.COMError: (-2147352571, 'Type incompatibility.', (None, None, None, 0, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File main.py", line 31, in <module>
    app = MainApp()
  File "main.py", line 20, in __init__
    cmd = self.gd.ctrl.EcrCmd("nofis apri", error)
  File "...lib\site-packages\comtypes\_memberspec.py", line 448, in func
    return obj.Invoke(memid, _invkind=1, *args, **kw) # DISPATCH_METHOD
  File "...lib\site-packages\comtypes\automation.py", line 878, in Invoke
    args))
_ctypes.COMError: (-2147352571, 'Type incompatibility.', ('TypeError: Parameter 1', ('nofis apri', '')))
  • 环境:32位(win-32)
  • Python:3.7.13
  • comtypes:v1.2.1
  • wxpython:4.0.4
python-3.x wxpython activex ocx comtypes
1个回答
0
投票

解决了

import comtypes
from _ctypes import pointer
...

error = pointer(comtypes.BSTR())
cmd = self.gd.ctrl.EcrCmd("nofis apri", error)
© www.soinside.com 2019 - 2024. All rights reserved.