I would like to know how to change the console title in C++ using a string as the new parameter.
I know you can use the SetConsoleTitle function of the Win32 API but that does not take a string parameter.
I need this because I am doing a Java native interface project with console effects and commands.
I am using windows and it only has to be compatible with Windows.
I would like to know how to change the console title in C++ using
Share
The
SetConsoleTitlefunction does indeed take a string argument. It’s just that the kind of string depends on the use of UNICODE or not.You have to use e.g. the
Tmacro to make sure the literal string is of the correct format (wide character or single byte):If you are using e.g.
std::stringthings get a little more complicated, as you might have to convert betweenstd::stringandstd::wstringdepending on theUNICODEmacro.One way of not having to do that conversion is to always use only
std::stringifUNICODEis not defined, or onlystd::wstringif it is defined. This can be done by adding atypedefin the"stdafx.h"header file:If your problem is that
SetConsoleTitledoesn’t take astd::string(orstd::wstring) it’s because it has to be compatible with C programs which doesn’t have the string classes (or classes at all). In that case you use thec_strof the string classes to get a pointer to the string to be used with function that require old-style C strings:There’s also another solution, and that is to use the explicit narrow-character "ASCII" version of the function, which have an
Asuffix:There’s of course also a wide-character variant, with a
Wsuffix: