[python-win32] Python COM Server, C++ Client - How ???

Aviel, Gal gal.aviel at intel.com
Sun Sep 23 15:05:12 CEST 2007


Hi Ryan,

Thanks very much for your post!! 

Your example looks short and sweet :) and is probably one of the only
examples on the web today showing how to write a C++ client connecting
to an late bound COM server, regardless or Python, so I guess people
from even outside the Python community googling as of tomorrow will
thanks you too ..

Unfortunately, it seems that an include file being used in the code
("atlcomcli.h") is not available on my system, as here they software
guys work with VC6.0 which (from short googling) does not seem to
support ATL (not sure this is true). Probably you developed the code on
a newer version of Visual Studio?

So unfortunately I had to resort to a non-ATL example found at
http://www.ddj.com/cpp/184403558?pgno=1; below is the slightly modified
version of that example which seems to work on my system (note inclusion
of 2 files generated by MIDL, probably not necessary but a simple way to
include all the header files which I need otherwise it won't compile).

Thanks again for your help 

p.s. love your web site, interesting stuff there :)

Thanks - Gal.


#include "../../eclipse_workspace1\Py2RTL\src\IDL\PTCS.h"    // use your
own path here
#include "../../eclipse_workspace1\Py2RTL\src\IDL\PTCS_i.c"  // use your
own path here
#include "iostream.h"


// for showing possible mistakes
void ShowErrorMessage(LPCTSTR,HRESULT);

int main() {


	DISPID dispid;
	DISPPARAMS dp={NULL,NULL,0,0};
	VARIANTARG vargs[2];
	VARIANT arg1,arg2,result;

	IDispatch * idsp;

	WCHAR progid[255];

	CLSID pclsid;

	// 'L' tells the compiler to
	// use two-byte characters
	// COM requires most strings
	// to be in this form to
	// support multiple spoken languages

	wcscpy(progid, L"VCD.PTCS");
	CLSIDFromProgID( progid, &pclsid);

	HRESULT hr = CoInitialize(NULL);
	if ( FAILED(hr) )
	{
		ShowErrorMessage("CoInitialize()",hr);
		exit(1);
	}
	
	hr = CoCreateInstance(pclsid, NULL, CLSCTX_ALL,
			IID_IDispatch, (void **)&idsp);

	if ( FAILED(hr) )
	{
		ShowErrorMessage("CoGetClassObject()",hr);
		exit(1);
	}
	else cout << "success." << endl;

	OLECHAR * name=L"Add";
	idsp->GetIDsOfNames(IID_NULL, &name,
        1, GetUserDefaultLCID(),
        &dispid);


	arg1.vt= VT_UI4;
	arg1.intVal = 3;
     
	arg2.vt= VT_UI4;
	arg2.intVal = 9;

	vargs[0]=arg1;
	vargs[1]=arg2;

	dp.rgvarg=vargs;
	dp.cArgs = 2; //how many args


	idsp->Invoke(dispid, IID_NULL,
        GetUserDefaultLCID(),
        DISPATCH_METHOD, &dp,
        &result,0,0);

	cout<<result.iVal<<endl;

	idsp->Release();

	return 0;

}

void ShowErrorMessage(LPCTSTR header, HRESULT hr)
{
   void* pMsg;

   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
                 FORMAT_MESSAGE_FROM_SYSTEM,NULL,hr,
                 MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), 
                 (LPTSTR)&pMsg,0,NULL);

   cout << header << ": Error(" << hex << hr << "): " 
        << (LPTSTR)pMsg << endl;

   LocalFree(pMsg);
}
---------------------------------------------------------------------
Intel Israel (74) Limited

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.


More information about the python-win32 mailing list