我有代码三块,我要和目前的工作:通过注册TLB访问蟒蛇未注册的COM对象
- 封闭源代码的应用程序(MAIN.EXE)
- 封闭源代码我在Python正在开发的DLL(comobj.dll)实现VB COM对象
- 代码
comobj.dll主机的COM对象(可以说,“MainInteract”),我想从使用蟒蛇。我已经可以在IronPython中完美地使用这个对象,但由于其他需求,我需要从常规的Python中使用它。我相信这里最好的方法是使用win32com,但我根本无法取得任何进展。
首先,一些工作IronPython的代码:
import clr
import os
import sys
__dir__ = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, __dir__)
sys.path.append(r"C:\Path\To\comobj.dll") #This is where the com object dll actually is
clr.AddReferenceToFileAndPath(os.path.join(__dir__, r'comobj_1_1.dll')) #This is the .NET interop assembly that was created automatically via SharpDevelop's COM Inspector
from comobj_1_1 import clsMainInteract
o = clsMainInteract()
o.DoStuff(True)
现在,我试图在常规的Python代码:
>>> import win32com.client
>>> win32com.client.Dispatch("{11111111-comobj_guid_i_got_from_com_inspector}")
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 104, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221164, 'Class not registered', None, None)
我已经使用了TLB的友好名称也试图:
>>> import win32com.client
>>> win32com.client.Dispatch("Friendly TLB Name I Saw")
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 104, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File "C:\Python26\lib\site-packages\win32com\client\dynamic.py", line 84, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.IID_IDispatch)
pywintypes.com_error: (-2147221005, 'Invalid class string', None, None)
其实,我得到的唯一成功是这样的:
import pythoncom
tlb = pythoncom.LoadRegTypeLib("{11111111-comobj_guid_i_got_from_com_inspector}",1,1,0)
>>> tlb
<PyITypeLib at 0x00AD7D78 with obj at 0x0025EDF0>
>>> tlb.GetDocumentation(1)
(u'clsMainInteract', None, 0, None)
但我不知道如何从那里去获得一个对象。我认为我的问题是我需要将DLL加载到我的进程中,并让它将自己注册到进程的COM源,所以我可以正确地使用CoCreateInstance/win32com.client.Dispatch()。
我也看到Activation Contexts被引用,特别是当谈到'没有注册COM'时,但通常在像“如果你在你的.manifest文件中指定正确的东西,Windows将为你创建一个上下文”的句子中。如果可能的话,我想避免使用清单文件,因为在(闭源代码)COM对象dll所在的文件夹中需要一个文件夹,并且如果我可以避免它,我宁愿不删除任何文件。
感谢您的帮助。
我不知道答案了我的头顶,但如果你能用C++来做,那么你可以很简单地包装它。 – ConcernedOfTunbridgeWells 2010-06-24 17:46:12
好的,我想这是一个选择,但我希望避免它。 – 2010-06-24 18:27:41