From 5d8375007754101ff2889d0e79486c8f9b7cf5ab Mon Sep 17 00:00:00 2001 From: Rob Mensching Date: Sun, 3 Sep 2017 11:22:38 -0700 Subject: Initial commit --- src/dutil/proc2utl.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/dutil/proc2utl.cpp (limited to 'src/dutil/proc2utl.cpp') 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 @@ +// 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. + +#include "precomp.h" + +/******************************************************************** + ProcFindAllIdsFromExeName() - returns an array of process ids that are running specified executable. + +*******************************************************************/ +extern "C" HRESULT DAPI ProcFindAllIdsFromExeName( + __in_z LPCWSTR wzExeName, + __out DWORD** ppdwProcessIds, + __out DWORD* pcProcessIds + ) +{ + HRESULT hr = S_OK; + DWORD er = ERROR_SUCCESS; + HANDLE hSnap = INVALID_HANDLE_VALUE; + BOOL fContinue = FALSE; + PROCESSENTRY32W peData = { sizeof(peData) }; + + hSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (INVALID_HANDLE_VALUE == hSnap) + { + ExitWithLastError(hr, "Failed to create snapshot of processes on system"); + } + + fContinue = ::Process32FirstW(hSnap, &peData); + + while (fContinue) + { + if (0 == lstrcmpiW((LPCWSTR)&(peData.szExeFile), wzExeName)) + { + if (!*ppdwProcessIds) + { + *ppdwProcessIds = static_cast(MemAlloc(sizeof(DWORD), TRUE)); + ExitOnNull(ppdwProcessIds, hr, E_OUTOFMEMORY, "Failed to allocate array for returned process IDs."); + } + else + { + DWORD* pdwReAllocReturnedPids = NULL; + pdwReAllocReturnedPids = static_cast(MemReAlloc(*ppdwProcessIds, sizeof(DWORD) * ((*pcProcessIds) + 1), TRUE)); + ExitOnNull(pdwReAllocReturnedPids, hr, E_OUTOFMEMORY, "Failed to re-allocate array for returned process IDs."); + + *ppdwProcessIds = pdwReAllocReturnedPids; + } + + (*ppdwProcessIds)[*pcProcessIds] = peData.th32ProcessID; + ++(*pcProcessIds); + } + + fContinue = ::Process32NextW(hSnap, &peData); + } + + er = ::GetLastError(); + if (ERROR_NO_MORE_FILES == er) + { + hr = S_OK; + } + else + { + hr = HRESULT_FROM_WIN32(er); + } + +LExit: + ReleaseFile(hSnap); + + return hr; +} -- cgit v1.2.3-55-g6feb