1

I have a COM library (SMISEventHandler) with which I would like to interface using win32com, which exports 2 classes (ImgEvents and SMISEvents). When I create an instance of one of the classes using EnsureDispatch("SMISEventHandler.ImgEvents") I get an object with the correct UUIDs and interface etc., the typelib is correctly passed to give the gencache interface files, and everything seems ok, but when I try to call some of the methods of the interface I get errors like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Public\Miniconda3\envs\hq_clin1\lib\site-packages\win32com\gen_py\2F06A879-D4E9-482E-AC01-E2B14A961C45x0x3x0\IDualImgEventTrigger.py", line 46, in ImageCreated
    return self._oleobj_.InvokeTypes(1610809349, LCID, 1, (24, 0), ((8, 1), (8, 1), (8, 1), (3, 1), (8, 1), (17, 1), (8, 1)),PatientId
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

or in some cases:

>>> ehw2.Status(1, "success")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Public\Miniconda3\envs\hq_clin1\lib\site-packages\win32com\gen_py\2F06A879-D4E9-482E-AC01-E2B14A961C45x0x3x0\IDualImgEventTrigger.py", line 67, in Status
    return self._ApplyTypes_(1610809344, 1, (24, 32), ((3, 1), (8, 1), (8, 49)), 'Status', None,Status
  File "C:\Users\Public\Miniconda3\envs\hq_clin1\lib\site-packages\win32com\client\__init__.py", line 572, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352562, 'Invalid number of parameters.', None, None)

Interestingly, it seems that the methods where we see "Invalid number of parameters" correspond to the methods which are defined on the second class (SMISEvents), but which have a different signature. Instances of that second class work correctly with no issues.

If I run the equivalent code in comtypes then the method calls succeed as expected

>>>obj = comtypes.client.CreateObject("SMISEventHandler.ImgEvents")
>>>obj.Status(1, "success")
0

The library also works correctly when accessed from VB6. This suggests that the COM library itself is working correctly and the fault lies with win32com. This also seems related to the following question from StackOverflow that was never resolved (other than to use comtypes): "Member not found." error using win32com

comtypes is not a great solution for me in practice because I need the cross-thread marshalling abilities of win32com, so I am very keen to find a solution to this problem, but running out of ideas to try - if anybody can suggest some ways to get a better idea of what is going on, I will certainly try them.

Python: 3.9.12 pywin32: 303

  • I am not so familiar with comtypes, but a common problem is that an interface created using EnsureDispatch() is case-sensitive. VBA, for example, just uses the IDispatch interface and looks up properties/methods using `IDispatch::GetIdsOfNames()`, and that look up is case-INsenstive. So a function like `myobj.rUn()` might work in VBA, even though in the type library the function is `Run()`. It can also be worth deleting any files in the gen_py directory and re-creating the wrappers. – DS_London Jun 14 '22 at 17:57

0 Answers0