Home > other >  Using SetConsoleTitleW(LPCWSTR) to set the console title, but console title resulting in random UNIC
Using SetConsoleTitleW(LPCWSTR) to set the console title, but console title resulting in random UNIC

Time:01-30

I am working on a Console Application project.

I wanted to change the title of the console to something else, so I used SetConsoleTitleW(LPCWSTR), which accepts wchar_t string input.

But when I try using the function in the following code:

#include <iostream>
#define UNICODE
#include <windows.h>

int main()
{
  SetConsoleTitleW(L"Bank Application");
  return 0;
}

My code is very straightforward, as I have just started the project with a simple WINAPI function.

However, this generates the following

console

Which consists of random UNICODE characters in the title of the console.

Any issue with the buffer? My code is very simple and straightforward, as far as I can see.

I'm using Embarcadero DevC with TDM-GCC 9.2.0 64-bit compiler, Release.

EDIT : Using SetConsoleA(LPCSTR) produces the same thing.

CodePudding user response:

From microsoft documentation https://docs.microsoft.com/en-us/windows/console/setconsoletitle

When the process terminates, the system restores the original console title.

Your screenshot shows your process has terminated. Why your console title shows those characters has nothing to do with your program.

CodePudding user response:

You screenshot suggests that you are not using a true Windows console. SetConsoleTitle is only intended to be used in a true Windows console, and using it with any other terminal emulator will just have undefined results.

I have slightly changed you code to

...
SetConsoleTitleW(L"Bank Application");
Sleep(5000);
return 0;
...

in order to have 5 seconds to see what happens. Then I tried it inside a plain console and correctly saw the changed title. But I also tried it from a MSYS2 terminal... and nothing happened at all.

I could not identify you terminal application from its icon, but I would still bet a coin that it is the culprit

  •  Tags:  
  • Related