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 )
{
. . .
}
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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 -
But clearly the notepad process is running.
I have also tried this with other process such as explorer.exe with no positive outcome.
Please advise on this
Thank You
Regards
Joseph
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 )
{
. . .
}