函数-线程遍历

/// <summary>
/// 线程遍历
/// </summary>
/// <returns></returns>
BOOL EnumThread()
{
    THREADENTRY32 te32 = { 0 };
    te32.dwSize = sizeof(THREADENTRY32);
    HANDLE hThreadSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);// 获取全部线程快照
    if (INVALID_HANDLE_VALUE == hThreadSnap)
    {
        ShowError(L"CreateToolhelp32Snapshot");
        return FALSE;
    }
    BOOL bRet = ::Thread32First(hThreadSnap, &te32);// 获取快照中第一条信息
    while (bRet)
    {
        printf("[%d]\t", te32.th32OwnerProcessID);// 显示 Owner Process ID
        printf("[%d]\n", te32.th32ThreadID);// 显示 Thread ID
        bRet = ::Thread32Next(hThreadSnap, &te32);// 获取快照中下一条信息
    }
    ::CloseHandle(hThreadSnap);// 关闭句柄
    return TRUE;
}

 

posted @ 2021-12-27 17:17  梦之心  阅读(53)  评论(0编辑  收藏  举报