I was trying to get the process ID of 'Calculator' on C++ using this code.
#define _WIN32_WINNT 0x0501
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
int wait;
//Handle - Window name
HWND windowHandle = FindWindowA(NULL, "Calculator");
//Getting ProcessID
DWORD processID;
GetWindowThreadProcessId(windowHandle, &processID);
if(processID){
cout << "Process ID is: " << processID << endl;
cin >> wait;
} else {
cout << "Unable to find the process id !" << endl;
cin >> wait;
}
return 0;
}
Okay, so i know what you are thinking, that I'm using GetWindowThreadProcessId(), how do I expect to get the actual processID, well I researched a bit and that was all I found.
I want to validate that the window is open by checking the handle from FindWindow using an IF , then if available get it's processID.
Thanks!
THE PROBLEM IS NOW SOLVED
What I was doing is that to check if it's getting the processID I would cout << processID << endl;
which is wrong and here is why. The whole line of code GetWindowThreadProcessId(windowHandle, &processID);
is what grabs the processID, so to cout
the ProcessID we need to store the whole function / line of code in a DWORD. So what I did is edited this line GetWindowThreadProcessId(windowHandle, &processID);
to this:
DWORD actualProcID = GetWindowThreadProcessId(windowHandle, &processID);
Biggest of them all the problem was in the if condition. Here is how it should like (it works):
DWORD processID;
DWORD actualProcId = GetWindowThreadProcessId(windowHandle, &processID);
if(actualProcId){ //We test the DWORD we declared.
cout << "Process ID is: " << processID << endl; // If true, we cout the process id
cin >> wait;
} else {
cout << "Unable to find the process id !" << endl;
cin >> wait;
}
Thank you for your time!