Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <ntstatus.h>
- #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
- enum SYSTEM_INFORMATION_CLASS { SystemProcessInformation = 5 };
- struct SYSTEM_PROCESS_INFORMATION
- {
- ULONG NextEntryOffset;
- DWORD unused0[14];
- WCHAR* ImageName;
- DWORD unused1;
- HANDLE UniqueProcessId;
- };
- extern "C" NTSTATUS __stdcall NtQuerySystemInformation(SYSTEM_INFORMATION_CLASS systemInformationClass, void* pSystemInformation, ULONG systemInformationLength, ULONG* pReturnLength);
- DWORD GetProcessID(wstring const& ImageName)
- {
- size_t size = 0x10000;
- string Data;
- Data.resize(size);
- NTSTATUS status;
- while ((status = NtQuerySystemInformation(SystemProcessInformation, (char*)Data.data(), (ULONG)Data.size(), nullptr)) == STATUS_INFO_LENGTH_MISMATCH)
- Data.resize(size *= 2);
- if (!NT_SUCCESS(status))
- throw runtime_error("NtQuerySystemInformation failed");
- auto pProcessInfo = (SYSTEM_PROCESS_INFORMATION*)Data.data();
- while (true)
- {
- if (pProcessInfo->ImageName && !_wcsicmp(pProcessInfo->ImageName, ImageName.c_str()))
- return (DWORD)pProcessInfo->UniqueProcessId;
- if (!pProcessInfo->NextEntryOffset)
- break;
- pProcessInfo = (SYSTEM_PROCESS_INFORMATION*)((byte*)pProcessInfo + pProcessInfo->NextEntryOffset);
- }
- throw runtime_error("process not found");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement