[pywin32-checkins] pywin32/win32/src win32pdhmodule.cpp,1.17,1.18
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Mark H. <mha...@us...> - 2008-12-03 22:16:45
|
Update of /cvsroot/pywin32/pywin32/win32/src In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv29538/win32/src Modified Files: win32pdhmodule.cpp Log Message: Merge unicode and 64bit fixes from py3k branch Index: win32pdhmodule.cpp =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/src/win32pdhmodule.cpp,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** win32pdhmodule.cpp 25 May 2008 00:44:43 -0000 1.17 --- win32pdhmodule.cpp 3 Dec 2008 21:34:22 -0000 1.18 *************** *** 36,40 **** // Function pointer typedefs typedef PDH_STATUS (WINAPI * FuncPdhEnumObjects)( ! LPCTSTR szReserved, // reserved LPCTSTR szMachineName, // machine name LPTSTR mszObjectList, // buffer for objects --- 36,40 ---- // Function pointer typedefs typedef PDH_STATUS (WINAPI * FuncPdhEnumObjects)( ! LPCTSTR szReserved, // DataSource LPCTSTR szMachineName, // machine name LPTSTR mszObjectList, // buffer for objects *************** *** 45,49 **** typedef PDH_STATUS (WINAPI * FuncPdhEnumObjectItems)( ! LPCTSTR szReserved, // reserved LPCTSTR szMachineName, // machine name LPCTSTR szObjectName, // object name --- 45,49 ---- typedef PDH_STATUS (WINAPI * FuncPdhEnumObjectItems)( ! LPCTSTR szReserved, // DataSource LPCTSTR szMachineName, // machine name LPCTSTR szObjectName, // object name *************** *** 57,62 **** typedef PDH_STATUS (WINAPI * FuncPdhOpenQuery)( ! LPVOID pReserved, // reserved ! DWORD dwUserData, // a value associated with this query HQUERY *phQuery // pointer to a buffer that will receive the query handle ) ; --- 57,62 ---- typedef PDH_STATUS (WINAPI * FuncPdhOpenQuery)( ! LPCTSTR szDataSource, // DataSource ! DWORD_PTR dwUserData, // a value associated with this query HQUERY *phQuery // pointer to a buffer that will receive the query handle ) ; *************** *** 73,77 **** HQUERY hQuery, // handle to the query LPCTSTR szFullCounterPath, // path of the counter ! DWORD dwUserData, // user-defined value HCOUNTER *phCounter // pointer to the counter handle buffer ); --- 73,77 ---- HQUERY hQuery, // handle to the query LPCTSTR szFullCounterPath, // path of the counter ! DWORD_PTR dwUserData, // user-defined value HCOUNTER *phCounter // pointer to the counter handle buffer ); *************** *** 108,112 **** typedef PDH_STATUS (WINAPI * FuncPdhExpandCounterPath)( LPCTSTR szWildCardPath, // counter path to expand ! LPSTR mszExpandedPathList, // names that match LPDWORD pcchPathListLength // size of buffer ); --- 108,112 ---- typedef PDH_STATUS (WINAPI * FuncPdhExpandCounterPath)( LPCTSTR szWildCardPath, // counter path to expand ! LPTSTR mszExpandedPathList, // names that match LPDWORD pcchPathListLength // size of buffer ); *************** *** 125,132 **** typedef PDH_STATUS (WINAPI *FuncPdhParseInstanceName) ( ! LPCSTR szInstanceString, ! LPSTR szInstanceName, LPDWORD pcchInstanceNameLength, ! LPSTR szParentName, LPDWORD pcchParentNameLength, LPDWORD lpIndex --- 125,132 ---- typedef PDH_STATUS (WINAPI *FuncPdhParseInstanceName) ( ! LPCTSTR szInstanceString, ! LPTSTR szInstanceName, LPDWORD pcchInstanceNameLength, ! LPTSTR szParentName, LPDWORD pcchParentNameLength, LPDWORD lpIndex *************** *** 134,138 **** typedef PDH_STATUS (WINAPI *FuncPdhBrowseCounters) ( ! PPDH_BROWSE_DLG_CONFIG_A pBrowseDlgData ); --- 134,138 ---- typedef PDH_STATUS (WINAPI *FuncPdhBrowseCounters) ( ! PPDH_BROWSE_DLG_CONFIG pBrowseDlgData ); *************** *** 184,213 **** static PyObject *win32pdh_counter_error; BOOL LoadPointers() { ! HMODULE handle = LoadLibrary("pdh.dll"); if (handle==NULL) { // PyErr_SetString(PyExc_RuntimeError, "The PDH DLL could not be located"); return FALSE; } ! pPdhEnumObjects = (FuncPdhEnumObjects)GetProcAddress(handle, "PdhEnumObjectsA"); ! pPdhEnumObjectItems = (FuncPdhEnumObjectItems)GetProcAddress(handle, "PdhEnumObjectItemsA"); pPdhCloseQuery = (FuncPdhCloseQuery)GetProcAddress(handle, "PdhCloseQuery"); pPdhRemoveCounter = (FuncPdhRemoveCounter)GetProcAddress(handle, "PdhRemoveCounter"); ! pPdhOpenQuery = (FuncPdhOpenQuery)GetProcAddress(handle, "PdhOpenQuery"); ! pPdhAddCounter = (FuncPdhAddCounter)GetProcAddress(handle, "PdhAddCounterA"); ! pPdhMakeCounterPath = (FuncPdhMakeCounterPath)GetProcAddress(handle, "PdhMakeCounterPathA"); ! pPdhGetCounterInfo = (FuncPdhGetCounterInfo)GetProcAddress(handle, "PdhGetCounterInfoA"); pPdhGetFormattedCounterValue = (FuncPdhGetFormattedCounterValue)GetProcAddress(handle, "PdhGetFormattedCounterValue"); pPdhCollectQueryData = (FuncPdhCollectQueryData)GetProcAddress(handle, "PdhCollectQueryData"); ! pPdhValidatePath = (FuncPdhValidatePath)GetProcAddress(handle, "PdhValidatePathA"); ! pPdhExpandCounterPath = (FuncPdhExpandCounterPath)GetProcAddress(handle, "PdhExpandCounterPathA"); ! pPdhParseCounterPath = (FuncPdhParseCounterPath)GetProcAddress(handle, "PdhParseCounterPathA"); pPdhSetCounterScaleFactor = (FuncPdhSetCounterScaleFactor)GetProcAddress(handle, "PdhSetCounterScaleFactor"); ! pPdhParseInstanceName = (FuncPdhParseInstanceName)GetProcAddress(handle, "PdhParseInstanceNameA"); ! pPdhBrowseCounters = (FuncPdhBrowseCounters)GetProcAddress(handle, "PdhBrowseCountersA"); ! pPdhConnectMachine = (FuncPdhConnectMachine)GetProcAddress(handle, "PdhConnectMachineA"); ! pPdhLookupPerfNameByIndex = (FuncPdhLookupPerfNameByIndex)GetProcAddress(handle, "PdhLookupPerfNameByIndexA"); ! pPdhLookupPerfIndexByName = (FuncPdhLookupPerfIndexByName)GetProcAddress(handle, "PdhLookupPerfIndexByName"); // Pdh error codes are in 2 different ranges --- 184,220 ---- static PyObject *win32pdh_counter_error; + // Select whether to load ansi or unicode API functions + #ifdef UNICODE + #define A_OR_W "W" + #else + #define A_OR_W "A" + #endif + BOOL LoadPointers() { ! HMODULE handle = LoadLibrary(_T("pdh.dll")); if (handle==NULL) { // PyErr_SetString(PyExc_RuntimeError, "The PDH DLL could not be located"); return FALSE; } ! pPdhEnumObjects = (FuncPdhEnumObjects)GetProcAddress(handle, "PdhEnumObjects" A_OR_W); ! pPdhEnumObjectItems = (FuncPdhEnumObjectItems)GetProcAddress(handle, "PdhEnumObjectItems" A_OR_W); pPdhCloseQuery = (FuncPdhCloseQuery)GetProcAddress(handle, "PdhCloseQuery"); pPdhRemoveCounter = (FuncPdhRemoveCounter)GetProcAddress(handle, "PdhRemoveCounter"); ! pPdhOpenQuery = (FuncPdhOpenQuery)GetProcAddress(handle, "PdhOpenQuery" A_OR_W); ! pPdhAddCounter = (FuncPdhAddCounter)GetProcAddress(handle, "PdhAddCounter" A_OR_W); ! pPdhMakeCounterPath = (FuncPdhMakeCounterPath)GetProcAddress(handle, "PdhMakeCounterPath" A_OR_W); ! pPdhGetCounterInfo = (FuncPdhGetCounterInfo)GetProcAddress(handle, "PdhGetCounterInfo" A_OR_W); pPdhGetFormattedCounterValue = (FuncPdhGetFormattedCounterValue)GetProcAddress(handle, "PdhGetFormattedCounterValue"); pPdhCollectQueryData = (FuncPdhCollectQueryData)GetProcAddress(handle, "PdhCollectQueryData"); ! pPdhValidatePath = (FuncPdhValidatePath)GetProcAddress(handle, "PdhValidatePath" A_OR_W); ! pPdhExpandCounterPath = (FuncPdhExpandCounterPath)GetProcAddress(handle, "PdhExpandCounterPath" A_OR_W); ! pPdhParseCounterPath = (FuncPdhParseCounterPath)GetProcAddress(handle, "PdhParseCounterPath" A_OR_W); pPdhSetCounterScaleFactor = (FuncPdhSetCounterScaleFactor)GetProcAddress(handle, "PdhSetCounterScaleFactor"); ! pPdhParseInstanceName = (FuncPdhParseInstanceName)GetProcAddress(handle, "PdhParseInstanceName" A_OR_W); ! pPdhBrowseCounters = (FuncPdhBrowseCounters)GetProcAddress(handle, "PdhBrowseCounters" A_OR_W); ! pPdhConnectMachine = (FuncPdhConnectMachine)GetProcAddress(handle, "PdhConnectMachine" A_OR_W); ! pPdhLookupPerfNameByIndex = (FuncPdhLookupPerfNameByIndex)GetProcAddress(handle, "PdhLookupPerfNameByIndex" A_OR_W); ! pPdhLookupPerfIndexByName = (FuncPdhLookupPerfIndexByName)GetProcAddress(handle, "PdhLookupPerfIndexByName" A_OR_W); // Pdh error codes are in 2 different ranges *************** *** 231,235 **** { DWORD detailLevel, flags = 0; ! char *reserved; PyObject *obMachine, *obObject; if (!PyArg_ParseTuple(args, "zOOi|i:EnumObjectItems", --- 238,242 ---- { DWORD detailLevel, flags = 0; ! TCHAR *reserved; PyObject *obMachine, *obObject; if (!PyArg_ParseTuple(args, "zOOi|i:EnumObjectItems", *************** *** 327,353 **** } ! PyObject *retCounter = PyList_New(0); ! if (szCounterListBuffer) ! for (szTemp = szCounterListBuffer; ! *szTemp != 0; ! szTemp += lstrlen(szTemp) + 1) { ! PyObject *obTemp = PyString_FromString(szTemp); ! PyList_Append(retCounter, obTemp); ! Py_XDECREF(obTemp); ! } ! PyObject *retInstance = PyList_New(0); if (szInstanceListBuffer) ! for (szTemp = szInstanceListBuffer; ! *szTemp != 0; ! szTemp += lstrlen(szTemp) + 1) { ! PyObject *obTemp = PyString_FromString(szTemp); ! PyList_Append(retInstance, obTemp); ! Py_XDECREF(obTemp); ! } ! PyObject *rc = Py_BuildValue("OO", retCounter, retInstance); ! Py_XDECREF(retCounter); ! Py_XDECREF(retInstance); ! free(szInstanceListBuffer); ! free(szCounterListBuffer); return rc; } --- 334,345 ---- } ! PyObject *rc = Py_BuildValue("NN", ! szCounterListBuffer ? PyWinObject_FromMultipleString(szCounterListBuffer) : PyList_New(0), ! szInstanceListBuffer ? PyWinObject_FromMultipleString(szInstanceListBuffer) : PyList_New(0)); ! if (szInstanceListBuffer) ! free(szInstanceListBuffer); ! if (szCounterListBuffer) ! free(szCounterListBuffer); return rc; } *************** *** 357,361 **** { DWORD detailLevel, refresh=1; ! char *reserved; PyObject *obMachine; if (!PyArg_ParseTuple(args, "zOi|i:EnumObjects", --- 349,353 ---- { DWORD detailLevel, refresh=1; ! TCHAR *reserved; // Is now DataSource (name of a performance log file) PyObject *obMachine; if (!PyArg_ParseTuple(args, "zOi|i:EnumObjects", *************** *** 382,386 **** pdhStatus = (*pPdhEnumObjects) ( ! reserved, // reserved strMachine, // local machine szObjectListBuffer, // pass in NULL buffers --- 374,378 ---- pdhStatus = (*pPdhEnumObjects) ( ! reserved, // DataSource strMachine, // local machine szObjectListBuffer, // pass in NULL buffers *************** *** 411,415 **** Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhEnumObjects) ( ! reserved, // reserved strMachine, // local machine szObjectListBuffer, // pass in NULL buffers --- 403,407 ---- Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhEnumObjects) ( ! reserved, // DataSource strMachine, // local machine szObjectListBuffer, // pass in NULL buffers *************** *** 425,439 **** } ! PyObject *retObject = PyList_New(0); if (szObjectListBuffer) ! for (szTemp = szObjectListBuffer; ! *szTemp != 0; ! szTemp += lstrlen(szTemp) + 1) { ! PyObject *obTemp = PyString_FromString(szTemp); ! PyList_Append(retObject, obTemp); ! Py_XDECREF(obTemp); ! } ! free(szObjectListBuffer); ! Py_INCREF(retObject); return retObject; } --- 417,423 ---- } ! PyObject *retObject = szObjectListBuffer ? PyWinObject_FromMultipleString(szObjectListBuffer) : PyList_New(0); if (szObjectListBuffer) ! free(szObjectListBuffer); return retObject; } *************** *** 445,461 **** PyObject *obhQuery; PyObject *obPath; ! DWORD userData = 0; ! if (!PyArg_ParseTuple(args, "OO|i:AddCounter", &obhQuery, // @pyparm int|hQuery||Handle to an open query. &obPath, // @pyparm string|path||Full path to the performance data ! &userData)) // @pyparm int|userData|0|User data associated with the counter. return NULL; if (!PyWinObject_AsHANDLE(obhQuery, &hQuery)) return NULL; TCHAR *szPath; if (!PyWinObject_AsTCHAR(obPath, &szPath, FALSE)) return NULL; HCOUNTER hCounter; ! CHECK_PDH_PTR(pPdhAddCounter); PyW32_BEGIN_ALLOW_THREADS PDH_STATUS pdhStatus = (*pPdhAddCounter) ( --- 429,450 ---- PyObject *obhQuery; PyObject *obPath; ! PyObject *obuserData = Py_None; // Might make more sense to use actual PyObject for userData ! DWORD_PTR userData = 0; ! CHECK_PDH_PTR(pPdhAddCounter); ! if (!PyArg_ParseTuple(args, "OO|O:AddCounter", &obhQuery, // @pyparm int|hQuery||Handle to an open query. &obPath, // @pyparm string|path||Full path to the performance data ! &obuserData)) // @pyparm int|userData|0|User data associated with the counter. return NULL; if (!PyWinObject_AsHANDLE(obhQuery, &hQuery)) return NULL; + if (obuserData != Py_None) + if (!PyWinLong_AsDWORD_PTR(obuserData, &userData)) + return NULL; TCHAR *szPath; if (!PyWinObject_AsTCHAR(obPath, &szPath, FALSE)) return NULL; HCOUNTER hCounter; ! PyW32_BEGIN_ALLOW_THREADS PDH_STATUS pdhStatus = (*pPdhAddCounter) ( *************** *** 499,507 **** static PyObject *PyOpenQuery(PyObject *self, PyObject *args) { ! DWORD userData = 0; ! char *reserved = NULL; ! if (!PyArg_ParseTuple(args, "|zi:OpenQuery", ! &reserved, // @pyparm object|reserved|None|Must be None ! &userData)) // @pyparm int|userData|0|User data associated with the query. return NULL; --- 488,497 ---- static PyObject *PyOpenQuery(PyObject *self, PyObject *args) { ! DWORD_PTR userData = 0; ! TCHAR *DataSource = NULL; ! PyObject *obDataSource = Py_None, *obuserData = Py_None; ! if (!PyArg_ParseTuple(args, "|OO:OpenQuery", ! &obDataSource, // @pyparm str|DataSource|None|Name of a performaance log file, or None for live data ! &obuserData)) // @pyparm int|userData|0|User data associated with the query. return NULL; *************** *** 509,519 **** CHECK_PDH_PTR(pPdhOpenQuery); PDH_STATUS pdhStatus; Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhOpenQuery) ( ! (void *)reserved, userData, &hQuery); Py_END_ALLOW_THREADS if (pdhStatus != ERROR_SUCCESS) return PyWin_SetAPIError("OpenQuery", pdhStatus); --- 499,516 ---- CHECK_PDH_PTR(pPdhOpenQuery); PDH_STATUS pdhStatus; + if (obuserData != Py_None) + if (!PyWinLong_AsDWORD_PTR(obuserData, &userData)) + return NULL; + if (!PyWinObject_AsTCHAR(obDataSource, &DataSource, TRUE)) + return NULL; + Py_BEGIN_ALLOW_THREADS pdhStatus = (*pPdhOpenQuery) ( ! DataSource, userData, &hQuery); Py_END_ALLOW_THREADS + PyWinObject_FreeTCHAR(DataSource); if (pdhStatus != ERROR_SUCCESS) return PyWin_SetAPIError("OpenQuery", pdhStatus); *************** *** 548,553 **** { PyObject *rc = NULL; ! char szResult[512]; ! DWORD bufSize = sizeof(szResult); DWORD flags = 0; DWORD dwInstanceIndex; --- 545,550 ---- { PyObject *rc = NULL; ! TCHAR szResult[512]; ! DWORD bufSize = sizeof(szResult)/sizeof(TCHAR); DWORD flags = 0; DWORD dwInstanceIndex; *************** *** 607,611 **** rc = PyWin_SetAPIError("MakeCounterPath for data", pdhStatus); else ! rc = PyString_FromString(szResult); done: --- 604,608 ---- rc = PyWin_SetAPIError("MakeCounterPath for data", pdhStatus); else ! rc = PyWinObject_FromTCHAR(szResult); done: *************** *** 654,679 **** PyObject *rc; if (pdhStatus != ERROR_SUCCESS) ! rc = PyWin_SetAPIError("GetCounterInfo for data", pdhStatus); else { ! if (!CheckCounterStatusOK(pInfo->CStatus)) ! rc = NULL; ! else ! rc = Py_BuildValue("iiiiiiz(zzzziz)z", ! pInfo->dwType, ! pInfo->CVersion, ! pInfo->lScale, ! pInfo->lDefaultScale, ! pInfo->dwUserData, ! pInfo->dwQueryUserData, ! pInfo->szFullPath, ! ! pInfo->szMachineName, ! pInfo->szObjectName, ! pInfo->szInstanceName, ! pInfo->szParentInstance, ! pInfo->dwInstanceIndex, ! pInfo->szCounterName, ! ! pInfo->szExplainText); } free(pInfo); --- 651,677 ---- PyObject *rc; if (pdhStatus != ERROR_SUCCESS) ! rc = PyWin_SetAPIError("GetCounterInfo for data", pdhStatus); else { ! if (!CheckCounterStatusOK(pInfo->CStatus)) ! rc = NULL; ! else ! rc = Py_BuildValue("iiiiNNN(NNNNiN)N", ! pInfo->dwType, ! pInfo->CVersion, ! // ??? CStatus is missing ??? ! pInfo->lScale, ! pInfo->lDefaultScale, ! PyWinObject_FromDWORD_PTR(pInfo->dwUserData), ! PyWinObject_FromDWORD_PTR(pInfo->dwQueryUserData), ! PyWinObject_FromTCHAR(pInfo->szFullPath), ! ! PyWinObject_FromTCHAR(pInfo->szMachineName), ! PyWinObject_FromTCHAR(pInfo->szObjectName), ! PyWinObject_FromTCHAR(pInfo->szInstanceName), ! PyWinObject_FromTCHAR(pInfo->szParentInstance), ! pInfo->dwInstanceIndex, ! PyWinObject_FromTCHAR(pInfo->szCounterName), ! ! PyWinObject_FromTCHAR(pInfo->szExplainText)); } free(pInfo); *************** *** 712,716 **** else if (format & PDH_FMT_LONG) rc = PyInt_FromLong(result.longValue); ! // XXX - need long int support else { PyErr_SetString(PyExc_ValueError, "Dont know how to convert the result"); --- 710,715 ---- else if (format & PDH_FMT_LONG) rc = PyInt_FromLong(result.longValue); ! else if (format & PDH_FMT_LARGE) ! rc = PyLong_FromLongLong(result.largeValue); else { PyErr_SetString(PyExc_ValueError, "Dont know how to convert the result"); *************** *** 789,805 **** */ // @comm The result is limited to 4096 bytes! ! char *buf = (char *)malloc(dwSize * sizeof(TCHAR)); ! PDH_STATUS pdhStatus = (*pPdhExpandCounterPath) (path, buf, &dwSize); PyObject *rc; if (pdhStatus != ERROR_SUCCESS) rc = PyWin_SetAPIError("ExpandCounterPath for data", pdhStatus); ! else { ! rc = PyList_New(0); ! for (char *szTemp = buf;*szTemp != 0;szTemp += lstrlen(szTemp) + 1) { ! PyObject *obTemp = PyString_FromString(szTemp); ! PyList_Append(rc, obTemp); ! Py_XDECREF(obTemp); ! } ! } PyWinObject_FreeTCHAR(path); free(buf); --- 788,803 ---- */ // @comm The result is limited to 4096 bytes! ! TCHAR *buf = (TCHAR *)malloc(dwSize * sizeof(TCHAR)); ! if (buf==NULL){ ! PyErr_NoMemory(); ! PyWinObject_FreeTCHAR(path); ! return NULL; ! } ! PDH_STATUS pdhStatus = (*pPdhExpandCounterPath) (path, buf, &dwSize); PyObject *rc; if (pdhStatus != ERROR_SUCCESS) rc = PyWin_SetAPIError("ExpandCounterPath for data", pdhStatus); ! else ! rc = PyWinObject_FromMultipleString(buf); PyWinObject_FreeTCHAR(path); free(buf); *************** *** 848,858 **** rc = PyWin_SetAPIError("ParseCounterPath", pdhStatus); } else { ! rc = Py_BuildValue("zzzziz", ! pCPE->szMachineName, ! pCPE->szObjectName, ! pCPE->szInstanceName, ! pCPE->szParentInstance, pCPE->dwInstanceIndex, ! pCPE->szCounterName); } free(pBuf); --- 846,856 ---- rc = PyWin_SetAPIError("ParseCounterPath", pdhStatus); } else { ! rc = Py_BuildValue("NNNNkN", ! PyWinObject_FromTCHAR(pCPE->szMachineName), ! PyWinObject_FromTCHAR(pCPE->szObjectName), ! PyWinObject_FromTCHAR(pCPE->szInstanceName), ! PyWinObject_FromTCHAR(pCPE->szParentInstance), pCPE->dwInstanceIndex, ! PyWinObject_FromTCHAR(pCPE->szCounterName)); } free(pBuf); *************** *** 881,885 **** if (pdhStatus != 0) return PyWin_SetAPIError("ParseInstanceName", pdhStatus); ! return Py_BuildValue("ssi", szName, szParent, dwInstance); } --- 879,886 ---- if (pdhStatus != 0) return PyWin_SetAPIError("ParseInstanceName", pdhStatus); ! return Py_BuildValue("NNk", ! PyWinObject_FromTCHAR(szName), ! PyWinObject_FromTCHAR(szParent), ! dwInstance); } *************** *** 917,921 **** DWORD rc = PDH_INVALID_DATA; CEnterLeavePython _celp; ! PyObject *args = Py_BuildValue("(s)", pMy->pcfg->szReturnPathBuffer); PyObject *result = PyEval_CallObject(pMy->func, args); Py_XDECREF(args); --- 918,925 ---- DWORD rc = PDH_INVALID_DATA; CEnterLeavePython _celp; ! /* ??? This does the wrong thing when multiple counters are selected. ! Should use PyWinObject_AsMultipleString, but will break code that expects a single string. ??? ! */ ! PyObject *args = Py_BuildValue("(N)", PyWinObject_FromTCHAR(pMy->pcfg->szReturnPathBuffer)); PyObject *result = PyEval_CallObject(pMy->func, args); Py_XDECREF(args); *************** *** 983,987 **** pcfg->pCallBack = PyCounterPathCallback; ! pcfg->szReturnPathBuffer = (char *)malloc(1024); pcfg->cchReturnPathLength = 1024; --- 987,991 ---- pcfg->pCallBack = PyCounterPathCallback; ! pcfg->szReturnPathBuffer = (TCHAR *)malloc(1024 * sizeof(TCHAR)); pcfg->cchReturnPathLength = 1024; *************** *** 998,1002 **** } else ! rc = PyString_FromString(pcfg->szReturnPathBuffer); // Note - myCfg does not own any references --- 1002,1006 ---- } else ! rc = PyWinObject_FromTCHAR(pcfg->szReturnPathBuffer); // Note - myCfg does not own any references *************** *** 1112,1148 **** }; - /* Initialize this module. */ - int AddConstant(PyObject *dict, char *key, long value) - { - PyObject *okey = PyString_FromString(key); - PyObject *oval = PyLong_FromLong(value); - if (!okey || !oval) { - Py_XDECREF(okey); - Py_XDECREF(oval); - return 1; - } - int rc = PyDict_SetItem(dict,okey, oval); - Py_XDECREF(okey); - Py_XDECREF(oval); - return rc; - } - #define ADD_CONSTANT(tok) AddConstant(dict,#tok, tok) ! extern"C" __declspec(dllexport) void ! initwin32pdh(void) { InitializeCriticalSection(&critSec); PyObject *dict, *module; module = Py_InitModule("win32pdh", win32pdh_functions); ! if (!module) /* Eeek - some serious error! */ ! return; dict = PyModule_GetDict(module); ! if (!dict) return; ! PyWinGlobals_Ensure(); ! Py_INCREF(PyWinExc_ApiError); PyDict_SetItemString(dict, "error", PyWinExc_ApiError); ! win32pdh_counter_error = PyString_FromString("win32pdh counter status error"); ! PyDict_SetItemString(dict, "counter status error", win32pdh_counter_error); LoadPointers(); // Setting an error in this function will cause Python to spew. --- 1116,1159 ---- }; + #define ADD_CONSTANT(tok) PyModule_AddIntConstant(module, #tok, tok) ! extern"C" __declspec(dllexport) ! #if (PY_VERSION_HEX < 0x03000000) ! void initwin32pdh(void) ! #else ! PyObject *PyInit_win32pdh(void) ! #endif { InitializeCriticalSection(&critSec); PyObject *dict, *module; + + #if (PY_VERSION_HEX < 0x03000000) + #define RETURN_ERROR return; module = Py_InitModule("win32pdh", win32pdh_functions); ! #else ! ! #define RETURN_ERROR return NULL; ! static PyModuleDef win32pdh_def = { ! PyModuleDef_HEAD_INIT, ! "win32pdh", ! "A module, encapsulating the Windows Performance Data Helpers API", ! -1, ! win32pdh_functions ! }; ! module = PyModule_Create(&win32pdh_def); ! #endif ! ! if (!module) ! RETURN_ERROR; dict = PyModule_GetDict(module); ! if (!dict) ! RETURN_ERROR; ! if (PyWinGlobals_Ensure() == -1) ! RETURN_ERROR; ! PyDict_SetItemString(dict, "error", PyWinExc_ApiError); ! win32pdh_counter_error = PyErr_NewException("win32pdh.counter_status_error", NULL, NULL); ! PyDict_SetItemString(dict, "counter_status_error", win32pdh_counter_error); LoadPointers(); // Setting an error in this function will cause Python to spew. *************** *** 1167,1169 **** --- 1178,1183 ---- ADD_CONSTANT(PERF_DETAIL_WIZARD); // ADD_CONSTANT(); + #if (PY_VERSION_HEX >= 0x03000000) + return module; + #endif } |