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?
But wondering is there a way from doing it with my code? I have two issues.
- I cannot get the char array called title to print. It just prints blank.
- The other issue is with GetConsoleOriginalTitle not getting the game console application name instead it is getting the Visual Studio console which is not the one I want.
@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.
Hmmm why does GetConsoleTitleA works and can get the HWND from FindWindow.
But when using GetConsoleOriginalTitleA and calling FindWindow the HWND returns null?
> 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.
When using Windows functions, always but always check the return for success/failure.
GetConsoleOriginalTitle() returns:
If the function succeeds, the return value is the length of the string copied to the buffer, in characters.
If the buffer is not large enough to store the title, the return value is zero and GetLastError returns ERROR_SUCCESS.
If the function fails, the return value is zero and GetLastError returns the error code.
If the return value is 0 then the function has failed. You then need to call GetLastError() to obtain the error code relating to the failure and then display it.
In VS, if you then use Tools/Error Lookup you can get a description for the error code.
If the return value is > 0, then this in the number of characters copied to the buffer.
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.
1. Unicode program environment has strong adaptability, and there will be no garbled problems
2. Unicode programs run faster than multi-byte programs. Reason: Windows uses Unicode encoding internally, and multi-byte functions will transcode the parameters and pass them to the Unicode function
3. Multi-byte control background can be used, GUI program is best to use Unicode
Stick with the Visual Studio default of Unicode character set, and explicitly use either the narrow character (A) or wide character (W) version for your text processing.