Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include <stdio.h>
- #include <tchar.h>
- #include <psapi.h>
- // To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
- // and compile with -DPSAPI_VERSION=1
- void PrintProcessNameAndID(DWORD processID)
- {
- TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
- // Get a handle to the process.
- HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
- PROCESS_VM_READ,
- FALSE, processID);
- // 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);
- // Release the handle to the process.
- CloseHandle(hProcess);
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- // Get the list of process identifiers.
- DWORD aProcesses[1024], cbNeeded, cProcesses;
- unsigned int i;
- if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
- {
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment