Using python as a COM client
mhurwitch at roadnet.com
mhurwitch at roadnet.com
Wed Sep 22 17:33:19 EDT 1999
I spent some time today learning how to use python as a COM client.
It's pretty straight-forward, but I did have a little bit of a learning
curve when I tried to use secondary dispinterfaces. I am posting here
so that an example is available on Deja News.
I have an ATL object that implements a primary dispinterface as
IMsgBoxObj, and a secondary dispinterface as IMsgBoxObj2. It answers a
query for IDispatch by returning IMsgBoxObj. Here's the IDL :
[
object,
uuid(1F17E70E-676E-11D3-AC59-00500462ECB1),
dual,
helpstring("IMsgBoxObj Interface"),
pointer_default(unique)
]
interface IMsgBoxObj : IDispatch
{
[id(1), helpstring("method ShowMsgBox")] HRESULT ShowMsgBox();
};
[
object,
uuid(F2570840-70C3-11d3-AC59-00500462ECB1),
dual,
helpstring("IMsgBoxObj2 Interface"),
pointer_default(unique)
]
interface IMsgBoxObj2 : IDispatch
{
[id(2), helpstring("method ShowMsgBox2")] HRESULT ShowMsgBox2();
};
[
uuid(1F17E700-676E-11D3-AC59-00500462ECB1),
version(1.0),
helpstring("MsgBoxObject 1.0 Type Library")
]
library MSGBOXOBJECTLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(1F17E70F-676E-11D3-AC59-00500462ECB1),
helpstring("MsgBoxObj Class")
]
coclass MsgBoxObj
{
[default] interface IMsgBoxObj;
interface IMsgBoxObj2;
};
};
The following python script can be used to exercise the object :
#--------------------------------------------------------------------
# multiple dispinterface client
import pythoncom
import win32com.client
# create a MsgBoxObj, ask it for the object's default dispinterface
# (by requesting IDispatch), and wrap the interface
# reference in an object that knows how to a dispinterface
IMsgBoxObj = win32com.client.Dispatch("MsgBoxObject.MsgBoxObj")
IMsgBoxObj.ShowMsgBox()
# now ask for a different dispinterface on the same object
IID_IMsgBoxObj2 = pythoncom.MakeIID("{F2570840-70C3-11d3-AC59-
00500462ECB1}")
dispinterface2 = IMsgBoxObj._oleobj_.QueryInterface(IID_IMsgBoxObj2,
pythoncom.IID_IDispatch)
IMsgBoxObj2 = win32com.client.Dispatch(dispinterface2,
resultCLSID=IID_IMsgBoxObj2)
IMsgBoxObj2.ShowMsgBox2()
#--------------------------------------------------------------------
Hope this is of some use...
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
More information about the Python-list
mailing list