How to get the handle of a window?

Okay, I'm making a trainer for Quake 2, and everything is working, exept I can't get the HWND of the Quake 2 window. I have this:

hwnd = FindWindow(NULL,(LPCWSTR)"Quake 2"); // Yes, the name of the window is "Quake 2", case for case.
if (hwnd != 0)
	cout << " You caught the window!" << endl;

	cout << "open the freakin game..." << endl;

And the output is always this: "open the freakin game..."

I looked up the FindWindow function, and I came across a lot of arguments pertaining to its use.

Can anyone help me get the handle of the Quake 2 window? I don't care whether you use Findwindow or not...

Last edited on
You could try using EnumDesktopWindows() to get the handle of all windows visible on the desktop, and GetWindowText() to check its title.

Hope this helps.
It could also be that he is doing a cast to (LPCWSTR).
Try it without the cast, and if that doesn't work try it like this
hwnd = FindWindow(NULL, _T("Quake 2") );
_T()? Is that a synonym of TEXT()?
I tried doing it without a cast, and I got the error that made me use a cast in the first place: "Cannot convert parameter 2 from const char[8] to LPCWSTR"

Then I tried your method and got '_T': identifier not found. Sounds like I'm missing the library?

I'm still learning C++ and don't know how to use either function mentioned. I tried googling them, and couldn't find an understandable definition or use for each. Can you please expain how to use them? (an example would be greatly appreciated)

Thanks to all who helped,
... LPCWSTR ...

Wow, I missed that one. It is your problem. You can't simply cast an "ANSI" string to a Unicode string. You must compile it as a Unicode string.

As for _T() and _TEXT()
To add to the article's comments, if you #define UNICODE you should also #define _UNICODE. (Both with and without the leading underscore.)

So, if you say
hwnd = FindWindow(NULL,TEXT("Quake 2"));
you should be in business.

If that doesn't work I'll post back with info about the EnumDesktopWindows() stuff.
Topic archived. No new replies allowed.