2

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!

Vancer
  • 55
  • 3
  • 10
  • The answer [here](http://stackoverflow.com/questions/6569405/how-to-get-active-process-name-in-c) might help you. – Catsunami Nov 02 '16 at 22:24
  • @Catsunami I'll try this asap. – Vancer Nov 02 '16 at 22:29
  • @Catsunami Sorry, but it is way different than C++ and i'm quite confused. – Vancer Nov 02 '16 at 22:32
  • What is your question? – Raymond Chen Nov 02 '16 at 22:36
  • Just before calling the `GetWindowThreadProcessId()` function, you have to check if the returned `windowHandle` is not NULL. I have tested under Windows 7 and it works at expected. – J. Piquard Nov 02 '16 at 22:36
  • The code could use some cleanup (e.g., more error checking) but it looks like you have the general idea correct. Are you having problems with the code? If so, what? – Jerry Coffin Nov 02 '16 at 22:37
  • @J.Piquard The thing is, it doesn't return the correct processID for the Calculator App on Windows 8.1, I ran the compiled executable outside of the IDE too, same thing it returns the processID of another running service. Plus yes, it doesn't return NULL, it picks up the Calculator window, I made a previous check using and if statement to see wether it detects it or now depending on this line of code `HWND windowHandle = FindWindowA(NULL, "Calculator");` – Vancer Nov 02 '16 at 22:45
  • @JerryCoffin I answered your question up there too ^^ – Vancer Nov 02 '16 at 22:49
  • Okay guys i fixed the code thanks for the help, turned out i had to declare the `GetWindowThreadProcessId(windowHandle, &processID);` function like `DWORD trpi = GetWindowThreadProcessId(windowHandle, &processID);` then use the variable trpi in the if statement! Thanks for your time. – Vancer Nov 02 '16 at 23:00
  • Did you try to replace `HWND windowHandle = FindWindowA(NULL, "Calculator");` by `HWND windowHandle = GetForegroundWindow();` to check if the ProcessID is correct ? It should be the ProcessId of your testing software. – J. Piquard Nov 02 '16 at 23:01
  • @J.Piquard The problem is now fixed thank you soo much for your time brother. – Vancer Nov 02 '16 at 23:02
  • @Vancer, if the solution is useful, you should be pleased to add a detailed as answer. – J. Piquard Nov 02 '16 at 23:07
  • 1
    *"I want to validate that the window is open..."* If the window is not open then `windowHandle` is zero. – Barmak Shemirani Nov 03 '16 at 03:55
  • It sounds like you have solved the wrong problem. If you need to be notified when any particular window is created, use WinEvents instead. – IInspectable Nov 03 '16 at 08:07

0 Answers0