home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_2525 < prev    next >
Encoding:
Text File  |  2008-04-04  |  30.6 KB  |  767 lines

  1.  
  2. #ifndef __PYWINTYPES_H__
  3. #define __PYWINTYPES_H__
  4.  
  5. // If building under a GCC, tweak what we need.
  6. #if defined(__GNUC__) && defined(_POSIX_C_SOURCE)
  7.     // python.h complains if _POSIX_C_SOURCE is already defined
  8. #    undef _POSIX_C_SOURCE
  9. #endif
  10.  
  11. // early msvc versions complain about pragma #s it doesn't understand
  12. // C:\mssdk\VC\INCLUDE\string.h(142) : warning C4616: #pragma warning : warning number '6059' out of range, must be between '4001' and '4999'
  13. // and:
  14. // C:\mssdk\include\wingdi.h(4340) : warning C4068: unknown pragma
  15. // I doubt we ever care about that warning, so unconditionally nuke em!
  16. #pragma warning( disable:4616 4068 )
  17. // Python.h and Windows.h both protect themselves from multiple
  18. // includes - so it is safe to do here (and provides a handy
  19. // choke point for #include vagaries
  20. #include "Python.h"
  21. #include "windows.h"
  22.  
  23. // See PEP-353 - this is the "official" test...
  24. #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
  25. // 2.3 and before have no Py_ssize_t
  26. typedef int Py_ssize_t;
  27. #define PyInt_FromSsize_t PyInt_FromLong
  28. #define PyInt_AsSsize_t PyInt_AsLong
  29. #define PY_SSIZE_T_MAX INT_MAX
  30. #define PY_SSIZE_T_MIN INT_MIN
  31. #endif
  32.  
  33. #if PY_VERSION_HEX < 0x02030000
  34. #define PyLong_AsUnsignedLongMask PyLong_AsUnsignedLong
  35. #endif
  36.  
  37. // This only enables runtime checks in debug builds - so we use
  38. // our own so we can enable it always should we desire...
  39. #define PyWin_SAFE_DOWNCAST Py_SAFE_DOWNCAST
  40.  
  41. // Lars: for WAVEFORMATEX
  42. #include "mmsystem.h"
  43.  
  44. // This can be removed once we are confident noone else uses it...
  45. #define PYWIN_USE_PYUNICODE
  46.  
  47. // *** NOTE *** FREEZE_PYWINTYPES is deprecated.  It used to be used
  48. // by the 'freeze' tool, but now py2exe etc do a far better job, and 
  49. // don't require a custom built pywintypes DLL.
  50. #ifdef FREEZE_PYWINTYPES
  51.     /* The pywintypes module is being included in a frozen .EXE/.DLL */
  52. #    define PYWINTYPES_EXPORT
  53. #else
  54. #    ifdef BUILD_PYWINTYPES
  55.         /* We are building pywintypesxx.dll */
  56. #        define PYWINTYPES_EXPORT __declspec(dllexport)
  57. #else
  58.         /* This module uses pywintypesxx.dll */
  59. #        define PYWINTYPES_EXPORT __declspec(dllimport)
  60. #        if defined(_MSC_VER)
  61. #            if defined(DEBUG) || defined(_DEBUG)
  62. #                pragma comment(lib,"pywintypes_d.lib")
  63. #            else
  64. #                pragma comment(lib,"pywintypes.lib")
  65. #            endif // DEBUG/_DEBUG
  66. #        endif // _MSC_VER
  67. #    endif // BUILD_PYWINTYPES
  68. #endif // FREEZE_PYWINTYPES
  69.  
  70. #include <tchar.h>
  71. /*
  72. ** Error/Exception handling
  73. */
  74. extern PYWINTYPES_EXPORT PyObject *PyWinExc_ApiError;
  75. // Register a Windows DLL that contains the messages in the specified range.
  76. extern PYWINTYPES_EXPORT BOOL PyWin_RegisterErrorMessageModule(DWORD first, DWORD last, HINSTANCE hmod);
  77. // Get the previously registered hmodule for an error code.
  78. extern PYWINTYPES_EXPORT HINSTANCE PyWin_GetErrorMessageModule(DWORD err);
  79.  
  80.  
  81. /* A global function that sets an API style error (ie, (code, fn, errTest)) */
  82. PYWINTYPES_EXPORT PyObject *PyWin_SetAPIError(char *fnName, long err = 0);
  83.  
  84. /* Basic COM Exception handling.  The main COM exception object
  85.    is actually defined here.  However, the most useful functions
  86.    for raising the exception are still in the COM package.  Therefore,
  87.    you can use the fn below to raise a basic COM exception - no fancy error
  88.    messages available, just the HRESULT.  It will, however, _be_ a COM
  89.    exception, and therefore trappable like any other COM exception
  90. */
  91. extern PYWINTYPES_EXPORT PyObject *PyWinExc_COMError;
  92. PYWINTYPES_EXPORT PyObject *PyWin_SetBasicCOMError(HRESULT hr);
  93.  
  94. /*
  95. ** String/UniCode support
  96. */
  97. #ifdef PYWIN_USE_PYUNICODE
  98.     /* Python has built-in Unicode String support */
  99. #define PyUnicodeType PyUnicode_Type
  100. // PyUnicode_Check is defined.
  101.  
  102. #else
  103.  
  104. /* If a Python Unicode object exists, disable it. */
  105. #ifdef PyUnicode_Check
  106. #undef PyUnicode_Check
  107. #define PyUnicode_Check(ob)    ((ob)->ob_type == &PyUnicodeType)
  108. #endif /* PyUnicode_Check */
  109.  
  110.     /* Need our custom Unicode object */
  111. extern PYWINTYPES_EXPORT PyTypeObject PyUnicodeType; // the Type for PyUnicode
  112. #define PyUnicode_Check(ob)    ((ob)->ob_type == &PyUnicodeType)
  113.  
  114.  
  115. // PyUnicode_AsUnicode clashes with the standard Python name - 
  116. // so if we are not using Python Unicode objects, we hide the
  117. // name with a #define.
  118. #define PyUnicode_AsUnicode(op) (((PyUnicode *)op)->m_bstrValue)
  119. //extern PYWINTYPES_EXPORT WCHAR *PyUnicode_AsUnicode(PyObject *op);
  120.  
  121. #endif /* PYWIN_USE_PYUNICODE */
  122.  
  123. extern PYWINTYPES_EXPORT int PyUnicode_Size(PyObject *op);
  124.  
  125. // Given a PyObject (string, Unicode, etc) create a "BSTR" with the value
  126. PYWINTYPES_EXPORT BOOL PyWinObject_AsBstr(PyObject *stringObject, BSTR *pResult, BOOL bNoneOK = FALSE, DWORD *pResultLen = NULL);
  127. // And free it when finished.
  128. PYWINTYPES_EXPORT void PyWinObject_FreeBstr(BSTR pResult);
  129.  
  130. PYWINTYPES_EXPORT PyObject *PyWinObject_FromBstr(const BSTR bstr, BOOL takeOwnership=FALSE);
  131.  
  132. // Convert a "char *" to a BSTR - free via ::SysFreeString()
  133. PYWINTYPES_EXPORT BSTR PyWin_String_AsBstr(const char *str);
  134.  
  135. // Given a string or Unicode object, get WCHAR characters.
  136. PYWINTYPES_EXPORT BOOL PyWinObject_AsWCHAR(PyObject *stringObject, WCHAR **pResult, BOOL bNoneOK = FALSE, DWORD *pResultLen = NULL);
  137. // And free it when finished.
  138. PYWINTYPES_EXPORT void PyWinObject_FreeWCHAR(WCHAR *pResult);
  139.  
  140. // As of Python 2.6, Python switched to 'wchar_t' for unicode, so old
  141. // win32 structures that still use 'unsigned short' now fail from C++ with
  142. // VS8 so we provide a couple of helpers.
  143. // XXX - but, when trying to use VC2003 with x64, the SDK x64 compiler
  144. // reports itself as 14.00.40310.41 - so this breaks under that compiler
  145. // Its not clear how to resolve this, but while VS2003 is the default
  146. // compiler, that is what must work.
  147. // py2.5 on x64 also needs it, and that is min x64 we support
  148. #if (PY_VERSION_HEX >= 0x02060000) || defined(_WIN64)
  149. inline BOOL PyWinObject_AsWCHAR(PyObject *stringObject, unsigned short **pResult, BOOL bNoneOK = FALSE, DWORD *pResultLen = NULL)
  150. {
  151.     return PyWinObject_AsWCHAR(stringObject, (WCHAR **)pResult, bNoneOK, pResultLen);
  152. }
  153. inline void PyWinObject_FreeWCHAR(unsigned short *pResult)
  154. {
  155.     PyWinObject_FreeWCHAR((WCHAR *)pResult);
  156. }
  157. #endif
  158.  
  159. // Given a PyObject (string, Unicode, etc) create a "char *" with the value
  160. // if pResultLen != NULL, it will be set to the result size NOT INCLUDING 
  161. // TERMINATOR (to be in line with SysStringLen, PyString_*, etc)
  162. PYWINTYPES_EXPORT BOOL PyWinObject_AsString(PyObject *stringObject, char **pResult, BOOL bNoneOK = FALSE, DWORD *pResultLen = NULL);
  163. // And free it when finished.
  164. PYWINTYPES_EXPORT void PyWinObject_FreeString(char *pResult);
  165. PYWINTYPES_EXPORT void PyWinObject_FreeString(WCHAR *pResult);
  166.  
  167. // Buffer functions that can be used in place of 's#' input format or PyString_AsStringAndSize
  168. // for 64-bit compatibility and API consistency
  169. PYWINTYPES_EXPORT BOOL PyWinObject_AsReadBuffer(PyObject *ob, void **buf, DWORD *buf_len, BOOL bNoneOk=FALSE);
  170. PYWINTYPES_EXPORT BOOL PyWinObject_AsWriteBuffer(PyObject *ob, void **buf, DWORD *buf_len, BOOL bNoneOk=FALSE);
  171.  
  172. // For 64-bit python compatibility, convert sequence to tuple and check length fits in a DWORD
  173. PYWINTYPES_EXPORT PyObject *PyWinSequence_Tuple(PyObject *obseq, DWORD *len);
  174.  
  175.  
  176. // an 'int' version (but aren't 'int' and 'DWORD' the same size?
  177. // Maybe a signed-ness issue?
  178. inline BOOL PyWinObject_AsReadBuffer(PyObject *ob, void **buf, int *buf_len, BOOL bNoneOk=FALSE)
  179. {
  180.     return PyWinObject_AsReadBuffer(ob, buf, (DWORD *)buf_len, bNoneOk);
  181. }
  182.  
  183. /* ANSI/Unicode Support */
  184. /* If UNICODE defined, will be a BSTR - otherwise a char *
  185.    Either way - PyWinObject_FreeTCHAR() must be called
  186. */
  187.  
  188. #ifdef UNICODE
  189. #define PyWinObject_AsTCHAR PyWinObject_AsWCHAR
  190. #define PyWinObject_FreeTCHAR PyWinObject_FreeWCHAR
  191. #define PyWinObject_FromTCHAR PyWinObject_FromOLECHAR
  192. #define PyString_FromTCHAR PyString_FromUnicode
  193. #else /* not UNICODE */
  194. #define PyWinObject_AsTCHAR PyWinObject_AsString
  195. #define PyWinObject_FreeTCHAR PyWinObject_FreeString
  196. inline PyObject *PyWinObject_FromTCHAR( TCHAR *str )
  197. {
  198.     if (str==NULL){
  199.         Py_INCREF(Py_None);
  200.         return Py_None;
  201.         }
  202.     return PyString_FromString(str);
  203. }
  204. inline PyObject *PyWinObject_FromTCHAR( TCHAR *str, int numChars )
  205. {
  206.     if (str==NULL){
  207.         Py_INCREF(Py_None);
  208.         return Py_None;
  209.         }
  210.     return PyString_FromStringAndSize(str, numChars);
  211. }
  212. #define PyString_FromTCHAR PyString_FromString
  213. #endif
  214.  
  215. #define PyWinObject_FromWCHAR PyWinObject_FromOLECHAR
  216.  
  217. // Converts a series of consecutive null terminated strings into a list
  218. PYWINTYPES_EXPORT PyObject *PyWinObject_FromMultipleString(WCHAR *multistring);
  219. PYWINTYPES_EXPORT PyObject *PyWinObject_FromMultipleString(char *multistring);
  220. // Converts a sequence of str/unicode objects into a series of consecutive null-terminated
  221. //    wide character strings with extra terminating null
  222. PYWINTYPES_EXPORT BOOL PyWinObject_AsMultipleString(PyObject *ob, WCHAR **pmultistring, BOOL bNoneOK=TRUE, DWORD *chars_returned=NULL);
  223. PYWINTYPES_EXPORT void PyWinObject_FreeMultipleString(WCHAR *pmultistring);
  224.  
  225. // Converts a sequence of str/unicode objects into a series of consecutive character strings
  226. //    terminated by double null
  227. PYWINTYPES_EXPORT BOOL PyWinObject_AsMultipleString(PyObject *ob, char **pmultistring, BOOL bNoneOK=TRUE, DWORD *chars_returned=NULL);
  228. PYWINTYPES_EXPORT void PyWinObject_FreeMultipleString(char *pmultistring);
  229.  
  230. // Convert a sequence of strings to an array of WCHAR pointers
  231. PYWINTYPES_EXPORT void PyWinObject_FreeWCHARArray(LPWSTR *wchars, DWORD str_cnt);
  232. PYWINTYPES_EXPORT BOOL PyWinObject_AsWCHARArray(PyObject *str_seq, LPWSTR **wchars, DWORD *str_cnt, BOOL bNoneOK = FALSE);
  233.  
  234. // Convert a sequence of string or unicode objects to an array of char *
  235. PYWINTYPES_EXPORT void PyWinObject_FreeCharArray(char **pchars, DWORD str_cnt);
  236. PYWINTYPES_EXPORT BOOL PyWinObject_AsCharArray(PyObject *str_seq, char ***pchars, DWORD *str_cnt, BOOL bNoneOK = FALSE);
  237.  
  238. PYWINTYPES_EXPORT PyObject *PyString_FromUnicode( const OLECHAR *str );
  239. PYWINTYPES_EXPORT PyObject *PyUnicodeObject_FromString(const char *string);
  240. PYWINTYPES_EXPORT PyObject *PyWinObject_FromOLECHAR(const OLECHAR * str);
  241. PYWINTYPES_EXPORT PyObject *PyWinObject_FromOLECHAR(const OLECHAR * str, int numChars);
  242.  
  243. // String support for buffers allocated via a function of your choice.
  244. PYWINTYPES_EXPORT BOOL PyWinObject_AsPfnAllocatedWCHAR(PyObject *stringObject, 
  245.                                                   void *(*pfnAllocator)(ULONG), 
  246.                                                   WCHAR **ppResult, 
  247.                                                   BOOL bNoneOK = FALSE,
  248.                                                   DWORD *pResultLen = NULL);
  249.  
  250. // String support for buffers allocated via CoTaskMemAlloc and CoTaskMemFree
  251. PYWINTYPES_EXPORT BOOL PyWinObject_AsTaskAllocatedWCHAR(PyObject *stringObject, WCHAR **ppResult, BOOL bNoneOK /*= FALSE*/,DWORD *pResultLen /*= NULL*/);
  252. PYWINTYPES_EXPORT void PyWinObject_FreeTaskAllocatedWCHAR(WCHAR * str);
  253. // String conversion - These must also be freed with PyWinObject_FreeString
  254. PYWINTYPES_EXPORT BOOL PyWin_WCHAR_AsString(WCHAR *input, DWORD inLen, char **pResult);
  255. PYWINTYPES_EXPORT BOOL PyWin_Bstr_AsString(BSTR input, char **pResult);
  256. PYWINTYPES_EXPORT BOOL PyWin_String_AsWCHAR(char *input, DWORD inLen, WCHAR **pResult);
  257.  
  258. PYWINTYPES_EXPORT void PyWinObject_FreeString(char *str);
  259. PYWINTYPES_EXPORT void PyWinObject_FreeString(WCHAR *str);
  260.  
  261. // Pointers.
  262. // Substitute for Python's inconsistent PyLong_AsVoidPtr
  263. PYWINTYPES_EXPORT BOOL PyWinLong_AsVoidPtr(PyObject *ob, void **pptr);
  264. PYWINTYPES_EXPORT PyObject *PyWinLong_FromVoidPtr(const void *ptr);
  265.  
  266. /*
  267. ** LARGE_INTEGER objects
  268. */
  269. // These need to be renamed.  For now, the old names still appear in the DLL.
  270. PYWINTYPES_EXPORT BOOL PyLong_AsTwoInts(PyObject *ob, int *hiint, unsigned *loint);
  271. PYWINTYPES_EXPORT PyObject *PyLong_FromTwoInts(int hidword, unsigned lodword);
  272.  
  273. // These seem (to MH anyway :) to be better names than using "int".
  274. inline BOOL PyLong_AsTwoI32(PyObject *ob, int *hiint, unsigned *loint) {return PyLong_AsTwoInts(ob, hiint, loint);}
  275. inline PyObject *PyLong_FromTwoI32(int hidword, unsigned lodword) {return PyLong_FromTwoInts(hidword, lodword);}
  276.  
  277. //AsLARGE_INTEGER takes either PyInteger, PyLong, (PyInteger, PyInteger)
  278. PYWINTYPES_EXPORT BOOL PyWinObject_AsLARGE_INTEGER(PyObject *ob, LARGE_INTEGER *pResult);
  279. PYWINTYPES_EXPORT BOOL PyWinObject_AsULARGE_INTEGER(PyObject *ob, ULARGE_INTEGER *pResult);
  280. PYWINTYPES_EXPORT PyObject *PyWinObject_FromLARGE_INTEGER(LARGE_INTEGER &val);
  281. PYWINTYPES_EXPORT PyObject *PyWinObject_FromULARGE_INTEGER(ULARGE_INTEGER &val);
  282. #define PyLong_FromLARGE_INTEGER PyWinObject_FromLARGE_INTEGER
  283. #define PyLong_FromULARGE_INTEGER PyWinObject_FromULARGE_INTEGER
  284. // Helpers that take a Py_LONG_LONG, but (a) have pywin32 consistent signatures
  285. // and (b) handle int *and* long (where Python only starts doing that in the
  286. // PyLong_* APIs post 2.4)
  287. // We also happen to know a LARGE_INTEGER is an __int64, so do it the easy way
  288. #define PyWinObject_AsPY_LONG_LONG(ob, pResult) PyWinObject_AsLARGE_INTEGER((ob), (LARGE_INTEGER *)(pResult))
  289. #define PyWinObject_AsUPY_LONG_LONG(ob, pResult) PyWinObject_AsULARGE_INTEGER((ob), (ULARGE_INTEGER *)(pResult))
  290. #define PyWinObject_FromPY_LONG_LONG(val) PyWinObject_FromLARGE_INTEGER((LARGE_INTEGER)val)
  291. #define PyWinObject_FromUPY_LONG_LONG(val) PyWinObject_FromULARGE_INTEGER((ULARGE_INTEGER)val)
  292.  
  293. PyObject *PyLong_FromI64(__int64 ival);
  294. BOOL PyLong_AsI64(PyObject *val, __int64 *lval);
  295.  
  296. // A DWORD_PTR and ULONG_PTR appear to mean "integer long enough to hold a pointer"
  297. // It is *not* actually a pointer (but is the same size as a pointer)
  298. inline PyObject *PyWinObject_FromULONG_PTR(ULONG_PTR v) {
  299.     return PyWinLong_FromVoidPtr((void *)v);
  300. }
  301. inline BOOL PyWinLong_AsULONG_PTR(PyObject *ob, ULONG_PTR *r) {
  302.     return PyWinLong_AsVoidPtr(ob, (void **)r);
  303. }
  304.  
  305. inline PyObject *PyWinObject_FromDWORD_PTR(DWORD_PTR v) {
  306.     return PyLong_FromVoidPtr((void *)v);
  307. }
  308. inline BOOL PyWinLong_AsDWORD_PTR(PyObject *ob, DWORD_PTR *r) {
  309.     return PyWinLong_AsVoidPtr(ob, (void **)r);
  310. }
  311.  
  312. // Some boolean helpers for Python 2.2 and earlier
  313. #if (PY_VERSION_HEX < 0x02030000 && !defined(PYWIN_NO_BOOL_FROM_LONG))
  314. // PyBool_FromLong only in 2.3 and later
  315. inline PyObject *PyBool_FromLong(long v)
  316. {
  317.     PyObject *ret= v ? Py_True : Py_False;
  318.     Py_INCREF(ret);
  319.     return ret;
  320. }
  321. #endif
  322.  
  323. /*
  324. ** OVERLAPPED Object and API
  325. */
  326. class PyOVERLAPPED; // forward declare
  327. extern PYWINTYPES_EXPORT PyTypeObject PyOVERLAPPEDType; // the Type for PyOVERLAPPED
  328. #define PyOVERLAPPED_Check(ob)    ((ob)->ob_type == &PyOVERLAPPEDType)
  329. PYWINTYPES_EXPORT BOOL PyWinObject_AsOVERLAPPED(PyObject *ob, OVERLAPPED **ppOverlapped, BOOL bNoneOK = TRUE);
  330. PYWINTYPES_EXPORT BOOL PyWinObject_AsPyOVERLAPPED(PyObject *ob, PyOVERLAPPED **ppOverlapped, BOOL bNoneOK = TRUE);
  331. PYWINTYPES_EXPORT PyObject *PyWinObject_FromOVERLAPPED(const OVERLAPPED *pOverlapped);
  332.  
  333. // A global function that can work as a module method for making an OVERLAPPED object.
  334. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewOVERLAPPED(PyObject *self, PyObject *args);
  335.  
  336. #ifndef NO_PYWINTYPES_IID
  337. /*
  338. ** IID/GUID support
  339. */
  340.  
  341. extern PYWINTYPES_EXPORT PyTypeObject PyIIDType;        // the Type for PyIID
  342. #define PyIID_Check(ob)        ((ob)->ob_type == &PyIIDType)
  343.  
  344. // Given an object repring a CLSID (either PyIID or string), fill the CLSID.
  345. PYWINTYPES_EXPORT BOOL PyWinObject_AsIID(PyObject *obCLSID, CLSID *clsid);
  346.  
  347. // return a native PyIID object representing an IID
  348. PYWINTYPES_EXPORT PyObject *PyWinObject_FromIID(const IID &riid);
  349.  
  350. // return a string/Unicode object representing an IID
  351. PYWINTYPES_EXPORT PyObject *PyWinStringObject_FromIID(const IID &riid);
  352. PYWINTYPES_EXPORT PyObject *PyWinUnicodeObject_FromIID(const IID &riid);
  353.  
  354. // A global function that can work as a module method for making an IID object.
  355. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewIID( PyObject *self, PyObject *args);
  356. #endif /*NO_PYWINTYPES_IID */
  357.  
  358. /*
  359. ** TIME support
  360. */
  361. PYWINTYPES_EXPORT PyObject *PyWinObject_FromSYSTEMTIME(const SYSTEMTIME &t);
  362. PYWINTYPES_EXPORT PyObject *PyWinObject_FromFILETIME(const FILETIME &t);
  363.  
  364. // Converts a TimeStamp, which is in 100 nanosecond units like a FILETIME
  365. // TimeStamp is actually defined as a LARGE_INTEGER, so this function will also
  366. // accept Windows security "TimeStamp" objects directly - however, we use a
  367. // LARGE_INTEGER prototype to avoid pulling in the windows security headers.
  368. PYWINTYPES_EXPORT PyObject *PyWinObject_FromTimeStamp(const LARGE_INTEGER &t);
  369.  
  370. PYWINTYPES_EXPORT BOOL PyWinObject_AsDATE(PyObject *ob, DATE *pDate);
  371. PYWINTYPES_EXPORT BOOL PyWinObject_AsFILETIME(PyObject *ob,    FILETIME *pDate);
  372. PYWINTYPES_EXPORT BOOL PyWinObject_AsSYSTEMTIME(PyObject *ob, SYSTEMTIME *pDate);
  373.  
  374. #ifndef NO_PYWINTYPES_TIME
  375.  
  376. extern PYWINTYPES_EXPORT PyTypeObject PyTimeType;        // the Type for PyTime
  377. #define PyTime_Check(ob)        ((ob)->ob_type == &PyTimeType)
  378.  
  379. PYWINTYPES_EXPORT PyObject *PyWinObject_FromDATE(DATE t);
  380. PYWINTYPES_EXPORT PyObject *PyWinTimeObject_FromLong(long t);
  381.  
  382. // A global function that can work as a module method for making a time object.
  383. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewTime( PyObject *self, PyObject *args);
  384.  
  385. #endif // NO_PYWINTYPES_TIME
  386.  
  387. // Convert a time object to a time_t value.
  388. PYWINTYPES_EXPORT BOOL PyWinObject_Astime_t(PyObject *ob, time_t *t);
  389.  
  390. // functions to return WIN32_FIND_DATA tuples, used in shell, win32api, and win32file
  391. PYWINTYPES_EXPORT PyObject *PyObject_FromWIN32_FIND_DATAA(WIN32_FIND_DATAA *pData);
  392. PYWINTYPES_EXPORT PyObject *PyObject_FromWIN32_FIND_DATAW(WIN32_FIND_DATAW *pData);
  393. #ifdef UNICODE
  394. #define PyObject_FromWIN32_FIND_DATA PyObject_FromWIN32_FIND_DATAW
  395. #else
  396. #define PyObject_FromWIN32_FIND_DATA PyObject_FromWIN32_FIND_DATAA
  397. #endif
  398.  
  399. // POINT tuple, used in win32api_display.cpp and win32gui.i
  400. PYWINTYPES_EXPORT BOOL PyWinObject_AsPOINT(PyObject *obpoint, LPPOINT ppoint);
  401.  
  402. // IO_COUNTERS dict, used in win32process and win32job
  403. PYWINTYPES_EXPORT PyObject *PyWinObject_FromIO_COUNTERS(PIO_COUNTERS pioc);
  404.  
  405. // Make an array of DWORD's from a sequence of Python ints
  406. PYWINTYPES_EXPORT BOOL PyWinObject_AsDWORDArray(PyObject *obdwords, DWORD **pdwords, DWORD *item_cnt, BOOL bNoneOk=TRUE);
  407.  
  408. // Conversion for resource id/name and class atom
  409. PYWINTYPES_EXPORT BOOL PyWinObject_AsResourceIdA(PyObject *ob, char **presource_id, BOOL bNoneOK = FALSE);
  410. PYWINTYPES_EXPORT BOOL PyWinObject_AsResourceIdW(PyObject *ob, WCHAR **presource_id, BOOL bNoneOK = FALSE);
  411. PYWINTYPES_EXPORT void PyWinObject_FreeResourceId(char *resource_id);
  412. PYWINTYPES_EXPORT void PyWinObject_FreeResourceId(WCHAR *resource_id);
  413. #ifdef UNICODE
  414. #define PyWinObject_AsResourceId PyWinObject_AsResourceIdW
  415. #else
  416. #define PyWinObject_AsResourceId PyWinObject_AsResourceIdA
  417. #endif
  418.  
  419. // WPARAM and LPARAM conversion
  420. PYWINTYPES_EXPORT BOOL PyWinObject_AsPARAM(PyObject *ob, WPARAM *pparam);
  421. inline PyObject *PyWinObject_FromPARAM(WPARAM param) {
  422.     return PyWinObject_FromULONG_PTR(param);
  423. }
  424. inline BOOL PyWinObject_AsPARAM(PyObject *ob, LPARAM *pparam) {
  425.     return PyWinObject_AsPARAM(ob, (WPARAM *)pparam);
  426. }
  427. inline PyObject *PyWinObject_FromPARAM(LPARAM param) {
  428.     return PyWinObject_FromULONG_PTR(param);
  429. }
  430.  
  431.  
  432. // RECT conversions
  433. // @object PyRECT|Tuple of 4 ints defining a rectangle: (left, top, right, bottom)
  434. PYWINTYPES_EXPORT BOOL PyWinObject_AsRECT(PyObject *obrect, LPRECT prect);
  435. PYWINTYPES_EXPORT PyObject *PyWinObject_FromRECT(LPRECT prect);
  436.  
  437. /*
  438. ** SECURITY_ATTRIBUTES support
  439. */
  440. extern PYWINTYPES_EXPORT PyTypeObject PySECURITY_ATTRIBUTESType;
  441. #define PySECURITY_ATTRIBUTES_Check(ob)        ((ob)->ob_type == &PySECURITY_ATTRIBUTESType)
  442. extern PYWINTYPES_EXPORT PyTypeObject PyDEVMODEType;
  443. extern PYWINTYPES_EXPORT PyTypeObject PyDEVMODEWType;
  444.  
  445. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewSECURITY_ATTRIBUTES(PyObject *self, PyObject *args);
  446. PYWINTYPES_EXPORT BOOL PyWinObject_AsSECURITY_ATTRIBUTES(PyObject *ob, SECURITY_ATTRIBUTES **ppSECURITY_ATTRIBUTES, BOOL bNoneOK = TRUE);
  447. PYWINTYPES_EXPORT PyObject *PyWinObject_FromSECURITY_ATTRIBUTES(const SECURITY_ATTRIBUTES &sa);
  448. PYWINTYPES_EXPORT BOOL PyWinObject_AsDEVMODE(PyObject *ob, PDEVMODEA * ppDEVMODE, BOOL bNoneOK = TRUE);
  449. PYWINTYPES_EXPORT BOOL PyWinObject_AsDEVMODE(PyObject *ob, PDEVMODEW * ppDEVMODE, BOOL bNoneOK);
  450. PYWINTYPES_EXPORT PyObject *PyWinObject_FromDEVMODE(PDEVMODEA);
  451. PYWINTYPES_EXPORT PyObject *PyWinObject_FromDEVMODE(PDEVMODEW);
  452.  
  453. /*
  454. ** WAVEFORMATEX support
  455. */
  456.  
  457. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewWAVEFORMATEX(PyObject *self, PyObject *args);
  458. PYWINTYPES_EXPORT PyObject *PyWinObject_FromWAVEFROMATEX(const WAVEFORMATEX &wfx);
  459. PYWINTYPES_EXPORT BOOL PyWinObject_AsWAVEFORMATEX(PyObject *ob, WAVEFORMATEX **ppWAVEFORMATEX, BOOL bNoneOK = TRUE);
  460. extern PYWINTYPES_EXPORT PyTypeObject PyWAVEFORMATEXType;
  461. #define PyWAVEFORMATEX_Check(ob)        ((ob)->ob_type == &PyWAVEFORMATEXType)
  462.  
  463.  
  464. /*
  465. ** SECURITY_DESCRIPTOR support
  466. */
  467. extern PYWINTYPES_EXPORT PyTypeObject PySECURITY_DESCRIPTORType;
  468. #define PySECURITY_DESCRIPTOR_Check(ob)        ((ob)->ob_type == &PySECURITY_DESCRIPTORType)
  469.  
  470. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewSECURITY_DESCRIPTOR(PyObject *self, PyObject *args);
  471. PYWINTYPES_EXPORT BOOL PyWinObject_AsSECURITY_DESCRIPTOR(PyObject *ob, PSECURITY_DESCRIPTOR *ppSECURITY_DESCRIPTOR, BOOL bNoneOK = TRUE);
  472. PYWINTYPES_EXPORT PyObject *PyWinObject_FromSECURITY_DESCRIPTOR(PSECURITY_DESCRIPTOR psd);
  473.  
  474. PYWINTYPES_EXPORT BOOL _MakeAbsoluteSD(PSECURITY_DESCRIPTOR psd_relative, PSECURITY_DESCRIPTOR *ppsd_absolute);
  475. PYWINTYPES_EXPORT void FreeAbsoluteSD(PSECURITY_DESCRIPTOR psd);
  476.  
  477. /*
  478. ** SID support
  479. */
  480. extern PYWINTYPES_EXPORT PyTypeObject PySIDType;
  481. #define PySID_Check(ob)        ((ob)->ob_type == &PySIDType)
  482.  
  483. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewSID(PyObject *self, PyObject *args);
  484. PYWINTYPES_EXPORT BOOL PyWinObject_AsSID(PyObject *ob, PSID *ppSID, BOOL bNoneOK = FALSE);
  485. PYWINTYPES_EXPORT PyObject *PyWinObject_FromSID(PSID pSID);
  486.  
  487. /*
  488. ** ACL support
  489. */
  490. extern PYWINTYPES_EXPORT PyTypeObject PyACLType;
  491. #define PyACL_Check(ob)        ((ob)->ob_type == &PyACLType)
  492.  
  493. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewACL(PyObject *self, PyObject *args);
  494. PYWINTYPES_EXPORT BOOL PyWinObject_AsACL(PyObject *ob, PACL *ppACL, BOOL bNoneOK = FALSE);
  495.  
  496. /*
  497. ** Win32 HANDLE wrapper - any handle closable by "CloseHandle()"
  498. */
  499. extern PYWINTYPES_EXPORT PyTypeObject PyHANDLEType; // the Type for PyHANDLE
  500. #define PyHANDLE_Check(ob)    ((ob)->ob_type == &PyHANDLEType)
  501.  
  502. // Convert an object to a HANDLE - None is always OK, as are ints, etc.
  503. PYWINTYPES_EXPORT BOOL PyWinObject_AsHANDLE(PyObject *ob, HANDLE *pRes);
  504. // For handles that use PyHANDLE.
  505. PYWINTYPES_EXPORT PyObject *PyWinObject_FromHANDLE(HANDLE h);
  506. // For handles that aren't returned as PyHANDLE or a subclass thereof (HDC, HWND, etc).
  507. // Return as python ints or longs 
  508. PYWINTYPES_EXPORT PyObject *PyWinLong_FromHANDLE(HANDLE h);
  509.  
  510. // A global function that can work as a module method for making a HANDLE object.
  511. PYWINTYPES_EXPORT PyObject *PyWinMethod_NewHANDLE( PyObject *self, PyObject *args);
  512.  
  513. // A global function that does the right thing wrt closing a "handle".
  514. // The object can be either a PyHANDLE or an integer.
  515. // If result is FALSE, a Python error is all setup (cf PyHANDLE::Close(), which doesnt set the Python error)
  516. PYWINTYPES_EXPORT BOOL PyWinObject_CloseHANDLE(PyObject *obHandle);
  517.  
  518. PYWINTYPES_EXPORT BOOL PyWinObject_AsHKEY(PyObject *ob, HKEY *pRes);
  519. PYWINTYPES_EXPORT PyObject *PyWinObject_FromHKEY(HKEY h);
  520. PYWINTYPES_EXPORT BOOL PyWinObject_CloseHKEY(PyObject *obHandle);
  521.  
  522. // MSG structure keeps coming up...
  523. PYWINTYPES_EXPORT BOOL PyWinObject_AsMSG(PyObject *ob, MSG *pMsg);
  524. PYWINTYPES_EXPORT PyObject *PyWinObject_FromMSG(const MSG *pMsg);
  525.  
  526. #include "winsock.h"
  527. /*
  528. ** SOCKET support.
  529. */
  530. PYWINTYPES_EXPORT
  531. BOOL PySocket_AsSOCKET
  532. //-------------------------------------------------------------------------
  533. // Helper function for dealing with socket arguments.
  534. (
  535.     PyObject *obSocket,
  536.     // [in] Python object being converted into a SOCKET handle.
  537.     SOCKET *ps
  538.     // [out] Returned socket handle
  539. );
  540.  
  541.  
  542.  
  543. /*
  544. ** Other Utilities
  545. */
  546. // ----------------------------------------------------------------------
  547. // WARNING - NEVER EVER USE new() ON THIS CLASS
  548. // This class can be used as a local variable, typically in a Python/C
  549. // function, and can be passed whereever a TCHAR/WCHAR is expected.
  550. // Typical Usage:
  551. // PyWin_AutoFreeBstr arg;
  552. // PyArg_ParseTuple("O", &obStr);
  553. // PyWinObject_AsAutoFreeBstr(obStr, &arg);
  554. // CallTheFunction(arg); // Will correctly pass BSTR/OLECHAR
  555. // -- when the function goes out of scope, the string owned by "arg" will
  556. // -- automatically be freed.
  557. // ----------------------------------------------------------------------
  558. class PYWINTYPES_EXPORT PyWin_AutoFreeBstr {
  559. public:
  560.     PyWin_AutoFreeBstr( BSTR bstr = NULL );
  561.     ~PyWin_AutoFreeBstr();
  562.     void SetBstr( BSTR bstr );
  563.     operator BSTR() {return m_bstr;}
  564. private:
  565.     BSTR m_bstr;
  566. };
  567.  
  568. inline BOOL PyWinObject_AsAutoFreeBstr(PyObject *stringObject, PyWin_AutoFreeBstr *pResult, BOOL bNoneOK = FALSE)
  569. {
  570.     if (bNoneOK && stringObject == Py_None) {
  571.         pResult->SetBstr(NULL);
  572.         return TRUE;
  573.     }
  574.     BSTR bs;
  575.     if (!PyWinObject_AsBstr(stringObject, &bs, bNoneOK))
  576.         return FALSE;
  577.     pResult->SetBstr(bs);
  578.     return TRUE;
  579. }
  580.  
  581. // ----------------------------------------------------------------------
  582. //
  583. // THREAD MANAGEMENT
  584. //
  585.  
  586. // ### need to rename the PYCOM_ stuff soon...
  587.  
  588. // We have 2 discrete locks in use (when no free-threaded is used, anyway).
  589. // The first type of lock is the global Python lock.  This is the standard lock
  590. // in use by Python, and must be used as documented by Python.  Specifically, no
  591. // 2 threads may _ever_ call _any_ Python code (including INCREF/DECREF) without
  592. // first having this thread lock.
  593. //
  594. // The second type of lock is a "global framework lock".  This lock is simply a 
  595. // critical section, and used whenever 2 threads of C code need access to global
  596. // data.  This is different than the Python lock - this lock is used when no Python
  597. // code can ever be called by the threads, but the C code still needs thread-safety.
  598.  
  599. // We also supply helper classes which make the usage of these locks a one-liner.
  600.  
  601. // The "framework" lock, implemented as a critical section.
  602. PYWINTYPES_EXPORT void PyWin_AcquireGlobalLock(void);
  603. PYWINTYPES_EXPORT void PyWin_ReleaseGlobalLock(void);
  604.  
  605. // Helper class for the DLL global lock.
  606. //
  607. // This class magically waits for the Win32/COM framework global lock, and releases it
  608. // when finished.  
  609. // NEVER new one of these objects - only use on the stack!
  610. class CEnterLeaveFramework {
  611. public:
  612.     CEnterLeaveFramework() {PyWin_AcquireGlobalLock();}
  613.     ~CEnterLeaveFramework() {PyWin_ReleaseGlobalLock();}
  614. };
  615.  
  616. // Python thread-lock stuff.  Free-threading patches use different semantics, but
  617. // these are abstracted away here...
  618. #ifndef FORCE_NO_FREE_THREAD
  619. # ifdef WITH_FREE_THREAD
  620. #  define PYCOM_USE_FREE_THREAD
  621. # endif
  622. #endif
  623. #ifdef PYCOM_USE_FREE_THREAD
  624. # include <threadstate.h>
  625. #else
  626. # include <pystate.h>
  627. #endif
  628.  
  629.  
  630. // Helper class for Enter/Leave Python
  631. //
  632. // This class magically waits for the Python global lock, and releases it
  633. // when finished.  
  634.  
  635. // Nested invocations will deadlock, so be careful.
  636.  
  637. // NEVER new one of these objects - only use on the stack!
  638. #ifndef PYCOM_USE_FREE_THREAD
  639. extern PYWINTYPES_EXPORT PyInterpreterState *PyWin_InterpreterState;
  640. extern PYWINTYPES_EXPORT BOOL PyWinThreadState_Ensure();
  641. extern PYWINTYPES_EXPORT void PyWinThreadState_Free();
  642. extern PYWINTYPES_EXPORT void PyWinThreadState_Clear();
  643. extern PYWINTYPES_EXPORT void PyWinInterpreterLock_Acquire();
  644. extern PYWINTYPES_EXPORT void PyWinInterpreterLock_Release();
  645.  
  646. extern PYWINTYPES_EXPORT void PyWinGlobals_Ensure();
  647. extern PYWINTYPES_EXPORT void PyWinGlobals_Free();
  648. #else
  649. #define PyWinThreadState_Ensure PyThreadState_Ensure
  650. #define PyWinThreadState_Free PyThreadState_Free
  651. #define PyWinThreadState_Clear PyThreadState_ClearExc
  652.  
  653. #endif
  654.  
  655. extern PYWINTYPES_EXPORT void PyWin_MakePendingCalls();
  656.  
  657. // For 2.3, use the PyGILState_ calls
  658. #if (PY_VERSION_HEX >= 0x02030000)
  659. #define PYWIN_USE_GILSTATE
  660. #endif
  661.  
  662. #ifndef PYWIN_USE_GILSTATE
  663.  
  664. class CEnterLeavePython {
  665. public:
  666.     CEnterLeavePython() {
  667.         acquired = FALSE;
  668.         acquire();
  669.     }
  670.     void acquire() {
  671.         if (acquired)
  672.             return;
  673.         created = PyWinThreadState_Ensure();
  674. #ifndef PYCOM_USE_FREE_THREAD
  675.         PyWinInterpreterLock_Acquire();
  676. #endif
  677.         if (created) {
  678.             // If pending python calls are waiting as we enter Python,
  679.             // it will generally mean an asynch signal handler, etc.
  680.             // We can either call it here, or wait for Python to call it
  681.             // as part of its "every 'n' opcodes" check.  If we wait for
  682.             // Python to check it and the pending call raises an exception,
  683.             // then it is _our_ code that will fail - this is unfair,
  684.             // as the signal was raised before we were entered - indeed,
  685.             // we may be directly responding to the signal!
  686.             // Thus, we flush all the pending calls here, and report any
  687.             // exceptions via our normal exception reporting mechanism.
  688.             // (of which we don't have, but not to worry... :)
  689.             // We can then execute our code in the knowledge that only
  690.             // signals raised _while_ we are executing will cause exceptions.
  691.             PyWin_MakePendingCalls();
  692.         }
  693.         acquired = TRUE;
  694.     }
  695.     ~CEnterLeavePython() {
  696.         if (acquired)
  697.             release();
  698.     }
  699.     void release() {
  700.     // The interpreter state must be cleared
  701.     // _before_ we release the lock, as some of
  702.     // the sys. attributes cleared (eg, the current exception)
  703.     // may need the lock to invoke their destructors - 
  704.     // specifically, when exc_value is a class instance, and
  705.     // the exception holds the last reference!
  706.         if ( !acquired )
  707.             return;
  708.         if ( created )
  709.             PyWinThreadState_Clear();
  710. #ifndef PYCOM_USE_FREE_THREAD
  711.         PyWinInterpreterLock_Release();
  712. #endif
  713.         if ( created )
  714.             PyWinThreadState_Free();
  715.         acquired = FALSE;
  716.     }
  717. private:
  718.     BOOL created;
  719.     BOOL acquired;
  720. };
  721.  
  722. #else // PYWIN_USE_GILSTATE
  723.  
  724. class CEnterLeavePython {
  725. public:
  726.     CEnterLeavePython() {
  727.         acquire();
  728.     }
  729.     void acquire(void) {
  730.         state = PyGILState_Ensure();
  731.         released = FALSE;
  732.     }
  733.     ~CEnterLeavePython() {
  734.         release();
  735.     }
  736.     void release(void) {
  737.         if (!released) {
  738.             PyGILState_Release(state);
  739.             released = TRUE;
  740.         }
  741.     }
  742. private:
  743.     PyGILState_STATE state;
  744.     BOOL released;
  745. };
  746. #endif // PYWIN_USE_GILSTATE
  747.  
  748. // A helper for simple exception handling.
  749. // try/__try
  750. #ifdef MAINWIN
  751. #define PYWINTYPES_TRY try
  752. #else
  753. #define PYWINTYPES_TRY __try
  754. #endif /* MAINWIN */
  755.  
  756. // catch/__except
  757. #if defined(__MINGW32__) || defined(MAINWIN)
  758. #define PYWINTYPES_EXCEPT catch(...)
  759. #else
  760. #define PYWINTYPES_EXCEPT __except( EXCEPTION_EXECUTE_HANDLER )
  761. #endif
  762. // End of exception helper macros.
  763.  
  764. #endif // __PYWINTYPES_H__
  765.  
  766.  
  767.