comtypes
comtypes copied to clipboard
CreateObject fails and then suceeds
I am working with a com library provided by a third party. If I run in the python interpreter
>>> fac_client = CreateObject("CxFac.FacClient")
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-2-945b33a92d0f> in <module>
----> 1 fac_client = CreateObject("CxFac.FacClient")
~\anaconda3\envs\pycygnet\lib\site-packages\comtypes\client\__init__.py in CreateObject(progid, clsctx, machine, interface, dynamic, pServerInfo)
236 logger.debug("CoCreateInstance(%s, clsctx=%s, interface=%s)",
237 clsid, clsctx, interface)
--> 238 obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
239 else:
240 logger.debug("CoCreateInstanceEx(%s, clsctx=%s, interface=%s, machine=%s,\
~\anaconda3\envs\pycygnet\lib\site-packages\comtypes\__init__.py in CoCreateInstance(clsid, interface, clsctx, punkouter)
1223 p = POINTER(interface)()
1224 iid = interface._iid_
-> 1225 _ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
1226 return p
1227
OSError: exception: access violation reading 0x0000000000000008
But if I run that same command a second time
fac_client = CreateObject("CxFac.FacClient")
Then there is no error and the Object is created correctly.
This happens in my program and I have had to work around this by
try:
CreateObject("CxFac.FacClient")
except OSError:
CreateObject("CxFac.FacClient")
fac_client = CreateObject("CxFac.FacClient")
Do you have any suggestions on how to determine what is causing the access violation and how to stop it?
I have also tried using GetModule
on the dll, but that doesn't allow me to return the object
GetModule("CxFac64.dll")
from comtypes.gen import CxFacLib
fac_client = CxFacLib.IFacClient()
fac_client.UserId()
TypeError: Expected a COM this pointer as first argument
@RoyLarson
Is this still a problem?
Subclasses of IUnknown
and IDispatch
defined in comtypes.gen
cannot be instantiated like normal Python objects.
Perhaps this is how it should be this.
GetModule("CxFac64.dll")
import comtypes
from comtypes.gen import CxFacLib
fac_client = comtypes.CoCreateInstance(CxFacLib.FacClient().IPersist_GetClassID(), interface=CxFacLib.IFacClient)
A good example is the part where instantiating UIAutomationClient.IUIAutomation
in pywinauto
.
https://github.com/pywinauto/pywinauto/blob/ba727ce4229e23293a9bc5912276cc556f64ad0f/pywinauto/windows/uia_defines.py#L50-L54
Anyway this looks like a bug to me. I've tried to register for downloading CygNet software, but it appears to request registration twice, so the second registration requests too much data including cell phone. If it's actual and possible to provide the software, please contact me by email: my_github_nick.replace("-", ".")
at gmail (dot) com. I'm OK to sign NDA if it's necessary.
I tried to recreate the error and it doesn't happen. I don't know if it was a windows update that allowed this to work or something else. I tried to recreate the environment that the error happened in but because CygNet has been updated several times I am not able to go back that far. I will reopen this if any of my coworkers have issues after I update the code to remove the guards.
Looking through that code again I left myself a note and apparently I had figured out what caused the issue. The problem was that there is a 32 bit registered version and a 64 bit registered version of the libraries. When using CreateObject COMTypes would try to retrieve the 32 bit version of the library first and fail because I am running 64 bit python. On the second try, it finds the 64 bit version and is therefore able to load the library.
I don't know if this is something that you would consider a bug still. I think I figured this out by installing 32 bit python and having it work immediately.
@RoyLarson thanks a lot for the info! I think we can re-open the issue as kind of enhancement at least. The suggested guards in comtypes' code may help others to figure it out much faster.
This is similar to #89 in that finding a library with the appropriate bit is not always possible.
I don't think that one of them is duplicated
, thus rather that they are separate issues that should be considered separately.