[pywin32-checkins] pywin32/com/win32com/src PythonCOM.cpp, 1.50, 1.51
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2008-07-20 03:16:17
|
Update of /cvsroot/pywin32/pywin32/com/win32com/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28897/win32com/src Modified Files: PythonCOM.cpp Log Message: Add pythoncom.ObjectFromAddress() and an example of how to use it. Index: PythonCOM.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/com/win32com/src/PythonCOM.cpp,v retrieving revision 1.50 retrieving revision 1.51 diff -C2 -d -r1.50 -r1.51 *** PythonCOM.cpp 19 Jul 2008 06:23:40 -0000 1.50 --- PythonCOM.cpp 20 Jul 2008 03:16:25 -0000 1.51 *************** *** 1436,1440 **** } - // @pymethod int|pythoncom|PumpWaitingMessages|Pumps all waiting messages for the current thread. // @comm It is sometimes necessary for a COM thread to have a message loop. This function --- 1436,1439 ---- *************** *** 1790,1796 **** --- 1789,1841 ---- return NULL; } + // docs for ObjectFromLresult don't mention reference counting, but + // it does say that you can't call this twice on the same object, and + // it has a signature that implies normal reference counting. So + // we assume this call has already added a reference to the result. return PyCom_PyObjectFromIUnknown((IUnknown *)ret, iid, FALSE); } + // @pymethod <o PyIUnknown>|pythoncom|ObjectFromAddress|Returns a COM object given its address in memory. + // @rdesc This method is useful for applications which return objects via non-standard + // mechanisms - eg, Windows Explorer allows you to send a specific message to the + // explorer window and the result will be the address of an object Explorer implements. + // This method allows you to recover the object from that address. + static PyObject *pythoncom_ObjectFromAddress(PyObject *self, PyObject *args) + { + IID iid = IID_IUnknown; + void *addr; + PyObject *obAddr; + PyObject *obIID = NULL; + // @pyparam int|address||The address which holds a COM object + // @pyparm <o PyIID>|iid|IUnknown|The IID to query + if (!PyArg_ParseTuple(args, "O|O", &obAddr, &obIID)) + return NULL; + if (!PyWinLong_AsVoidPtr(obAddr, &addr)) + return NULL; + if (obIID && !PyWinObject_AsIID(obIID, &iid)) + return NULL; + + HRESULT hr; + IUnknown *ret = 0; + PyThreadState *_save = PyEval_SaveThread(); + PYWINTYPES_TRY + { + hr = ((IUnknown *)addr)->QueryInterface(iid, (void **)&ret); + PyEval_RestoreThread(_save); + } + PYWINTYPES_EXCEPT + { + PyEval_RestoreThread(_save); + return PyErr_Format(PyExc_ValueError, "Address is not a valid COM object (win32 exception attempting to retrieve it!)"); + } + if (FAILED(hr) || ret==0) { + PyCom_BuildPyException(hr); + return NULL; + } + // We've added a reference via the QI above. + return PyCom_PyObjectFromIUnknown(ret, iid, FALSE); + } + + /* List of module functions */ // @module pythoncom|A module, encapsulating the OLE automation API *************** *** 1862,1866 **** { "new", pythoncom_new, 1 }, { "New", pythoncom_new, 1 }, // @pymeth New|Create a new instance of an OLE automation server. ! { "ObjectFromLresult", pythoncom_ObjectFromLresult, 1 }, // @pymeth ObjectFromLresult|Returns a COM object given its address in memory. { "OleInitialize", pythoncom_OleInitialize, 1}, // @pymeth OleInitialize| { "OleGetClipboard", pythoncom_OleGetClipboard, 1}, // @pymeth OleGetClipboard|Retrieves a data object that you can use to access the contents of the clipboard. --- 1907,1912 ---- { "new", pythoncom_new, 1 }, { "New", pythoncom_new, 1 }, // @pymeth New|Create a new instance of an OLE automation server. ! { "ObjectFromAddress", pythoncom_ObjectFromAddress, 1 }, // @pymeth ObjectFromAddress|Returns a COM object given its address in memory. ! { "ObjectFromLresult", pythoncom_ObjectFromLresult, 1 }, // @pymeth ObjectFromLresult|Retrieves a requested interface pointer for an object based on a previously generated object reference. { "OleInitialize", pythoncom_OleInitialize, 1}, // @pymeth OleInitialize| { "OleGetClipboard", pythoncom_OleGetClipboard, 1}, // @pymeth OleGetClipboard|Retrieves a data object that you can use to access the contents of the clipboard. |