Ⅱ. サンプルプログラム
#include <Windows.h> #include <tlhelp32.h> #include <iostream> #include <vector> #include <algorithm> std::vector<uint32_t> GetAllThreadIds(uint32_t processId) { std::vector<uint32_t> threadIds; THREADENTRY32 threadEntry32 = { 0 }; auto hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, processId); if (hThreadSnap != INVALID_HANDLE_VALUE) { threadEntry32.dwSize = sizeof(THREADENTRY32); if (!Thread32First(hThreadSnap, &threadEntry32)) { CloseHandle(hThreadSnap); } else { do { if (threadEntry32.th32OwnerProcessID == processId) { threadIds.emplace_back(threadEntry32.th32ThreadID); } } while (Thread32Next(hThreadSnap, &threadEntry32)); CloseHandle(hThreadSnap); } } return threadIds; } int main() { auto pid = GetCurrentProcessId(); auto threadIds = GetAllThreadIds(pid); std::sort(threadIds.begin(), threadIds.end()); for (auto threadId : threadIds) { std::cout << "threadId: " << threadId << std::endl; } return 0; }
実行結果