OLD | NEW |
(Empty) | |
| 1 //------------------------------------------------------------------------------
------------------- |
| 2 // <copyright file="proc2utl.cpp" company="Outercurve Foundation"> |
| 3 // Copyright (c) 2004, Outercurve Foundation. |
| 4 // This software is released under Microsoft Reciprocal License (MS-RL). |
| 5 // The license and further copyright text can be found in the file |
| 6 // LICENSE.TXT at the root directory of the distribution. |
| 7 // </copyright> |
| 8 // |
| 9 // <summary> |
| 10 // Process helper functions that require PSAPI.DLL. |
| 11 // </summary> |
| 12 //------------------------------------------------------------------------------
------------------- |
| 13 |
| 14 #include "../precomp.h" |
| 15 |
| 16 /******************************************************************** |
| 17 ProcFindAllIdsFromExeName() - returns an array of process ids that are running
specified executable. |
| 18 |
| 19 *******************************************************************/ |
| 20 extern "C" HRESULT DAPI ProcFindAllIdsFromExeName( |
| 21 __in_z LPCWSTR wzExeName, |
| 22 __out DWORD** ppdwProcessIds, |
| 23 __out DWORD* pcProcessIds |
| 24 ) |
| 25 { |
| 26 HRESULT hr = S_OK; |
| 27 DWORD er = ERROR_SUCCESS; |
| 28 HANDLE hSnap = INVALID_HANDLE_VALUE; |
| 29 BOOL fContinue = FALSE; |
| 30 PROCESSENTRY32W peData = { sizeof(peData) }; |
| 31 |
| 32 hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 33 if (INVALID_HANDLE_VALUE == hSnap) |
| 34 { |
| 35 ExitWithLastError(hr, "Failed to create snapshot of processes on system"
); |
| 36 } |
| 37 |
| 38 fContinue = ::Process32FirstW(hSnap, &peData); |
| 39 |
| 40 while (fContinue) |
| 41 { |
| 42 if (0 == lstrcmpiW((LPCWSTR)&(peData.szExeFile), wzExeName)) |
| 43 { |
| 44 if (!*ppdwProcessIds) |
| 45 { |
| 46 *ppdwProcessIds = static_cast<DWORD*>(MemAlloc(sizeof(DWORD), TR
UE)); |
| 47 ExitOnNull(ppdwProcessIds, hr, E_OUTOFMEMORY, "Failed to allocat
e array for returned process IDs."); |
| 48 } |
| 49 else |
| 50 { |
| 51 DWORD* pdwReAllocReturnedPids = NULL; |
| 52 pdwReAllocReturnedPids = static_cast<DWORD*>(MemReAlloc(*ppdwPro
cessIds, sizeof(DWORD) * ((*pcProcessIds) + 1), TRUE)); |
| 53 ExitOnNull(pdwReAllocReturnedPids, hr, E_OUTOFMEMORY, "Failed to
re-allocate array for returned process IDs."); |
| 54 |
| 55 *ppdwProcessIds = pdwReAllocReturnedPids; |
| 56 } |
| 57 |
| 58 (*ppdwProcessIds)[*pcProcessIds] = peData.th32ProcessID; |
| 59 ++(*pcProcessIds); |
| 60 } |
| 61 |
| 62 fContinue = ::Process32NextW(hSnap, &peData); |
| 63 } |
| 64 |
| 65 er = ::GetLastError(); |
| 66 if (ERROR_NO_MORE_FILES == er) |
| 67 { |
| 68 hr = S_OK; |
| 69 } |
| 70 else |
| 71 { |
| 72 hr = HRESULT_FROM_WIN32(er); |
| 73 } |
| 74 |
| 75 LExit: |
| 76 ReleaseFile(hSnap); |
| 77 |
| 78 return hr; |
| 79 } |
OLD | NEW |