Antonino Bonanno
Antonino Bonanno

Reputation: 1

Python pass string ref into method of COM (ActiveXCtrl)

I am trying to use a method present in an ocx file.

The definition of this method is (resolution from VS - c#) VS Screen

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

My Python code

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)

access to self.gd.ctrl.Status is fine.

I can't pass the right type of parameters to the self.gd.ctrl.EcrCmd method

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', '')))

Upvotes: 0

Views: 42

Answers (1)

Antonino Bonanno
Antonino Bonanno

Reputation: 1

Solved

import comtypes
from _ctypes import pointer
...

error = pointer(comtypes.BSTR())
cmd = self.gd.ctrl.EcrCmd("nofis apri", error)

Upvotes: 0

Related Questions