我Hook了WriteProcessMemory,我得到了进程句柄,我希望从这个进程句柄来反查出PID,从而知道是哪个程序进行的WriteProcessMemory。有哪个函数可以从进程句柄得到进程ID呢,欢迎大家讨论!

解决方案 »

  1.   

    DWORD GetProcessId(
      HANDLE Process
    );
      

  2.   


    美女明慧啊……不仅仅是XP,而且必须是XP SP1以上。但现在有谁不是XP SP2呢?
      

  3.   

    呵呵,我查了下,好像有其他办法,支持其他的操作系统,可以测试下,This has been tried successfully on Win2K and Vista:#include "Winternl.h"typedef DWORD (WINAPI* pfnGetProcID)(HANDLE h);typedef NTSTATUS (WINAPI* pfnQueryInformationProcess)(
        HANDLE ProcessHandle,
        PROCESSINFOCLASS ProcessInformationClass,
        PVOID ProcessInformation,
        ULONG ProcessInformationLength,
        PULONG ReturnLength);DWORD MyGetProcessId(HANDLE h)
    {
        static pfnQueryInformationProcess ntQIP = (pfnQueryInformationProcess) GetProcAddress(GetModuleHandle("NTDLL.DLL"),"NtQueryInformationProcess");
        static pfnGetProcID getPId  = (pfnGetProcID) GetProcAddress(GetModuleHandle("KERNEL32.DLL"),"GetProcessId");    if ((ntQIP == NULL) && (getPId == NULL))
            throw Exception("Can't retrieve process ID : GetProcessID not supported");    if (getPId != NULL)
            return getPId(h);
        else
        {
            PROCESS_BASIC_INFORMATION info;
            ULONG returnSize;
            ntQIP(h, ProcessBasicInformation, &info, sizeof(info), &returnSize);  // Get basic information.
            return info.UniqueProcessId;
        }
    }