[python-win32] C/C++ COM client calling Python COM server
Tim Roberts
timr at probo.com
Mon Mar 21 19:18:37 EDT 2016
Khangura,Gindi wrote:
> I have a COM server implemented in Python 2.7 (using pythoncom) that I
> am able to call using a Python or VBScript client. I would like to
> call the COM server from C/C++ code as well, but cannot find any
> information regarding this.
> My intention is to have a Python-only COM server that can be accessed
> by a Java application, VBScript script, and C/C++ DLL; this question
> is just focusing on the C/C++ part of it.
Because C++ is a lower-level language, accessing an object is much
wordier. In this particular instance, what you have is a late-binding
"dispatch" interface, meaning that you can't just define a C++ class and
fetch a pointer. You have to access everything indirectly. It's a pain
in the ass.
Here's a web page to get you started.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms221694.aspx
The first sample shows how to start from a ProgID or CLSID (which you
have) and get an IDispatch interface pointer. The second example shows
a general-purpose "Invoke" function that can call a method on an
IDispatch interface, given the method name and a list of parameters.
You have to fetch the ID number for the method you want to call, then
you have to create an argument array, then you call IDispatch::Invoke
for that ID number.
To call your one example function, this is approximately it:
CComPtr<IDispatch> pDispatch;
pDispatch.CoCreateInstance( your_clsid );
DISPID id;
pDispatch->GetIDsOfNames( IID_NULL, L"getNextNum", 1,
LOCALE_USER_DEFAULT, &id );
DISPPARAMS dparams;
ZeroMemory( &dparams, sizeof(DISPPARAMS) );
VARIANT vRet;
VariantInit( &vRet );
Invoke( pDispatch, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&dparams, &vRet, NULL, NULL );
The result comes back in vRet. The type will probably have vRet.vt ==
VT_I4, and the value is in V_I4(&vRet).
This is why most people don't use late-binding IDispatch objects from C++.
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list