I want to get the window handle of a game console application by name.
I have tried researching and found on MSDN if I want to get the title of the console application I can use the GetConsoleOriginalTitle function but when I store the return value into a char array and try to print it out it only shows blank characters.
Can anyone tell me what I’m doing wrong? How can I print out the console application title name?
@copypasta, depending on your compiler you may be creating a Unicode version of your program. A lot of the WinAPI functions have three(!) different versions available. For example GetConsoleOriginalTitle() is actually a macro, an alias, that can call either GetConsoleOriginalTitleA() (ANSI, retrieves a char array) or GetConsoleOriginalTitleW(unicode(), retrieves a wchar_t array). Which version is actually used depends on the definition of the UNICODE preprocessor constant. The Visual Studio IDE usually sets this, modern versions of VS have it defaulted to the Unicode character set.
Not all WinAPI functions have different ANSI/Unicode versions.
I don't know what your compiler is, if it is Visual Studio that defaults to Unicode. ANSI is Win9X.
If there is ANSI and Unicode versions, prefer explicitly calling whatever version is appropriate for the character set you want to support.
std::cout supports char arrays, so call the A version. That is what JLBorges did, he explicitly called the A version of WinAPI functions.
I have tried running the binary instead of inside visual studio and the text shows up in console. But why when running it inside visual studio I get blank text? I have been trying to figure this out and don’t know how to make the text not blank.
> why when running it inside visual studio I get blank text?
When running from inside Visual Studio, the original title of the console window would be ...VsDebugConsole.exe; the title is changed to the actual title once the program starts running and FindWindow won't find the window with the original title.
> I have been trying to figure this out and don’t know how to make the text not blank.
To run from inside Visual Studio, instead of retrieving the original title of the console window,
retrieve its current title.
> why does GetConsoleTitleA works and can get the HWND from FindWindow.
> But when using GetConsoleOriginalTitleA and calling FindWindow the HWND returns null?
FindWindow looks for a top level window with the specified title. So it requires the current window title (from GetConsoleTitleA), not an old title (from GetConsoleOriginalTitleA) which may have been replaced.
Why? Multi-byte is essentially ANSI characters. Explicitly using the ANSI/Multi-byte WinAPI function does work regardless of what the character option is set in the project's properties, the code JLBorges showed compiles and runs fine with the default Unicode character set.
I always use the default Unicode character set and never have a problem because I explicitly use the A or W version of WinAPI functions when dealing with text in my WinAPI code.
The programs could call the W versions of the WinAPI function, use wchar_t C strings and output to std::wcout.
wchar_t has a Windows data type alias handy, WCHAR.
Unicode is 2 bytes in size and uses UTF-16, multi-byte can be one or two bytes depending on the character and the locale.
Multi-byte is not recommended for new projects, MBCS was an MS invention of extending ANSI that attempted to provide support for non-European characters before the Unicode standard was adopted.
Always explicitly call the A/W version of any WinAPI function when available. Not every WinAPI function deals with text so no need to have two different versions.
One thing to note if using wchar_t/WCHAR, WinAPI functions that fill a C string buffer have a potential buffer overrun issue. ANSI/MB is 1 byte in size, Unicode is 2. You need to divide the size of the C string by the number of bytes in the character set.