Problem understanding how to get PId

using the MSDN library i came up with this code, however it is not working and i can't really understand why :<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() 
{


HWND WindowHandel = FindWindow(0, "Calculator");

LPDWORD proccesID;
GetWindowThreadProcessId(WindowHandel, proccesID);


std::cout << proccesID << std::endl;

system("pause");
return 0;
}
Last edited on
Probably should be:
1
2
DWORD proccesID;
GetWindowThreadProcessId(WindowHandel, &proccesID);
The error comes from this code
 
HWND WindowHandel = FindWindow(0, "Calculator");



and says
error C2664: 'FindWindowW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'


adding "&" did not change anything :<
error C2664: 'FindWindowW' : cannot convert parameter 2 from 'const char [11]' to 'LPCWSTR'

This error occurs when there is a discrepency between the the type of build (UNICODE or MBCS) and the string literals you are using.

We will take the short route and use the FindWindowA function.

adding "&" did not change anything :<

You probably misunderstood the error I was trying to point out.



Does this work for you??:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
   HWND WindowHandel = FindWindowA(0, "Calculator"); //use FindWindowA function.

    DWORD proccesID = 0; //use a DWORD not LPDWORD
   
 GetWindowThreadProcessId(WindowHandel, &proccesID); //pass the address of the DWORD


    std::cout << proccesID << std::endl;
}

Last edited on
Yeah it does, so what is the difference between FindWindow function and FindWindowA ?
And why add an “&” in front of the variable processID?
Yeah it does, so what is the difference between FindWindow function and FindWindowA ?


WinAPI functions that take strings come in 3 flavors:

TCHAR flavor (FindWindow)
char flavor (FindWindowA)
wchar_t flavor (FindWindowW)

TCHAR is #defined as either chars or wchar_t depending on your project settings. But for the purposes of writing stable, consistent code, you should consider it its own type.

Since you are giving it a char array in the form of a string literal, you want to use the char version of the function (FindWindowA). To use the TCHAR version, you would want to give it a TCHAR string:

1
2
3
FindWindowA(0, "Calculator");  // <- char string, 'A' version of the function
FindWindowW(0, L"Calculator"); // <- wchar_t string, 'W' version of the function
FindWindow(0, TEXT("Calculator")); // TCHAR string, 'normal' version of the function 


This is true of practically all WinAPI structs/functions.


And why add an “&” in front of the variable processID?


Because GetWindowThreadProcessId needs a pointer to a DWORD. The & symbol is the "address of" operator, which gives you a pointer to the processID variable.
Disch has said pretty much what I was going to say.

The only other thing I was going to mention is something about
the Microsoft Windows API naming convention (the way that MS names
it variables and functions etc.. in the API).

The prototype for the GetWindowThreadProcessIs looks like this:
1
2
DWORD WINAPI GetWindowThreadProcessId(   HWND hWnd,  LPDWORD lpdwProcessId
);


If you see LP at the front of the typename it means Long Pointer.
So LPDWORD is a Long Pointer to a Double Word.

In this day and age, windows is all 32bit or 64bit Flat memory model - so the actual Long part of the name no longer means much - so we just say Pointer.
Topic archived. No new replies allowed.