Getting a ProcessID from a FindWindow Handle - C++

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!

14 Related questions 71 How to get main window handle from process id? 25 how to get process handle from process id? 2 Get the process id from a window handle without GetWindowThreadProcessId (c#.net 4.0) Related questions 71 How to get main window handle from process id? 25 how to get process handle from process id? 2 Get the process id from a window handle without GetWindowThreadProcessId (c#.net 4.0) 0 GetWindowThreadProcessId can not find my window handle, even though its process ID exists on Window 2 Get the window handle of an externally running program through C++ 3 Getting Process Information from Process Handle 39 Find process id by window's handle 4 C++ Can't get process id (windows) 1 How to find PID of window containing X 0 How to find a Window Handle on ProcessID C# Load 7 more related questions Show fewer related questions Reset to default

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like