| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | |
| 6 | // Exit macros |
| 7 | #define ProcExitOnLastError(x, s, ...) ExitOnLastErrorSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 8 | #define ProcExitOnLastErrorDebugTrace(x, s, ...) ExitOnLastErrorDebugTraceSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 9 | #define ProcExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 10 | #define ProcExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 11 | #define ProcExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 12 | #define ProcExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PROCUTIL, x, s, __VA_ARGS__) |
| 13 | #define ProcExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PROCUTIL, p, x, e, s, __VA_ARGS__) |
| 14 | #define ProcExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, p, x, s, __VA_ARGS__) |
| 15 | #define ProcExitOnNullDebugTrace(p, x, e, s, ...) ExitOnNullDebugTraceSource(DUTIL_SOURCE_PROCUTIL, p, x, e, s, __VA_ARGS__) |
| 16 | #define ProcExitOnInvalidHandleWithLastError(p, x, s, ...) ExitOnInvalidHandleWithLastErrorSource(DUTIL_SOURCE_PROCUTIL, p, x, s, __VA_ARGS__) |
| 17 | #define ProcExitOnWin32Error(e, x, s, ...) ExitOnWin32ErrorSource(DUTIL_SOURCE_PROCUTIL, e, x, s, __VA_ARGS__) |
| 18 | #define ProcExitOnGdipFailure(g, x, s, ...) ExitOnGdipFailureSource(DUTIL_SOURCE_PROCUTIL, g, x, s, __VA_ARGS__) |
| 19 | |
| 20 | /******************************************************************** |
| 21 | ProcFindAllIdsFromExeName() - returns an array of process ids that are running specified executable. |
| 22 | |
| 23 | *******************************************************************/ |
| 24 | extern "C" HRESULT DAPI ProcFindAllIdsFromExeName( |
| 25 | __in_z LPCWSTR wzExeName, |
| 26 | __out DWORD** ppdwProcessIds, |
| 27 | __out DWORD* pcProcessIds |
| 28 | ) |
| 29 | { |
| 30 | HRESULT hr = S_OK; |
| 31 | DWORD er = ERROR_SUCCESS; |
| 32 | HANDLE hSnap = INVALID_HANDLE_VALUE; |
| 33 | BOOL fContinue = FALSE; |
| 34 | PROCESSENTRY32W peData = { sizeof(peData) }; |
| 35 | |
| 36 | hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 37 | if (INVALID_HANDLE_VALUE == hSnap) |
| 38 | { |
| 39 | ProcExitWithLastError(hr, "Failed to create snapshot of processes on system"); |
| 40 | } |
| 41 | |
| 42 | fContinue = ::Process32FirstW(hSnap, &peData); |
| 43 | |
| 44 | while (fContinue) |
| 45 | { |
| 46 | if (0 == lstrcmpiW((LPCWSTR)&(peData.szExeFile), wzExeName)) |
| 47 | { |
| 48 | if (!*ppdwProcessIds) |
| 49 | { |
| 50 | *ppdwProcessIds = static_cast<DWORD*>(MemAlloc(sizeof(DWORD), TRUE)); |
| 51 | ProcExitOnNull(ppdwProcessIds, hr, E_OUTOFMEMORY, "Failed to allocate array for returned process IDs."); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | DWORD* pdwReAllocReturnedPids = NULL; |
| 56 | pdwReAllocReturnedPids = static_cast<DWORD*>(MemReAlloc(*ppdwProcessIds, sizeof(DWORD) * ((*pcProcessIds) + 1), TRUE)); |
| 57 | ProcExitOnNull(pdwReAllocReturnedPids, hr, E_OUTOFMEMORY, "Failed to re-allocate array for returned process IDs."); |
| 58 | |
| 59 | *ppdwProcessIds = pdwReAllocReturnedPids; |
| 60 | } |
| 61 | |
| 62 | (*ppdwProcessIds)[*pcProcessIds] = peData.th32ProcessID; |
| 63 | ++(*pcProcessIds); |
| 64 | } |
| 65 | |
| 66 | fContinue = ::Process32NextW(hSnap, &peData); |
| 67 | } |
| 68 | |
| 69 | er = ::GetLastError(); |
| 70 | if (ERROR_NO_MORE_FILES == er) |
| 71 | { |
| 72 | hr = S_OK; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | hr = HRESULT_FROM_WIN32(er); |
| 77 | } |
| 78 | |
| 79 | LExit: |
| 80 | ReleaseFile(hSnap); |
| 81 | |
| 82 | return hr; |
| 83 | } |