[python-win32] Passing data to/from Python script

Mark Hammond mhammond at skippinet.com.au
Thu Nov 19 05:11:59 CET 2009


On 19/11/2009 5:13 AM, Hung Nguyen wrote:
> Thank you Mark. I will look at the .h file for more details as you suggested. However if you can elaborate a little bit more at a high level as how to pass a COM objects between host application and scripts, that would be of great help. Also, I am wondering if there's sample code doing stuff like that?

To pass one into Python you would do something like:

   PyObject *ob = PyCom_PyObjectFromIUnknown(pYourOb, IID_YOUR_OB, TRUE);
   PyObject *args = Py_BuildValue("iN", some_int, ob);
   // make the call with args.

Note that if you are trying to use IDispatch, the object passed to the 
script will be a PyIDispatch - the script probably wants to wrap that 
using win32com.client.Dispatch to make it simple to use.

And to get it back, something like:

   PyObject *ob;
   if (!PyArg_ParseTuple(args, "O", &ob))
     return NULL;
   IYourInterface *pYourOb;
   if (!PyCom_InterfaceFromPyInstanceOrObject(ob, IID_YOUR_OB, (void 
**)&pYourOb))
     return NULL;
   // pYourOb is ready to go with a *new* COM reference.

HTH,

Mark


More information about the python-win32 mailing list