Skip to content

Instantly share code, notes, and snippets.

@kusma
Created January 4, 2010 21:39
Show Gist options
  • Save kusma/268888 to your computer and use it in GitHub Desktop.
Save kusma/268888 to your computer and use it in GitHub Desktop.
static DWORD GetProcessId(HANDLE hProcess)
{
typedef DWORD (WINAPI *pfnGPI)(HANDLE);
typedef ULONG (WINAPI *pfnNTQIP)(HANDLE, ULONG, PVOID, ULONG, PULONG);
static int first = 1;
static pfnGPI GetProcessId;
static pfnNTQIP ZwQueryInformationProcess;
if (first) {
first = 0;
GetProcessId = (pfnGPI)GetProcAddress(
GetModuleHandle("KERNEL32.DLL"), "GetProcessId");
if (!GetProcessId)
ZwQueryInformationProcess = (pfnNTQIP)GetProcAddress(
GetModuleHandle("NTDLL.DLL"),
"ZwQueryInformationProcess");
}
if (GetProcessId)
return GetProcessId(hProcess);
if (ZwQueryInformationProcess) {
struct {
PVOID Reserved1;
PVOID PebBaseAddress;
PVOID Reserved2[2];
ULONG_PTR UniqueProcessId;
PVOID Reserved3;
} pbi;
ZwQueryInformationProcess(hProcess, 0, &pbi, sizeof(pbi), 0);
return pbi.UniqueProcessId;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment