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:

  1. dllcanunloadnow
  2. dllgetclassobject
  3. dllregisterserver
  4. 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:

  1. coinitializeex()
  2. 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

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -