问题
I am attempting to write some scripts to employ a COM type library that came with an application to control it externally.
However, I am stuck in my use of the Python libraries for COM (pythoncom and win32com):
Using a COM Browser, I am able to navigate to find the GUID for the Type Library I am interested in using.
Using pythoncom, I am able to easily navigate this type library to see what objects are available available.
>>> import win32com.client
>>> import pythoncom
>>> type_lib_iid = '{12345678-ABCD-EFGH-1234-12341234}'
>>> lib = pythoncom.LoadTypeLib(type_lib_iid, 1, 0)
>>> for index in range(0, lib.GetTypeInfoCount()):
print lib.GetDocumentation(index)[0]
print lib.GetTypeInfo(index).GetTypeAttr().iid
This returns all the GUID's of the available COM objects (sorry if I am butchering this vocabulary).
Strangely, while all the objects that are returned by this call appear in the COM browser when I browse the type library, only 4 of the 81 that are available can I call Dispatch on. The rest return:
'Class not registered.'
My question is in two parts:
1) How is it possible that objects can be found both through a COM browser and through the above call and not be callable using win32com.client.Dispatch? [Note: the application in question IS running.]
2) Am I using the wrong function? Is Dispatch incorrect? I freely admit that I have found the documentation for the Python COM modules confusing, but I have not found any examples where existing COM objects were not Dispatched upon, so I do not know where to go from here. Furthermore, the 4 objects that CAN be dispatched upon seem to work exactly as explained in the documentation.
[One last note: if I attempt pythoncom.CoCreateInstance(TypeLibCLSID, None, 1, GUIDofInterfaceObject) I get 'Interface not supported." The critical interface I need to use is the GUID I am trying here.]
回答1:
Only CoClass interfaces can be created directly using Dispatch. The other interfaces are usually created by drilling down through the properties and methods of the top-level objects. If you run makepy on your typelib, the generated wrapper file will have the CoClass's marked with a comment.
来源:https://stackoverflow.com/questions/10286071/only-partial-access-to-a-com-type-library-through-python