diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2009-03-20 00:40:24 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2009-03-20 00:40:24 +0000 |
commit | 87a4eb739f94bc4a1f2976911338effe5caa0c0f (patch) | |
tree | 9d4c1b0f8b1ea1f117dbd9a638ad3c0e70cc6665 /win/inspircd_win32wrapper.cpp | |
parent | e0856f12218ac78fe798b9b76cc1b0fb1bafe62e (diff) |
This is a nicer fix, and we are seen to be explicitly freeing the BSTR values, which explicitly shows any reader of the code that this is a leak prevention measure
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@11239 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'win/inspircd_win32wrapper.cpp')
-rw-r--r-- | win/inspircd_win32wrapper.cpp | 85 |
1 files changed, 46 insertions, 39 deletions
diff --git a/win/inspircd_win32wrapper.cpp b/win/inspircd_win32wrapper.cpp index 2aac1da05..ed60a4028 100644 --- a/win/inspircd_win32wrapper.cpp +++ b/win/inspircd_win32wrapper.cpp @@ -623,59 +623,66 @@ void donewmi() int getcpu() { HRESULT hres; + int cpu = -1; /* Use WQL, similar to SQL, to construct a query that lists the cpu usage and pid of all processes */ IEnumWbemClassObject* pEnumerator = NULL; - hres = pSvc->ExecQuery(L"WQL", L"Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process", - WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); - - /* Query didn't work */ - if (FAILED(hres)) - return -1; - IWbemClassObject *pclsObj = NULL; - ULONG uReturn = 0; - - /* Iterate the query results */ - while (pEnumerator) - { - VARIANT vtProp; - /* Next item */ - HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); + BSTR Language = SysAllocString(L"WQL"); + BSTR Query = SysAllocString(L"Select PercentProcessorTime,IDProcess from Win32_PerfFormattedData_PerfProc_Process"); - /* No more items left */ - if (uReturn == 0) - break; + hres = pSvc->ExecQuery(Language, Query, WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); + + /* Query didn't work */ + if (!FAILED(hres)) + { + IWbemClassObject *pclsObj = NULL; + ULONG uReturn = 0; - /* Find process ID */ - hr = pclsObj->Get(L"IDProcess", 0, &vtProp, 0, 0); - if (!FAILED(hr)) + /* Iterate the query results */ + while (pEnumerator) { - /* Matches our process ID? */ - if (vtProp.uintVal == GetCurrentProcessId()) + VARIANT vtProp; + /* Next item */ + HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); + + /* No more items left */ + if (uReturn == 0) + break; + + /* Find process ID */ + hr = pclsObj->Get(L"IDProcess", 0, &vtProp, 0, 0); + if (!FAILED(hr)) { - VariantClear(&vtProp); - /* Get CPU percentage for this process */ - hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0); - if (!FAILED(hr)) + /* Matches our process ID? */ + if (vtProp.uintVal == GetCurrentProcessId()) { - /* Deal with wide string ickyness. Who in their right - * mind puts a number in a bstrVal wide string item?! - */ VariantClear(&vtProp); - int cpu = 0; - std::wstringstream out(vtProp.bstrVal); - out >> cpu; - pEnumerator->Release(); - pclsObj->Release(); - return cpu; + /* Get CPU percentage for this process */ + hr = pclsObj->Get(L"PercentProcessorTime", 0, &vtProp, 0, 0); + if (!FAILED(hr)) + { + /* Deal with wide string ickyness. Who in their right + * mind puts a number in a bstrVal wide string item?! + */ + VariantClear(&vtProp); + cpu = 0; + std::wstringstream out(vtProp.bstrVal); + out >> cpu; + break; + } } } } + + pEnumerator->Release(); + pclsObj->Release(); } - pEnumerator->Release(); - pclsObj->Release(); - return -1; + + SysFreeString(Language); + SysFreeString(Query); + + return cpu; } void usleep(unsigned long usecs) |