ctypes - How to correctly load a Windows COM DLL in Python -
i trying load windows com dll in python exposed interfaces.
using dependency walker tool can list functions in dll. see 4 functions:
- dllcanunloadnow
- dllgetclassobject
- dllregisterserver
- dllunregisterserver
if understood correctly, need object of class using dllgetclassobject() , use exposed interfaces.
i using pythoncom , win32com.client extract object (got recipe stackoverflow post)
import pythoncom import win32com.client def createinstancefromdll(dll, clsid_class, iid_interface=pythoncom.iid_idispatch, punkouter=none, dwclscontext=pythoncom.clsctx_server): uuid import uuid ctypes import oledll, c_long, byref e = oledll(dll) print (e) #print (e.dllregisterserver()) clsid_class = uuid(clsid_class).bytes_le iclassfactory = uuid(str(pythoncom.iid_iclassfactory)).bytes_le com_classfactory = c_long(0) hr = e.dllgetclassobject(clsid_class, iclassfactory, byref(com_classfactory)) myfactory = pythoncom.objectfromaddress(com_classfactory.value, pythoncom.iid_iclassfactory) = myfactory.createinstance(punkouter, iid_interface) d = win32com.client.__wrapdispatch(i) return d print (createinstancefromdll('ptscontrol.dll', '{32a917e0-b1d4-4692-b0d7-793d81d9c8b5}'))
i got cls_id
windows registry ptscontrol tool. throws windowserror.
<oledll 'ptscontrol.dll', handle 70010000 @ 2451470> traceback (most recent call last): file "c:\wp\automation.py", line 35, n <module> print (createinstancefromdll('ptscontrol.dll', '{32a917e0-b1d4-4692-b0d7-793 d81d9c8b5}')) file "c:\wp\automation.py", line 29, n createinstancefromdll hr = e.dllgetclassobject(clsid_class, iclassfactory, byref(com_classfactory) ) file "_ctypes/callproc.c", line 945, in getresult windowserror: [error -2147467262] no such interface supported
any ideas doing wrong? not have access tool's source code.
in c++ 2-step process no idea how in python:
- coinitializeex()
- cocreateinstance()
in general, best technique access windows com dll in python? !!
i couldn't figure out how call dllgetclassobject directly, following approach worked me. , should too, seeing dll has dllregisterserver , dllunregisterserver:
cd c:\path\to\libs regsvr32 yourlib.dll
and in python:
import win32com.client lib = win32com.client.dispatch("some.thing") lib.somefunction()
i not sure should specify parameter dispatch. dll came sample vb app did this:
dim lib new some.thing lib.somefunction()
Comments
Post a Comment