PythonCOM.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /* PythonCOM.h
  2. Main header for Python COM support.
  3. This file is involved mainly with client side COM support for
  4. Python.
  5. Most COM work put together by Greg Stein and Mark Hammond, with a
  6. few others starting to come out of the closet.
  7. --------------------------------------------------------------------
  8. Thread State Rules
  9. ------------------
  10. These rules apply to PythonCOM in general, and not just to
  11. the client side.
  12. The rules are quite simple, but it is critical they be followed.
  13. In general, errors here will be picked up quite quickly, as Python
  14. will raise a Fatal Error. However, the Release() issue in particular
  15. may keep a number of problems well hidden.
  16. Interfaces:
  17. -----------
  18. Before making ANY call out to COM, you MUST release the Python lock.
  19. This is true to ANY call whatsoever, including the COM call in question,
  20. but also any calls to "->Release();"
  21. This is normally achieved with the calls
  22. PY_INTERFACE_PRECALL and PY_INTERFACE_POSTCALL, which release
  23. and acquire the Python lock.
  24. Gateways:
  25. ---------
  26. Before doing anything related to Python, gateways MUST acquire the
  27. Python lock, and must release it before returning.
  28. This is normally achieved with PY_GATEWAY_METHOD at the top of a
  29. gateway method. This macro resolves to a class, which automatically does
  30. the right thing.
  31. Release:
  32. --------
  33. As mentioned above for Interfaces, EVERY call to Release() must be done
  34. with the Python lock released. This is expanded here.
  35. This is very important, but an error may not be noticed. The problem will
  36. only be seen when the Release() is on a Python object and the Release() is the
  37. final one for the object. In this case, the Python object will attempt to
  38. acquire the Python lock before destroying itself, and Python will raise a
  39. fatal error.
  40. In many many cases, you will not notice this error, but someday, someone will
  41. implement the other side in Python, and suddenly FatalErrors will start
  42. appearing. Make sure you get this right.
  43. Eg, this code is correct:
  44. PY_INTERFACE_PRECALL;
  45. pSomeObj->SomeFunction(pSomeOtherObject);
  46. pSomeOtherObject->Release();
  47. PY_INTERFACE_POSTCALL;
  48. However, this code is WRONG, but will RARELY FAIL.
  49. PY_INTERFACE_PRECALL;
  50. pSomeObj->SomeFunction(pSomeOtherObject);
  51. PY_INTERFACE_POSTCALL;
  52. pSomeOtherObject->Release();
  53. --------------------------------------------------------------------
  54. */
  55. #ifndef __PYTHONCOM_H__
  56. #define __PYTHONCOM_H__
  57. // #define _DEBUG_LIFETIMES // Trace COM object lifetimes.
  58. #ifdef FREEZE_PYTHONCOM
  59. /* The pythoncom module is being included in a frozen .EXE/.DLL */
  60. #define PYCOM_EXPORT
  61. #else
  62. #ifdef BUILD_PYTHONCOM
  63. /* We are building pythoncomxx.dll */
  64. #define PYCOM_EXPORT __declspec(dllexport)
  65. #else
  66. /* This module uses pythoncomxx.dll */
  67. #define PYCOM_EXPORT __declspec(dllimport)
  68. #ifndef _DEBUG
  69. #pragma comment(lib, "pythoncom.lib")
  70. #else
  71. #pragma comment(lib, "pythoncom_d.lib")
  72. #endif
  73. #endif
  74. #endif
  75. #ifdef MS_WINCE
  76. // List of interfaces not supported by CE.
  77. #define NO_PYCOM_IDISPATCHEX
  78. #define NO_PYCOM_IPROVIDECLASSINFO
  79. #define NO_PYCOM_IENUMGUID
  80. #define NO_PYCOM_IENUMCATEGORYINFO
  81. #define NO_PYCOM_ICATINFORMATION
  82. #define NO_PYCOM_ICATREGISTER
  83. #define NO_PYCOM_ISERVICEPROVIDER
  84. #define NO_PYCOM_IPROPERTYSTORAGE
  85. #define NO_PYCOM_IPROPERTYSETSTORAGE
  86. #define NO_PYCOM_ENUMSTATPROPSTG
  87. #include "ocidl.h"
  88. #include "oleauto.h"
  89. #endif // MS_WINCE
  90. #ifdef __MINGW32__
  91. // Special Mingw32 considerations.
  92. #define NO_PYCOM_ENUMSTATPROPSTG
  93. #define __try try
  94. #define __except catch
  95. #include <olectl.h>
  96. #endif // __MINGW32__
  97. #include <PyWinTypes.h> // Standard Win32 Types
  98. #ifndef NO_PYCOM_IDISPATCHEX
  99. #include <dispex.h> // New header for IDispatchEx interface.
  100. #endif // NO_PYCOM_IDISPATCHEX
  101. #if defined(MAINWIN)
  102. // Mainwin seems to have 1/2 the VT_RECORD infrastructure in place
  103. #if !defined(VT_RECORD)
  104. #define VT_RECORD 36
  105. #define V_RECORDINFO(X) ((X)->brecVal.pRecInfo)
  106. #define V_RECORD(X) ((X)->brecVal.pvRecord)
  107. #else
  108. #pragma message( \
  109. "MAINWIN appears to have grown correct VT_RECORD " \
  110. "support. Please update PythonCOM.h accordingly")
  111. #endif // VT_RECORD
  112. #endif // MAINWIN
  113. class PyIUnknown;
  114. // To make life interesting/complicated, I use C++ classes for
  115. // all Python objects. The main advantage is that I can derive
  116. // a PyIDispatch object from a PyIUnknown, etc. This provides a
  117. // clean C++ interface, and "automatically" provides all base
  118. // Python methods to "derived" Python types.
  119. //
  120. // Main disadvantage is that any extension DLLs will need to include
  121. // these headers, and link with this .lib
  122. //
  123. // Base class for (most of) the type objects.
  124. class PYCOM_EXPORT PyComTypeObject : public PyTypeObject {
  125. public:
  126. PyComTypeObject(const char *name, PyComTypeObject *pBaseType, int typeSize, struct PyMethodDef *methodList,
  127. PyIUnknown *(*thector)(IUnknown *));
  128. ~PyComTypeObject();
  129. // is the given object an interface type object? (e.g. PyIUnknown)
  130. static BOOL is_interface_type(PyObject *ob);
  131. public:
  132. PyIUnknown *(*ctor)(IUnknown *);
  133. };
  134. // A type used for interfaces that can automatically provide enumerators
  135. // (ie, they themselves aren't enumerable, but do have a suitable default
  136. // method that returns a PyIEnum object
  137. class PYCOM_EXPORT PyComEnumProviderTypeObject : public PyComTypeObject {
  138. public:
  139. PyComEnumProviderTypeObject(const char *name, PyComTypeObject *pBaseType, int typeSize,
  140. struct PyMethodDef *methodList, PyIUnknown *(*thector)(IUnknown *),
  141. const char *enum_method_name);
  142. static PyObject *iter(PyObject *self);
  143. const char *enum_method_name;
  144. };
  145. // A type used for PyIEnum interfaces
  146. class PYCOM_EXPORT PyComEnumTypeObject : public PyComTypeObject {
  147. public:
  148. static PyObject *iter(PyObject *self);
  149. static PyObject *iternext(PyObject *self);
  150. PyComEnumTypeObject(const char *name, PyComTypeObject *pBaseType, int typeSize, struct PyMethodDef *methodList,
  151. PyIUnknown *(*thector)(IUnknown *));
  152. };
  153. // Very very base class - not COM specific - Should exist in the
  154. // Python core somewhere, IMO.
  155. class PYCOM_EXPORT PyIBase : public PyObject {
  156. public:
  157. // virtuals for Python support
  158. virtual PyObject *getattr(char *name);
  159. virtual int setattr(char *name, PyObject *v);
  160. virtual PyObject *repr();
  161. virtual int compare(PyObject *other)
  162. {
  163. if (this == other)
  164. return 0;
  165. if (this < other)
  166. return -1;
  167. return 1;
  168. }
  169. // These iter are a little special, in that returning NULL means
  170. // use the implementation in the type
  171. virtual PyObject *iter() { return NULL; }
  172. virtual PyObject *iternext() { return NULL; }
  173. protected:
  174. PyIBase();
  175. virtual ~PyIBase();
  176. public:
  177. static BOOL is_object(PyObject *, PyComTypeObject *which);
  178. BOOL is_object(PyComTypeObject *which);
  179. static void dealloc(PyObject *ob);
  180. static PyObject *repr(PyObject *ob);
  181. static PyObject *getattro(PyObject *self, PyObject *name);
  182. static int setattro(PyObject *op, PyObject *obname, PyObject *v);
  183. static int cmp(PyObject *ob1, PyObject *ob2);
  184. static PyObject *richcmp(PyObject *ob1, PyObject *ob2, int op);
  185. };
  186. /* Special Type objects */
  187. extern PYCOM_EXPORT PyTypeObject PyOleEmptyType; // equivalent to VT_EMPTY
  188. extern PYCOM_EXPORT PyTypeObject PyOleMissingType; // special Python handling.
  189. extern PYCOM_EXPORT PyTypeObject PyOleArgNotFoundType; // special VT_ERROR value
  190. extern PYCOM_EXPORT PyTypeObject PyOleNothingType; // special VT_ERROR value
  191. // ALL of these set an appropriate Python error on bad return.
  192. // Given a Python object that is a registered COM type, return a given
  193. // interface pointer on its underlying object, with a new reference added.
  194. PYCOM_EXPORT BOOL PyCom_InterfaceFromPyObject(PyObject *ob, REFIID iid, LPVOID *ppv, BOOL bNoneOK = TRUE);
  195. // As above, but allows instance with "_oleobj_" attribute.
  196. PYCOM_EXPORT BOOL PyCom_InterfaceFromPyInstanceOrObject(PyObject *ob, REFIID iid, LPVOID *ppv, BOOL bNoneOK = TRUE);
  197. // Release an arbitary COM pointer.
  198. // NOTE: the PRECALL/POSTCALL stuff is probably not strictly necessary
  199. // since the PyGILSTATE stuff has been in place (and even then, it only
  200. // mattered when it was the last Release() on a Python implemented object)
  201. #define PYCOM_RELEASE(pUnk) \
  202. { \
  203. if (pUnk) { \
  204. PY_INTERFACE_PRECALL; \
  205. (pUnk)->Release(); \
  206. PY_INTERFACE_POSTCALL; \
  207. } \
  208. }
  209. // Given an IUnknown and an Interface ID, create and return an object
  210. // of the appropriate type. eg IID_Unknown->PyIUnknown,
  211. // IID_IDispatch->PyIDispatch, etc.
  212. // Uses a map that external extension DLLs can populate with their IID/type.
  213. // Under the principal of least surprise, this will return Py_None is punk is NULL.
  214. // Otherwise, a valid PyI*, but with NULL m_obj (and therefore totally useless)
  215. // object would be created.
  216. // BOOL bAddRef indicates if a COM reference count should be added to the IUnknown.
  217. // This depends purely on the context in which it is called. If the IUnknown is obtained
  218. // from a function that creates a new ref (eg, CoCreateInstance()) then you should use
  219. // FALSE. If you receive the pointer as (eg) a param to a gateway function, then
  220. // you normally need to pass TRUE, as this is truly a new reference.
  221. // *** ALWAYS take the time to get this right. ***
  222. PYCOM_EXPORT PyObject *PyCom_PyObjectFromIUnknown(IUnknown *punk, REFIID riid, BOOL bAddRef = FALSE);
  223. // VARIANT <-> PyObject conversion utilities.
  224. PYCOM_EXPORT BOOL PyCom_VariantFromPyObject(PyObject *obj, VARIANT *var);
  225. PYCOM_EXPORT PyObject *PyCom_PyObjectFromVariant(const VARIANT *var);
  226. // PROPVARIANT
  227. PYCOM_EXPORT PyObject *PyObject_FromPROPVARIANT(PROPVARIANT *pVar);
  228. PYCOM_EXPORT PyObject *PyObject_FromPROPVARIANTs(PROPVARIANT *pVars, ULONG cVars);
  229. PYCOM_EXPORT BOOL PyObject_AsPROPVARIANT(PyObject *ob, PROPVARIANT *pVar);
  230. // Other conversion helpers...
  231. PYCOM_EXPORT PyObject *PyCom_PyObjectFromSTATSTG(STATSTG *pStat);
  232. PYCOM_EXPORT BOOL PyCom_PyObjectAsSTATSTG(PyObject *ob, STATSTG *pStat, DWORD flags = 0);
  233. PYCOM_EXPORT BOOL PyCom_SAFEARRAYFromPyObject(PyObject *obj, SAFEARRAY **ppSA, VARENUM vt = VT_VARIANT);
  234. PYCOM_EXPORT PyObject *PyCom_PyObjectFromSAFEARRAY(SAFEARRAY *psa, VARENUM vt = VT_VARIANT);
  235. #ifndef NO_PYCOM_STGOPTIONS
  236. PYCOM_EXPORT BOOL PyCom_PyObjectAsSTGOPTIONS(PyObject *obstgoptions, STGOPTIONS **ppstgoptions);
  237. #endif
  238. PYCOM_EXPORT PyObject *PyCom_PyObjectFromSTATPROPSETSTG(STATPROPSETSTG *pStat);
  239. PYCOM_EXPORT BOOL PyCom_PyObjectAsSTATPROPSETSTG(PyObject *, STATPROPSETSTG *);
  240. // Currency support.
  241. PYCOM_EXPORT PyObject *PyObject_FromCurrency(CURRENCY &cy);
  242. PYCOM_EXPORT BOOL PyObject_AsCurrency(PyObject *ob, CURRENCY *pcy);
  243. // OLEMENUGROUPWIDTHS are used by axcontrol, shell, etc
  244. PYCOM_EXPORT BOOL PyObject_AsOLEMENUGROUPWIDTHS(PyObject *oblpMenuWidths, OLEMENUGROUPWIDTHS *pWidths);
  245. PYCOM_EXPORT PyObject *PyObject_FromOLEMENUGROUPWIDTHS(const OLEMENUGROUPWIDTHS *pWidths);
  246. /* Functions for Initializing COM, and also letting the core know about it!
  247. */
  248. PYCOM_EXPORT HRESULT PyCom_CoInitializeEx(LPVOID reserved, DWORD dwInit);
  249. PYCOM_EXPORT HRESULT PyCom_CoInitialize(LPVOID reserved);
  250. PYCOM_EXPORT void PyCom_CoUninitialize();
  251. ///////////////////////////////////////////////////////////////////
  252. // Error related functions
  253. // Client related functions - generally called by interfaces before
  254. // they return NULL back to Python to indicate the error.
  255. // All these functions return NULL so interfaces can generally
  256. // just "return PyCom_BuildPyException(hr, punk, IID_IWhatever)"
  257. // Uses the HRESULT, and IErrorInfo interfaces if available to
  258. // create and set a pythoncom.com_error.
  259. PYCOM_EXPORT PyObject *PyCom_BuildPyException(HRESULT hr, IUnknown *pUnk = NULL, REFIID iid = IID_NULL);
  260. // Uses the HRESULT and an EXCEPINFO structure to create and
  261. // set a pythoncom.com_error.
  262. PYCOM_EXPORT PyObject *PyCom_BuildPyExceptionFromEXCEPINFO(HRESULT hr, EXCEPINFO *pexcepInfo, UINT nArgErr = (UINT)-1);
  263. // Sets a pythoncom.internal_error - no one should ever see these!
  264. PYCOM_EXPORT PyObject *PyCom_BuildInternalPyException(char *msg);
  265. // Log an error to a Python logger object if one can be found, or
  266. // to stderr if no log available.
  267. // If logProvider is not NULL, we will call a "_GetLogger_()" method on it.
  268. // If logProvider is NULL, we attempt to fetch "win32com.logger".
  269. // If they do not exist, return None, or raise an error fetching them
  270. // (or even writing to them once fetched), the message still goes to stderr.
  271. // NOTE: By default, win32com does *not* provide a logger, so default is that
  272. // all errors are written to stdout.
  273. // This will *not* write a record if a COM Server error is current.
  274. PYCOM_EXPORT void PyCom_LoggerNonServerException(PyObject *logProvider, const char *fmt, ...);
  275. // Write an error record, including exception. This will write an error
  276. // record even if a COM server error is current.
  277. PYCOM_EXPORT void PyCom_LoggerException(PyObject *logProvider, const char *fmt, ...);
  278. // Write a warning record - in general this does *not* mean a call failed, but
  279. // still is something in the programmers control that they should change.
  280. // XXX - if an exception is pending when this is called, the traceback will
  281. // also be written. This is undesirable and will be changed should this
  282. // start being a problem.
  283. PYCOM_EXPORT void PyCom_LoggerWarning(PyObject *logProvider, const char *fmt, ...);
  284. // Server related error functions
  285. // These are supplied so that any Python errors we detect can be
  286. // converted into COM error information. The HRESULT returned should
  287. // be returned by the COM function, and these functions also set the
  288. // IErrorInfo interfaces, so the caller can extract more detailed
  289. // information about the Python exception.
  290. // Set a COM exception, logging the exception if not an explicitly raised 'server' exception
  291. PYCOM_EXPORT HRESULT PyCom_SetAndLogCOMErrorFromPyException(const char *methodName, REFIID riid /* = IID_NULL */);
  292. PYCOM_EXPORT HRESULT PyCom_SetAndLogCOMErrorFromPyExceptionEx(PyObject *provider, const char *methodName,
  293. REFIID riid /* = IID_NULL */);
  294. // Used in gateways to SetErrorInfo() with a simple HRESULT, then return it.
  295. // The description is generally only useful for debugging purposes,
  296. // and if you are debugging via a server that supports IErrorInfo (like Python :-)
  297. // NOTE: this function is usuable from outside the Python context
  298. PYCOM_EXPORT HRESULT PyCom_SetCOMErrorFromSimple(HRESULT hr, REFIID riid = IID_NULL, const char *description = NULL);
  299. // Used in gateways to SetErrorInfo() the current Python exception, and
  300. // (assuming not a server error explicitly raised) also logs an error
  301. // to stdout/win32com.logger.
  302. // NOTE: this function assumes GIL held
  303. PYCOM_EXPORT HRESULT PyCom_SetCOMErrorFromPyException(REFIID riid = IID_NULL);
  304. // A couple of EXCEPINFO helpers - could be private to IDispatch
  305. // if it wasnt for the AXScript support (and ITypeInfo if we get around to that :-)
  306. // These functions do not set any error states to either Python or
  307. // COM - they simply convert to/from PyObjects and EXCEPINFOs
  308. // Use the current Python exception to fill an EXCEPINFO structure.
  309. PYCOM_EXPORT void PyCom_ExcepInfoFromPyException(EXCEPINFO *pExcepInfo);
  310. // Fill in an EXCEPINFO structure from a Python instance or tuple object.
  311. // (ie, similar to the above, except the Python exception object is specified,
  312. // rather than using the "current"
  313. PYCOM_EXPORT BOOL PyCom_ExcepInfoFromPyObject(PyObject *obExcepInfo, EXCEPINFO *pexcepInfo, HRESULT *phresult = NULL);
  314. // Create a Python object holding the exception information. The exception
  315. // information is *not* freed by this function. Python exceptions are
  316. // raised and NULL is returned if an error occurs.
  317. PYCOM_EXPORT PyObject *PyCom_PyObjectFromExcepInfo(const EXCEPINFO *pexcepInfo);
  318. ///////////////////////////////////////////////////////////////////
  319. //
  320. // External C++ helpers - these helpers are for other DLLs which
  321. // may need similar functionality, but dont want to duplicate all
  322. // This helper is for an application that has an IDispatch, and COM arguments
  323. // and wants to call a Python function. It is assumed the caller can map the IDispatch
  324. // to a Python object, so the Python handler is passed.
  325. // Args:
  326. // handler : A Python callable object.
  327. // dispparms : the COM arguments.
  328. // pVarResult : The variant for the return value of the Python call.
  329. // pexcepinfo : Exception info the helper may fill out.
  330. // puArgErr : Argument error the helper may fill out on exception
  331. // addnArgs : Any additional arguments to the Python function. May be NULL.
  332. // If addnArgs is NULL, then it is assumed the Python call should be native -
  333. // ie, the COM args are packed as normal Python args to the call.
  334. // If addnArgs is NOT NULL, it is assumed the Python function itself is
  335. // a helper. This Python function will be called with 2 arguments - both
  336. // tuples - first one is the COM args, second is the addn args.
  337. PYCOM_EXPORT BOOL PyCom_MakeOlePythonCall(PyObject *handler, DISPPARAMS FAR *params, VARIANT FAR *pVarResult,
  338. EXCEPINFO FAR *pexcepinfo, UINT FAR *puArgErr, PyObject *addnlArgs);
  339. /////////////////////////////////////////////////////////////////////////////
  340. // Various special purpose singletons
  341. class PYCOM_EXPORT PyOleEmpty : public PyObject {
  342. public:
  343. PyOleEmpty();
  344. };
  345. class PYCOM_EXPORT PyOleMissing : public PyObject {
  346. public:
  347. PyOleMissing();
  348. };
  349. class PYCOM_EXPORT PyOleArgNotFound : public PyObject {
  350. public:
  351. PyOleArgNotFound();
  352. };
  353. class PYCOM_EXPORT PyOleNothing : public PyObject {
  354. public:
  355. PyOleNothing();
  356. };
  357. // We need to dynamically create C++ Python objects
  358. // These helpers allow each type object to create it.
  359. #define MAKE_PYCOM_CTOR(classname) \
  360. static PyIUnknown *classname::PyObConstruct(IUnknown *pInitObj) { return new classname(pInitObj); }
  361. #define MAKE_PYCOM_CTOR_ERRORINFO(classname, iid) \
  362. static PyIUnknown *classname::PyObConstruct(IUnknown *pInitObj) { return new classname(pInitObj); } \
  363. static PyObject *SetPythonCOMError(PyObject *self, HRESULT hr) \
  364. { \
  365. return PyCom_BuildPyException(hr, GetI(self), iid); \
  366. }
  367. #define GET_PYCOM_CTOR(classname) classname::PyObConstruct
  368. // Macros that interfaces should use. PY_INTERFACE_METHOD at the top of the method
  369. // The other 2 wrap directly around the underlying method call.
  370. #define PY_INTERFACE_METHOD
  371. // Identical to Py_BEGIN_ALLOW_THREADS except no { !!!
  372. #define PY_INTERFACE_PRECALL PyThreadState *_save = PyEval_SaveThread();
  373. #define PY_INTERFACE_POSTCALL PyEval_RestoreThread(_save);
  374. /////////////////////////////////////////////////////////////////////////////
  375. // class PyIUnknown
  376. class PYCOM_EXPORT PyIUnknown : public PyIBase {
  377. public:
  378. MAKE_PYCOM_CTOR(PyIUnknown);
  379. virtual PyObject *repr();
  380. virtual int compare(PyObject *other);
  381. static IUnknown *GetI(PyObject *self);
  382. IUnknown *m_obj;
  383. static char *szErrMsgObjectReleased;
  384. static void SafeRelease(PyIUnknown *ob);
  385. static PyComTypeObject type;
  386. // The Python methods
  387. static PyObject *QueryInterface(PyObject *self, PyObject *args);
  388. static PyObject *SafeRelease(PyObject *self, PyObject *args);
  389. protected:
  390. PyIUnknown(IUnknown *punk);
  391. ~PyIUnknown();
  392. };
  393. /////////////////////////////////////////////////////////////////////////////
  394. // class PyIDispatch
  395. class PYCOM_EXPORT PyIDispatch : public PyIUnknown {
  396. public:
  397. MAKE_PYCOM_CTOR(PyIDispatch);
  398. static IDispatch *GetI(PyObject *self);
  399. static PyComTypeObject type;
  400. // The Python methods
  401. static PyObject *Invoke(PyObject *self, PyObject *args);
  402. static PyObject *InvokeTypes(PyObject *self, PyObject *args);
  403. static PyObject *GetIDsOfNames(PyObject *self, PyObject *args);
  404. static PyObject *GetTypeInfo(PyObject *self, PyObject *args);
  405. static PyObject *GetTypeInfoCount(PyObject *self, PyObject *args);
  406. protected:
  407. PyIDispatch(IUnknown *pdisp);
  408. ~PyIDispatch();
  409. };
  410. #ifndef NO_PYCOM_IDISPATCHEX
  411. /////////////////////////////////////////////////////////////////////////////
  412. // class PyIDispatchEx
  413. class PYCOM_EXPORT PyIDispatchEx : public PyIDispatch {
  414. public:
  415. MAKE_PYCOM_CTOR_ERRORINFO(PyIDispatchEx, IID_IDispatchEx);
  416. static IDispatchEx *GetI(PyObject *self);
  417. static PyComTypeObject type;
  418. // The Python methods
  419. static PyObject *GetDispID(PyObject *self, PyObject *args);
  420. static PyObject *InvokeEx(PyObject *self, PyObject *args);
  421. static PyObject *DeleteMemberByName(PyObject *self, PyObject *args);
  422. static PyObject *DeleteMemberByDispID(PyObject *self, PyObject *args);
  423. static PyObject *GetMemberProperties(PyObject *self, PyObject *args);
  424. static PyObject *GetMemberName(PyObject *self, PyObject *args);
  425. static PyObject *GetNextDispID(PyObject *self, PyObject *args);
  426. protected:
  427. PyIDispatchEx(IUnknown *pdisp);
  428. ~PyIDispatchEx();
  429. };
  430. #endif // NO_PYCOM_IDISPATCHEX
  431. /////////////////////////////////////////////////////////////////////////////
  432. // class PyIClassFactory
  433. class PYCOM_EXPORT PyIClassFactory : public PyIUnknown {
  434. public:
  435. MAKE_PYCOM_CTOR(PyIClassFactory);
  436. static IClassFactory *GetI(PyObject *self);
  437. static PyComTypeObject type;
  438. // The Python methods
  439. static PyObject *CreateInstance(PyObject *self, PyObject *args);
  440. static PyObject *LockServer(PyObject *self, PyObject *args);
  441. protected:
  442. PyIClassFactory(IUnknown *pdisp);
  443. ~PyIClassFactory();
  444. };
  445. #ifndef NO_PYCOM_IPROVIDECLASSINFO
  446. /////////////////////////////////////////////////////////////////////////////
  447. // class PyIProvideTypeInfo
  448. class PYCOM_EXPORT PyIProvideClassInfo : public PyIUnknown {
  449. public:
  450. MAKE_PYCOM_CTOR(PyIProvideClassInfo);
  451. static IProvideClassInfo *GetI(PyObject *self);
  452. static PyComTypeObject type;
  453. // The Python methods
  454. static PyObject *GetClassInfo(PyObject *self, PyObject *args);
  455. protected:
  456. PyIProvideClassInfo(IUnknown *pdisp);
  457. ~PyIProvideClassInfo();
  458. };
  459. class PYCOM_EXPORT PyIProvideClassInfo2 : public PyIProvideClassInfo {
  460. public:
  461. MAKE_PYCOM_CTOR(PyIProvideClassInfo2);
  462. static IProvideClassInfo2 *GetI(PyObject *self);
  463. static PyComTypeObject type;
  464. // The Python methods
  465. static PyObject *GetGUID(PyObject *self, PyObject *args);
  466. protected:
  467. PyIProvideClassInfo2(IUnknown *pdisp);
  468. ~PyIProvideClassInfo2();
  469. };
  470. #endif // NO_PYCOM_IPROVIDECLASSINFO
  471. /////////////////////////////////////////////////////////////////////////////
  472. // class PyITypeInfo
  473. class PYCOM_EXPORT PyITypeInfo : public PyIUnknown {
  474. public:
  475. MAKE_PYCOM_CTOR(PyITypeInfo);
  476. static PyComTypeObject type;
  477. static ITypeInfo *GetI(PyObject *self);
  478. PyObject *GetContainingTypeLib();
  479. PyObject *GetDocumentation(MEMBERID);
  480. PyObject *GetRefTypeInfo(HREFTYPE href);
  481. PyObject *GetRefTypeOfImplType(int index);
  482. PyObject *GetFuncDesc(int pos);
  483. PyObject *GetIDsOfNames(OLECHAR FAR *FAR *, int);
  484. PyObject *GetNames(MEMBERID);
  485. PyObject *GetTypeAttr();
  486. PyObject *GetVarDesc(int pos);
  487. PyObject *GetImplTypeFlags(int index);
  488. PyObject *GetTypeComp();
  489. protected:
  490. PyITypeInfo(IUnknown *);
  491. ~PyITypeInfo();
  492. };
  493. /////////////////////////////////////////////////////////////////////////////
  494. // class PyITypeComp
  495. class PYCOM_EXPORT PyITypeComp : public PyIUnknown {
  496. public:
  497. MAKE_PYCOM_CTOR(PyITypeComp);
  498. static PyComTypeObject type;
  499. static ITypeComp *GetI(PyObject *self);
  500. PyObject *Bind(OLECHAR *szName, unsigned short wflags);
  501. PyObject *BindType(OLECHAR *szName);
  502. protected:
  503. PyITypeComp(IUnknown *);
  504. ~PyITypeComp();
  505. };
  506. /////////////////////////////////////////////////////////////////////////////
  507. // class CPyTypeLib
  508. class PYCOM_EXPORT PyITypeLib : public PyIUnknown {
  509. public:
  510. MAKE_PYCOM_CTOR(PyITypeLib);
  511. static PyComTypeObject type;
  512. static ITypeLib *GetI(PyObject *self);
  513. PyObject *GetLibAttr();
  514. PyObject *GetDocumentation(int pos);
  515. PyObject *GetTypeInfo(int pos);
  516. PyObject *GetTypeInfoCount();
  517. PyObject *GetTypeInfoOfGuid(REFGUID guid);
  518. PyObject *GetTypeInfoType(int pos);
  519. PyObject *GetTypeComp();
  520. protected:
  521. PyITypeLib(IUnknown *);
  522. ~PyITypeLib();
  523. };
  524. /////////////////////////////////////////////////////////////////////////////
  525. // class PyIConnectionPoint
  526. class PYCOM_EXPORT PyIConnectionPoint : public PyIUnknown {
  527. public:
  528. MAKE_PYCOM_CTOR_ERRORINFO(PyIConnectionPoint, IID_IConnectionPoint);
  529. static PyComTypeObject type;
  530. static IConnectionPoint *GetI(PyObject *self);
  531. static PyObject *GetConnectionInterface(PyObject *self, PyObject *args);
  532. static PyObject *GetConnectionPointContainer(PyObject *self, PyObject *args);
  533. static PyObject *Advise(PyObject *self, PyObject *args);
  534. static PyObject *Unadvise(PyObject *self, PyObject *args);
  535. static PyObject *EnumConnections(PyObject *self, PyObject *args);
  536. protected:
  537. PyIConnectionPoint(IUnknown *);
  538. ~PyIConnectionPoint();
  539. };
  540. class PYCOM_EXPORT PyIConnectionPointContainer : public PyIUnknown {
  541. public:
  542. MAKE_PYCOM_CTOR_ERRORINFO(PyIConnectionPointContainer, IID_IConnectionPointContainer);
  543. static PyComTypeObject type;
  544. static IConnectionPointContainer *GetI(PyObject *self);
  545. static PyObject *EnumConnectionPoints(PyObject *self, PyObject *args);
  546. static PyObject *FindConnectionPoint(PyObject *self, PyObject *args);
  547. protected:
  548. PyIConnectionPointContainer(IUnknown *);
  549. ~PyIConnectionPointContainer();
  550. };
  551. /////////////////////////////////////////////////////////////////////////////
  552. // class PythonOleArgHelper
  553. //
  554. // A PythonOleArgHelper is used primarily to help out Python helpers
  555. // which need to convert from a Python object when the specific OLE
  556. // type is known - eg, when a TypeInfo is available.
  557. //
  558. // The type of conversion determines who owns what buffers etc. I wish BYREF didnt exist :-)
  559. typedef enum {
  560. // We dont know what sort of conversion it is yet.
  561. POAH_CONVERT_UNKNOWN,
  562. // A PyObject is given, we convert to a VARIANT, make the COM call, then BYREFs back to a PyObject
  563. // ie, this is typically a "normal" COM call, where Python initiates the call
  564. POAH_CONVERT_FROM_PYOBJECT,
  565. // A VARIANT is given, we convert to a PyObject, make the Python call, then BYREFs back to a VARIANT.
  566. // ie, this is typically handling a COM event, where COM itself initiates the call.
  567. POAH_CONVERT_FROM_VARIANT,
  568. } POAH_CONVERT_DIRECTION;
  569. class PYCOM_EXPORT PythonOleArgHelper {
  570. public:
  571. PythonOleArgHelper();
  572. ~PythonOleArgHelper();
  573. BOOL ParseTypeInformation(PyObject *reqdObjectTuple);
  574. // Using this call with reqdObject != NULL will check the existing
  575. // VT_ of the variant. If not VT_EMPTY, then the result will be coerced to
  576. // that type. This contrasts with PyCom_PyObjectToVariant which just
  577. // uses the Python type to determine the variant type.
  578. BOOL MakeObjToVariant(PyObject *obj, VARIANT *var, PyObject *reqdObjectTuple = NULL);
  579. PyObject *MakeVariantToObj(VARIANT *var);
  580. VARTYPE m_reqdType;
  581. BOOL m_bParsedTypeInfo;
  582. BOOL m_bIsOut;
  583. POAH_CONVERT_DIRECTION m_convertDirection;
  584. PyObject *m_pyVariant; // if non-null, a win32com.client.VARIANT
  585. union {
  586. void *m_pValueHolder;
  587. short m_sBuf;
  588. long m_lBuf;
  589. LONGLONG m_llBuf;
  590. VARIANT_BOOL m_boolBuf;
  591. double m_dBuf;
  592. float m_fBuf;
  593. IDispatch *m_dispBuf;
  594. IUnknown *m_unkBuf;
  595. SAFEARRAY *m_arrayBuf;
  596. VARIANT *m_varBuf;
  597. DATE m_dateBuf;
  598. CY m_cyBuf;
  599. };
  600. };
  601. /////////////////////////////////////////////////////////////////////////////
  602. // global functions and variables
  603. PYCOM_EXPORT BOOL MakePythonArgumentTuples(PyObject **pArgs, PythonOleArgHelper **ppHelpers, PyObject **pNamedArgs,
  604. PythonOleArgHelper **ppNamedHelpers, DISPPARAMS FAR *params);
  605. // Convert a Python object to a BSTR - allow embedded NULLs, None, etc.
  606. PYCOM_EXPORT BOOL PyCom_BstrFromPyObject(PyObject *stringObject, BSTR *pResult, BOOL bNoneOK = FALSE);
  607. // MakeBstrToObj - convert a BSTR into a Python string.
  608. //
  609. // ONLY USE THIS FOR TRUE BSTR's - Use the fn below for OLECHAR *'s.
  610. // NOTE - does not use standard macros, so NULLs get through!
  611. PYCOM_EXPORT PyObject *MakeBstrToObj(const BSTR bstr);
  612. // Size info is available (eg, a fn returns a string and also fills in a size variable)
  613. PYCOM_EXPORT PyObject *MakeOLECHARToObj(const OLECHAR *str, int numChars);
  614. // No size info avail.
  615. PYCOM_EXPORT PyObject *MakeOLECHARToObj(const OLECHAR *str);
  616. PYCOM_EXPORT void PyCom_LogF(const char *fmt, ...);
  617. // Generic conversion from python sequence to VT_VECTOR array
  618. // Resulting array must be freed with CoTaskMemFree
  619. template <typename arraytype>
  620. BOOL SeqToVector(PyObject *ob, arraytype **pA, ULONG *pcount, BOOL (*converter)(PyObject *, arraytype *))
  621. {
  622. TmpPyObject seq = PyWinSequence_Tuple(ob, pcount);
  623. if (seq == NULL)
  624. return FALSE;
  625. *pA = (arraytype *)CoTaskMemAlloc(*pcount * sizeof(arraytype));
  626. if (*pA == NULL) {
  627. PyErr_NoMemory();
  628. return FALSE;
  629. }
  630. for (ULONG i = 0; i < *pcount; i++) {
  631. PyObject *item = PyTuple_GET_ITEM((PyObject *)seq, i);
  632. if (!(*converter)(item, &(*pA)[i]))
  633. return FALSE;
  634. }
  635. return TRUE;
  636. }
  637. #endif // __PYTHONCOM_H__