2

我已经设法通过使用GetThreadId(HANDLE)boost::thread中的native_handle()中获取 Windows ThreadId 。遗憾的是,该调用在 Windows XP 上不可用,在四处搜索后,我找到了通过 WINAPI 的Thread32First()Thread32Next()函数遍历所有线程来为 XP 提供 als 后备支持的解决方案。

这确实有效,但我的问题是我目前只能识别我的进程的线程......我现在不知道如何将一侧的native_handle() / HANDLE与循环遍历中的适当THREADENTRY32 匹配。

THREADENTRY32 te32;
//...
do { 
    if( te32.th32OwnerProcessID == GetCurrentProcessId() ) {
        DWORD threadId = te32.th32ThreadID;
        printf( "\n     THREAD ID      = 0x%08X", te32.th32ThreadID ); 
    }
} while( Thread32Next(hThreadSnap, &te32 ) );

任何人都可以帮助我吗?如何在 WindowsXP 上将 boost::thread->native_handle() 转换为 ThreadId?

非常感谢!

4

1 回答 1

2

将循环中的每个线程 ID 传递给,OpenThread()直到找到匹配的HANDLE. 例如:

HANDLE hBoostThread = ...; // from boost::thread->native_handle()
DWORD dwBoostThreadID = 0;

THREADENTRY32 te32;
//...
do
{ 
    if( te32.th32OwnerProcessID == GetCurrentProcessId() )
    {
        HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
        if (hThread != NULL)
        {
            if (hThread == hBoostThread)
            {
                CloseHandle(hThread);
                dwBoostThreadID = te32.th32ThreadID; 
                break;
            }
            CloseHandle(hThread);
        }
    }
}
while( Thread32Next(hThreadSnap, &te32 ) );

为了更好地衡量,您可以将其包装在一个函数中,您可以在GetThreadId()本机不可用时调用该函数,以便您的代码不需要知道差异,例如:

DWORD WINAPI MyGetThreadId(HANDLE Thread)
{
    THREADENTRY32 te32;

    HANDLE hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (hThreadSnap == INVALID_HANDLE_VALUE)
        return 0;

    if (Thread32First(hThreadSnap, &te32))
    {
        do
        { 
            HANDLE hOpenThread = OpenThread(THREAD_ALL_ACCESS, FALSE, te32.th32ThreadID);
            if (hOpenThread != NULL)
            {
                if (hOpenThread == Thread)
                {
                    CloseHandle(hOpenThread);
                    CloseHandle(hThreadSnap);
                    return te32.th32ThreadID; 
                }

                CloseHandle(hOpenThread);
            }
        }
        while( Thread32Next(hThreadSnap, &te32 ) );
    }

    CloseHandle(hThreadSnap);
    return 0;
}

typedef DWORD (WINAPI *LPFN_GTID)(HANDLE);

LPFN_GTID lpGetThreadId = (LPFN_GTID) GetProcAddress(GetModuleHandle("kernel32"), "GetThreadId");
if (!lpGetThreadId)
    lpGetThreadId = &MyGetThreadId;

DWORD dwThreadID = lpGetThreadId((HANDLE) boost::thread->native_handle());

话虽如此,更好的选择是直接查询目标线程本身以获取其自己的 ID,而不是尝试手动寻找它:

typedef long (WINAPI *LPFN_NTQIT)(HANDLE thread, int infoclass, void *buf, long size, long *used);

typedef struct _THREAD_BASIC_INFORMATION
{
    ULONG ExitStatus;
    void* TebBaseAddress;
    ULONG UniqueProcessId;
    ULONG UniqueThreadId;
    ULONG AffinityMask;
    ULONG BasePriority;
    ULONG DiffProcessPriority;
} THREAD_BASIC_INFORMATION;

DWORD WINAPI MyGetThreadId(HANDLE Thread)
{
    DWORD dwThreadId = 0;

    HMODULE hLib = LoadLibrary("ntdll.dll");
    if (hLib != NULL)
    {
        LPFN_NTQIT lpNtQueryInformationThread = (LPFN_NTQIT) GetProcAddress(hLib, "NtQueryInformationThread");

        if (lpNtQueryInformationThread != NULL)
        {
            THREAD_BASIC_INFORMATION tbi = {0};
            ULONG used = 0;

            if (lpNtQueryInformationThread(Thread, 0, &tbi, sizeof(tbi), &used) == 0)
                dwThreadId = tbi.UniqueThreadId;
        }

        FreeLibrary(hLib);
    }

    return dwThreadId;
}
于 2013-05-23T17:37:13.870 回答