The function "ProcessHandleToId" tells you which process ID hides behind a specific process handle. This function is based on undocumented calls, but it works fine in all windows 32 bit systems.

The function is useful mostly for API hooks. E.g. if you install a hook on APIs like TerminateProcess or OpenProcess or something similar, you often need to know which process you're dealing with. The win32 APIs don't offer any way to get this kind of information, so I had to implement it myself.

function ProcessHandleToId (processHandle: dword) : dword; stdcall;
function  ThreadHandleToId ( threadHandle: dword) : dword; stdcall;

// Example:
function OpenProcessCallback(access         : dword;
                             inheritHandles : bool;
                             processHandle  : dword) : dword; stdcall;
var pid : dword;
  pid := ProcessHandleToId(processHandle);
  if pid = GetCurrentProcessID then
    // someone wants to open itself, strange thing...