// Conversion for WPARAM and LPARAM // (WPARAM is defined as UINT_PTR, and LPARAM is defined as LONG_PTR - see // pywintypes.h for inline functions to resolve this) BOOL PyWinObject_AsPARAM(PyObject *ob, WPARAM *pparam) { assert(!PyErr_Occurred()); // lingering exception? if (ob==NULL || ob==Py_None){ *pparam=NULL; return TRUE; } // XXX - why this UNICODE block? Can't we just do both anyway? Maybe // just via the buffer interface? #ifdef UNICODE #define TCHAR_DESC "Unicode" if (PyUnicode_Check(ob)){ *pparam = (WPARAM)PyUnicode_AS_UNICODE(ob); return TRUE; } #else #define TCHAR_DESC "String" if (PyString_Check(ob)){ *pparam = (WPARAM)PyString_AS_STRING(ob); return TRUE; } #endif DWORD bufsize; if (PyWinObject_AsReadBuffer(ob, (VOID **)pparam, &bufsize)) return TRUE; PyErr_Clear(); if (PyWinLong_AsVoidPtr(ob, (void **)pparam)) return TRUE; PyErr_Format(PyExc_TypeError, "WPARAM must be a " TCHAR_DESC ", int, or buffer object (got %s)", ob->ob_type->tp_name); return FALSE; }
// @object PyResourceId|Identifies a resource or function in a module. // This can be a WORD-sized integer value (0-65536), or string/unicode // depending on whether the *A or *W API function is to be called. // Class atoms as used with <om win32gui.CreateWindow> are also treated // as resource ids since they can also be represented by a name or WORD id. // When passing resource names and types as strings, they are usually formatted // as a pound sign followed by decimal form of the id. ('#42' for example) BOOL PyWinObject_AsResourceIdA(PyObject *ob, char **presource_id, BOOL bNoneOK) { // Plain character conversion if (PyWinObject_AsString(ob, presource_id, bNoneOK)) return TRUE; PyErr_Clear(); if (PyWinLong_AsVoidPtr(ob, (void **)presource_id) && IS_INTRESOURCE(*presource_id)) return TRUE; *presource_id=NULL; PyErr_SetString(PyExc_TypeError, "Resource id/name must be string or int in the range 0-65536"); return FALSE; }
BOOL PyWinObject_AsResourceIdW(PyObject *ob, WCHAR **presource_id, BOOL bNoneOK) { // Unicode version of above if (PyWinObject_AsWCHAR(ob, presource_id, bNoneOK)) return TRUE; PyErr_Clear(); if (PyWinLong_AsVoidPtr(ob, (void **)presource_id) && IS_INTRESOURCE(*presource_id)) return TRUE; *presource_id=NULL; PyErr_SetString(PyExc_TypeError, "Resource id/name must be unicode or int in the range 0-65536"); return FALSE; }
// @pymethod boolean|timer|kill_timer|Creates a timer that executes a callback function // @comm Uses the KillTimer API function. static PyObject * py_timer_kill_timer (PyObject * self, PyObject * args) { PyObject * py_timer_id; UINT_PTR timer_id; if (!PyArg_ParseTuple (args, "O:kill_timer", &py_timer_id)) // @pyparm int|IDEvent||Timer id as returned by <om timer.set_timer> return NULL; if (!PyWinLong_AsVoidPtr(py_timer_id, (void **)&timer_id)) return NULL; if (timer_id_callback_map) if (0 != PyDict_DelItem (timer_id_callback_map, py_timer_id)) return NULL; BOOL rc; Py_BEGIN_ALLOW_THREADS; rc = ::KillTimer (NULL, timer_id); Py_END_ALLOW_THREADS; return PyBool_FromLong(rc); }
BOOL PySocket_AsSOCKET //------------------------------------------------------------------------- // Helper function for dealing with socket arguments. ( PyObject *obSocket, // [in] Python object being converted into a SOCKET handle. SOCKET *ps // [out] Returned socket handle ) { PyObject *o = NULL; PyObject *out = NULL; // Most common case, a python socket object (which apparently has no public C API) o = PyObject_GetAttrString(obSocket, "fileno"); if (o == NULL){ // Not a socket object, attempt direct conversion to integer handle PyErr_Clear(); out=obSocket; Py_INCREF(out); } else if (PyCallable_Check(o)){ // Normal socket object, whose fileno() method returns the integer handle out = PyObject_CallObject(o, NULL); Py_DECREF(o); if (out==NULL) return FALSE; } else // ??? fileno may be a number, rather than a method that returns a number ??? out=o; BOOL bsuccess=PyWinLong_AsVoidPtr(out, (void **)ps); Py_DECREF(out); if (!bsuccess){ PyErr_Clear(); PyErr_SetString(PyExc_TypeError, "Expected a socket object or numeric socket handle"); } return bsuccess; }