diff options
Diffstat (limited to 'src/dutil/proc2utl.cpp')
| -rw-r--r-- | src/dutil/proc2utl.cpp | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/dutil/proc2utl.cpp b/src/dutil/proc2utl.cpp new file mode 100644 index 00000000..8a2fd09b --- /dev/null +++ b/src/dutil/proc2utl.cpp | |||
| @@ -0,0 +1,68 @@ | |||
| 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 | ProcFindAllIdsFromExeName() - returns an array of process ids that are running specified executable. | ||
| 7 | |||
| 8 | *******************************************************************/ | ||
| 9 | extern "C" HRESULT DAPI ProcFindAllIdsFromExeName( | ||
| 10 | __in_z LPCWSTR wzExeName, | ||
| 11 | __out DWORD** ppdwProcessIds, | ||
| 12 | __out DWORD* pcProcessIds | ||
| 13 | ) | ||
| 14 | { | ||
| 15 | HRESULT hr = S_OK; | ||
| 16 | DWORD er = ERROR_SUCCESS; | ||
| 17 | HANDLE hSnap = INVALID_HANDLE_VALUE; | ||
| 18 | BOOL fContinue = FALSE; | ||
| 19 | PROCESSENTRY32W peData = { sizeof(peData) }; | ||
| 20 | |||
| 21 | hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | ||
| 22 | if (INVALID_HANDLE_VALUE == hSnap) | ||
| 23 | { | ||
| 24 | ExitWithLastError(hr, "Failed to create snapshot of processes on system"); | ||
| 25 | } | ||
| 26 | |||
| 27 | fContinue = ::Process32FirstW(hSnap, &peData); | ||
| 28 | |||
| 29 | while (fContinue) | ||
| 30 | { | ||
| 31 | if (0 == lstrcmpiW((LPCWSTR)&(peData.szExeFile), wzExeName)) | ||
| 32 | { | ||
| 33 | if (!*ppdwProcessIds) | ||
| 34 | { | ||
| 35 | *ppdwProcessIds = static_cast<DWORD*>(MemAlloc(sizeof(DWORD), TRUE)); | ||
| 36 | ExitOnNull(ppdwProcessIds, hr, E_OUTOFMEMORY, "Failed to allocate array for returned process IDs."); | ||
| 37 | } | ||
| 38 | else | ||
| 39 | { | ||
| 40 | DWORD* pdwReAllocReturnedPids = NULL; | ||
| 41 | pdwReAllocReturnedPids = static_cast<DWORD*>(MemReAlloc(*ppdwProcessIds, sizeof(DWORD) * ((*pcProcessIds) + 1), TRUE)); | ||
| 42 | ExitOnNull(pdwReAllocReturnedPids, hr, E_OUTOFMEMORY, "Failed to re-allocate array for returned process IDs."); | ||
| 43 | |||
| 44 | *ppdwProcessIds = pdwReAllocReturnedPids; | ||
| 45 | } | ||
| 46 | |||
| 47 | (*ppdwProcessIds)[*pcProcessIds] = peData.th32ProcessID; | ||
| 48 | ++(*pcProcessIds); | ||
| 49 | } | ||
| 50 | |||
| 51 | fContinue = ::Process32NextW(hSnap, &peData); | ||
| 52 | } | ||
| 53 | |||
| 54 | er = ::GetLastError(); | ||
| 55 | if (ERROR_NO_MORE_FILES == er) | ||
| 56 | { | ||
| 57 | hr = S_OK; | ||
| 58 | } | ||
| 59 | else | ||
| 60 | { | ||
| 61 | hr = HRESULT_FROM_WIN32(er); | ||
| 62 | } | ||
| 63 | |||
| 64 | LExit: | ||
| 65 | ReleaseFile(hSnap); | ||
| 66 | |||
| 67 | return hr; | ||
| 68 | } | ||
