[python-win32] Passing IDispatch from a C program to Python -- Is it possible?

Finn Bragason fbragason@cggenesisproject.com
Tue, 4 Feb 2003 15:44:10 +1100


I want to create a COM object in a C/C++ program and pass it to an
embedded Python program.
So my C/C++ program is something like:

// converter function
int converter(PyObject * obj, void * p)
{
	// Initialise obj with p -- somehow
	return 1;
}


main()
...
// create a COM object
IConnectPtr conn;;
HRESULT hr =3D conn.CreateInstance(__uuidof(Connect));
... do something with conn
// Do the Python embedding initialise
Py_Initialize();
..etc..etc
// Create a PyObject from the conn Interface to pass to Python
PyObject * p =3D Py_BuildValue("O&", converter, *conn);
// Set the newly created object as a parameter to python
PyTuple_SetItem(pArgs, 1, p);
// Call Python
PyObject_CallObject(func, pArgs);

My Python program would be something like:
def func(conn):
	# Test if conn object is connected
	if conn.IsConnected:
		# do something with it

Any comments greatly appreciated.
Fin