I have a plugin that I'm trying to create as a sample for an application from the company I work for. I'm trying to write this plugin in Python.
The way the plugin architecture works is that the plugin needs to implement an interface defined in a provided COM type library. So it is a COM client to that type library and in the end gets registered as a COM Server to the registry and the application by giving it the ClassID for late-bound COM by the application.
I'm using pythoncom and win32com and have used makepy.py to generate 开发者_JAVA百科the needed python code from the type libarary but I can't seem to find a way to create a class that implements the interface from this type library.
Any pointers on this would be greatly appreciated.
Thanks
When I try to run Dispatch to get the COM object I get the following exeption:
>>> interface = win32com.client.Dispatch('{68AC7909-804F-4D6D-861C-8382DAA7B029}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\win32com\client\__init__.py", line 95, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,clsctx)
File "C:\Python26\Lib\site-packages\win32com\client\dynamic.py", line 108, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python26\Lib\site-packages\win32com\client\dynamic.py", line 85, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221164, 'Class not registered', None, None)
You can simply use win32com.client.Dispatch()
:
object = win32com.client.Dispatch("Class.Name")
This is the example from ActiveState quick start guide:
>>> import win32com.client
>>> w=win32com.client.Dispatch("Word.Application")
>>> w.Visible=1
>>> w
<win32com.gen_py.Microsoft Word 8.0 Object Library._Application>
If it doesn't work, you can use win32com.client.gencache.EnsureModule()
to make sure you've
generated the right cache module.
from win32com.client import Dispatch
from win32com.client import gencache
# This comes from running: makepy.py -i "Microsoft Excel 14.0 Object Library"
gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 7)
obj = Dispatch("Excel.Application.14")
# gives <win32com.gen_py.Microsoft Excel 14.0 Object Library._Application instance ...>
print repr(obj)
The same thing with comtypes (simpler and supports custom interfaces)
from comtypes.client import CreateObject
obj = CreateObject("Excel.Application")
精彩评论