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


    还是学习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;
    }

  • 相关阅读:
    Sass开发环境搭建
    三款Javascript SPAs框架资料整理和总结
    Web纯前端“旭日图”实现元素周期表
    能在多种前端框架下使用的表格控件
    控件使用经验-MVP模式+控件封装
    最好的Angular2表格控件
    跨平台开发的两种方法及其对比
    是时候 UWP 了 !
    你的系统也可以拥有“数据透视表”功能!
    Xamarin 免费了,你能做什么?
  • 原文地址:https://www.cnblogs.com/through/p/4965336.html
Copyright © 2020-2023  润新知