From fdc0cf5c1f1da3d085e00ac534cd9d6460f3f9d5 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 1 Sep 2009 15:39:33 -0700 Subject: [PATCH] add mapping from wmi HRESULT to NTRESULT --- src/os/win32/win32_sigar.c | 9 ++++++++- src/os/win32/wmi.cpp | 34 +++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index c98af479..5ac57b64 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -1445,7 +1445,11 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid, } /* likely we are 32-bit, pid process is 64-bit */ - return sigar_proc_args_wmi_get(sigar, pid, procargs); + status = sigar_proc_args_wmi_get(sigar, pid, procargs); + if (status == ERROR_NOT_FOUND) { + status = SIGAR_NO_SUCH_PROCESS; + } + return status; } int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid, @@ -1609,6 +1613,9 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid, /* likely we are 32-bit, pid process is 64-bit */ /* procexe->cwd[0] = XXX where else can we try? */ status = sigar_proc_exe_wmi_get(sigar, pid, procexe); + if (status == ERROR_NOT_FOUND) { + status = SIGAR_NO_SUCH_PROCESS; + } } if (procexe->cwd[0] != '\0') { diff --git a/src/os/win32/wmi.cpp b/src/os/win32/wmi.cpp index a816bbd9..82229cab 100644 --- a/src/os/win32/wmi.cpp +++ b/src/os/win32/wmi.cpp @@ -42,15 +42,18 @@ class WMI { HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len); HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value); HRESULT GetProcCommandLine(DWORD pid, TCHAR *value); + int GetLastError(); private: IWbemServices *wbem; + HRESULT result; BSTR GetProcQuery(DWORD pid); }; WMI::WMI() { wbem = NULL; + result = S_OK; CoInitializeEx(NULL, COINIT_MULTITHREADED); } @@ -60,14 +63,31 @@ WMI::~WMI() CoUninitialize(); } +/* XXX must be a better way to map HRESULT */ +int WMI::GetLastError() +{ + switch (result) { + case S_OK: + return ERROR_SUCCESS; + case WBEM_E_NOT_FOUND: + return ERROR_NOT_FOUND; + case WBEM_E_ACCESS_DENIED: + return ERROR_ACCESS_DENIED; + case WBEM_E_NOT_SUPPORTED: + return SIGAR_ENOTIMPL; + default: + return ERROR_INVALID_FUNCTION; + } +} + HRESULT WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass) { - HRESULT result; IWbemLocator *locator; wchar_t path[MAX_PATH]; if (wbem) { - return S_OK; + result = S_OK; + return result; } result = @@ -116,6 +136,7 @@ void WMI::Close() if (wbem) { wbem->Release(); wbem = NULL; + result = S_OK; } } @@ -128,7 +149,6 @@ BSTR WMI::GetProcQuery(DWORD pid) HRESULT WMI::GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len) { - HRESULT result; IWbemClassObject *obj; VARIANT var; @@ -177,11 +197,11 @@ extern "C" int sigar_proc_args_wmi_get(sigar_t *sigar, sigar_pid_t pid, WMI *wmi = new WMI(); if (FAILED(wmi->Open())) { - return GetLastError(); + return wmi->GetLastError(); } if (FAILED(wmi->GetProcCommandLine(pid, buf))) { - status = GetLastError(); + status = wmi->GetLastError(); } else { status = sigar_parse_proc_args(sigar, buf, procargs); @@ -201,13 +221,13 @@ extern "C" int sigar_proc_exe_wmi_get(sigar_t *sigar, sigar_pid_t pid, WMI *wmi = new WMI(); if (FAILED(wmi->Open())) { - return GetLastError(); + return wmi->GetLastError(); } procexe->name[0] = '\0'; if (FAILED(wmi->GetProcExecutablePath(pid, buf))) { - status = GetLastError(); + status = wmi->GetLastError(); } else { status = SIGAR_OK;