开发者

Understanding how to use COM in Python

开发者 https://www.devze.com 2023-02-13 02:40 出处:网络
I\'m attempting to implement the equivalent of a VB program in python using COM. Here are the relevant lines from the VB program:

I'm attempting to implement the equivalent of a VB program in python using COM. Here are the relevant lines from the VB program:

eConCall = New Microsoft.Dynamics.GP.eConnect.eConnectMethods
eConCall.eConnect_EntryPoint(sConnectionString, EnumTypes.ConnectionStringType.SqlClient, myXmlDocument, EnumTypes.SchemaValidationType.None)

In Python, I'm doing:

import win32com.client
eConCall = win32com.client.Dispatch("Microsoft.Dynamics.GP.eConnect.eConnectMethods")

but eConCall doesn't have a eConnect_EntryPoint method. In fact it doesn't seem to have any methods:

eConCall = win32com.client.gencache.EnsureDispatch("Microsoft.Dynamics.GP.eConnect.eConnectMethods")
dir(eConCall)

Prints:

['CLSID', '_ApplyTypes_', '__doc__', '__eq__', '__getattr__', 
'__init__', '__module__', '__ne__', '__repr__', '__setattr__', 
'_get_good_object_', '_get_good_single_object_', '_oleobj_', 
'_prop_map_get_', '_prop_map_put_', 'coclass_clsid']

I'm pretty sure I'm misunderstanding how Dispatch should be used and how I should be getting access to eConnectMethods in Python. Can a kind soul help me out? How would I get a hold of an instance of eConnectMethods such that I can call eConnect_EntryPoint on i开发者_开发问答t?


I think, you don't see any methods as you've got the so called late-binding, which means that whatever you try to invoke is checked at runtime via standard COM IDispatch interface and while you work with valid methods/properties - it just works. Or you get runtime error otherwise, quite like in VBScript.

Seems that your task is not that easy for late-binding trial&error, maybe you'd want to check an option of early-binding - check this link - http://oreilly.com/catalog/pythonwin32/chapter/ch12.html.

I would also suggest you to try some simpler COM examples, that will help you feel comfortable with the usage of IDispatch in Python, i.e. by checking more simple Excel or Word automation samples available online.

Update Ok, I missed that you already tried to create early-bound wrapper. Take a look at result of the makepy.py (somewhere in \Lib\site-packages\win32com\gen_py\.py) and compare it with IDL of the COM server. Maybe the method is still available through late-binding, though it didn't make it for some reason into the wrapper..


You should consider comtypes which allow low level access of COM

0

精彩评论

暂无评论...
验证码 换一张
取 消