how to use python com server in c++?

Stefan Schukat SSchukat at dspace.de
Mon Aug 21 08:14:46 EDT 2006


Hello, 

you have to use the low level API methods for access and then the
methods of
IDispatch since the win32com gateway only supports late bound calls for
dispatch
interfaces.

e.g. 

	IDispatch* pDisp;
	CoCreateInstance( ... CLSID_Leelay, ...., IID_IDispatch,
...&pDisp);

	// Add Call without any error checking
	LCID lcid = LOCALE_USER_DEFAULT;
	OLECHAR* pwszName = _T("Add");
	DISPID dispid;
	pDispatch->GetIDsOfNames(IID_NULL, &pwszName, 1, lcid, &dispid);
	VARIANTARG vargsArgs[2];
	VariantInit(&vargsArgs[0]);
	VariantInit(&vargsArgs[1]);
	vargsArgs[0].vt   = VT_I4;
	vargsArgs[0].lVal = 5;
	vargsArgs[1].vt   = VT_I4;
	vargsArgs[1].lVal = 8;
	DISPPARAMS dispparParams = {vargsArgs, 0, 1, NULL};
	VARIANT varResult;
	VariantInit(& varResult);
	HRESULT hr = pDispatch->Invoke(dispid, IID_NULL, lcid,
DISPATCH_METHOD,
      				       &dispparParams, &varResult, NULL,
NULL);
	
Or use a library which has some support for late bound calls, e.g. ATL:
	CComQIPtr<IDispatch> spIDispatch;
	hr = spIDispatch.CoCreateInstance(CLSID_ComServer);
	CComDispatchDriver spSumDisp(spIDispatch);

      CComVariant svarcResult;
      CComVariant svarcParam1(5);
      CComVariant svarcParam2(8);
      spSumDisp.Invoke2(L"Add", &svarcParam1, &svarcParam1,
&svarcResult);


	Stefan

> -----Original Message-----
> From: python-list-bounces+sschukat=dspace.de at python.org 
> [mailto:python-list-bounces+sschukat=dspace.de at python.org] On 
> Behalf Of Leo Jay
> Sent: Saturday, August 19, 2006 5:23 PM
> To: python-list at python.org
> Subject: how to use python com server in c++?
> 
> dear all,
> i have a python com server like this:
> 
> import win32com.server.register
> 
> class HelloWorld:
>     _reg_clsid_         = "{B0EB5AAB-0465-4D54-9CF9-04ADF7F73E4E}"
>     _reg_desc_          = 'Python test com server'
>     _reg_progid_        = "Leojay.ComServer"
>     _public_methods_    = ['Add', 'Mul']
> 
>     def Add(self, a, b):
>         return a+b
>     def Mul(self, a, b):
>         return a*b
> 
> 
> if __name__ == '__main__':
>     win32com.server.register.UseCommandLine(HelloWorld)
> 
> 
> after registering the com server, i can use it in visual basic .net:
>         Dim a As Integer = 5
>         Dim b As Integer = 8
>         Dim h As Object = CreateObject("Leojay.ComServer")
>         MsgBox(h.Add(a, b).ToString() + " " + h.Mul(a, b).ToString())
> 
> but i don't know how to use it in visual c++.
> 
> who has any idea about using this com server in viusal c++?
> a detailed sample of early binding would be better, thanks.
> 
> 
> 
> --
> Best Regards,
> Leo Jay
> --
> http://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list