Created
June 16, 2020 20:34
-
-
Save Crspy/553930d03880a8e09352de7f359a1fa9 to your computer and use it in GitHub Desktop.
Find Process Handle & BaseAdresss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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