Created
January 4, 2010 21:39
-
-
Save kusma/268888 to your computer and use it in GitHub Desktop.
This file contains 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
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