My C++ application to Get the thread count and thread IDs of a process, is not working

Joseph RW 60 Reputation points
2023-07-20T03:33:43.38+00:00

Hi Microsoft team,

I have written this C++ application to get the thread count and the thread IDs of a running process, I am supplying the notepad.exe argument to one of the functions, to get the Notepad Process ID and through it I am trying to enumerate the thread count and thread IDs.

This is the application source code,

#include <windows.h>
#include <TlHelp32.h>
#include <iostream>
#include <string>

void printThreadIDs(DWORD processID) {
    HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
    THREADENTRY32 te32;

    hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (hThreadSnap == INVALID_HANDLE_VALUE) {

        std::wcout << L"Error in getting thread snapshot\n";
        return;
    }

    te32.dwSize = sizeof(THREADENTRY32);

    if (!Thread32First(hThreadSnap, &te32)) {
        std::wcout << L"Error in getting first thread\n";
        CloseHandle(hThreadSnap);
        return;
    }

    int threadCount = 0;
    do {
        if (te32.th32OwnerProcessID == processID) {
            ++threadCount;
            std::wcout << L"THREAD ID: " << te32.th32ThreadID << L"\n";
        }
    } while (Thread32Next(hThreadSnap, &te32));

    std::wcout << L"THREAD COUNT: " << threadCount << L"\n";

    CloseHandle(hThreadSnap);
}

DWORD getProcessID(const std::wstring& processName) {
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;

    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE) {
        std::wcout << L"Error in getting process snapshot\n";
        return 0;
    }

    pe32.dwSize = sizeof(PROCESSENTRY32);

    if (!Process32First(hProcessSnap, &pe32)) {
        std::wcout << L"Error in getting first process\n";
        CloseHandle(hProcessSnap);
        return 0;
    }

    do {
        if (std::wstring(pe32.szExeFile) == processName) {
            CloseHandle(hProcessSnap);
            return pe32.th32ProcessID;
        }
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);
    return 0;
}

int wmain() {
    DWORD processID = getProcessID(L"notepad.exe");

     printf("ID: %x", processID);

    if (processID == 0) {
        std::wcout << L"Notepad process not found or unable to open\n";
        return 1;
    }
    printThreadIDs(processID);


    std::string input;

    while (true) {
        std::getline(std::cin, input);

        if (input == "stop") {
            break;
        }

        std::cout << "Enter key pressed!\n";

        printThreadIDs(processID);

    }

    return 0;
} 


The purpose of the while loop is this:

I want to generate more notepad threads, by opening the Replace or Find dialog boxes, and when I hit the ENTER key in the application shown above I will see the newly generated Thread count and IDs

But after running the application this is what I see -

Screenshot_20230227_051846

But clearly the notepad process is running.

Screenshot 2023-07-19 200253-1

I have also tried this with other process such as explorer.exe with no positive outcome.

Please advise on this

Thank You

Regards

Joseph

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2023-07-20T04:16:05.5066667+00:00

    According to Task Manager (Details tab), the name is “Notepad.exe”, therefore use getProcessID( L"Notepad.exe" ).

    Or use some case-insensitive comparison, for example:

    if( lstrcmpiW( pe32.szExeFile, processName.c_str() ) == 0 )
    {
       . . .
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.