首页 > Windows开发 > 详细

GetProcessIdOfThread在WinXP及之前操作系统的替代实现

时间:2015-11-14 23:12:59      阅读:399      评论:0      收藏:0      [点我收藏+]

还是学习VLD2.X版本看到的:

在Windows XP及之前的操作系统没有提供GetProcessIdOfThread的API,这里给出了一个替代的实现方式:

头文件:

#if _WIN32_WINNT < 0x0600 // Windows XP or earlier, no GetProcessIdOfThread()
DWORD _GetProcessIdOfThread (HANDLE thread);
#define GetProcessIdOfThread _GetProcessIdOfThread
#endif

源文件:

通过NtQueryInformationThread函数获取:

DWORD _GetProcessIdOfThread (HANDLE thread)
{
    typedef struct _CLIENT_ID {
        HANDLE UniqueProcess;
        HANDLE UniqueThread;
    } CLIENT_ID, *PCLIENT_ID;

    typedef LONG NTSTATUS;
    typedef LONG KPRIORITY;

    typedef struct _THREAD_BASIC_INFORMATION {
        NTSTATUS  ExitStatus;
        PVOID     TebBaseAddress;
        CLIENT_ID ClientId;
        KAFFINITY AffinityMask;
        KPRIORITY Priority;
        KPRIORITY BasePriority;
    } THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;

    const static THREADINFOCLASS ThreadBasicInformation = (THREADINFOCLASS)0;

    typedef NTSTATUS (WINAPI *PNtQueryInformationThread) (HANDLE thread,
        THREADINFOCLASS infoclass, PVOID buffer, ULONG buffersize,
        PULONG used);

    static PNtQueryInformationThread NtQueryInformationThread = NULL;

    THREAD_BASIC_INFORMATION tbi;
    NTSTATUS status;
    if (NtQueryInformationThread == NULL) {
        HMODULE ntdll = GetModuleHandleW(L"ntdll.dll");
        if (ntdll)
        {
            NtQueryInformationThread = (PNtQueryInformationThread)GetProcAddress(ntdll, "NtQueryInformationThread");
        }

        if (NtQueryInformationThread == NULL) {
            return 0;
        }
    }

    status = NtQueryInformationThread(thread, ThreadBasicInformation, &tbi, sizeof(tbi), NULL);
    if(status < 0) {
        // Shall we go through all the trouble of setting last error?
        return 0;
    }

    return (DWORD)tbi.ClientId.UniqueProcess;
}

GetProcessIdOfThread在WinXP及之前操作系统的替代实现

原文:http://www.cnblogs.com/through/p/4965336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!