Skip to content

Instantly share code, notes, and snippets.

@Crspy
Created June 16, 2020 20:34
Show Gist options
  • Select an option

  • Save Crspy/553930d03880a8e09352de7f359a1fa9 to your computer and use it in GitHub Desktop.

Select an option

Save Crspy/553930d03880a8e09352de7f359a1fa9 to your computer and use it in GitHub Desktop.
Find Process Handle & BaseAdresss
struct ProcessInfo {
HANDLE handle;
HMODULE baseAddress;
};
ProcessInfo FindProcessHandle(const TCHAR* targetProcName)
{
DWORD aProcesses[1024], bytesNeeded;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &bytesNeeded))
{
return {};
}
// Calculate how many process identifiers were returned.
auto processesCount = bytesNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for (size_t i = 0; i < processesCount; i++)
{
if (aProcesses[i] != 0)
{
TCHAR szProcessName[MAX_PATH] = _T("<unknown>");
// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,
FALSE, aProcesses[i]);
// Get the process name.
if (NULL != hProcess)
{
HMODULE hMod;
DWORD cbNeeded;
if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
&cbNeeded))
{
GetModuleBaseName(hProcess, hMod, szProcessName,
sizeof(szProcessName) / sizeof(TCHAR));
}
// Print the process name and identifier.
//_tprintf(TEXT("%s (PID: %u)\n"), szProcessName, processID);
if (_wcsicmp(szProcessName, targetProcName) == 0)
{
return { hProcess, hMod }; //
}
// Release the handle to the process.
CloseHandle(hProcess);
}
}
}
return {};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment