0

Before I start,

Please do understand that this question is not a duplicate. There are questions out there with the same header :

Convert LPCSTR to LPCWSTR

However, I did some research before asking this question and came across ATL or Active Template Library.

However the compiler I use, doesn't come with ATL, which does not allow me to convert from LPCSTR to LPCWSTR.

Also, to understand why I need to convert these two datatypes, the reason is that I am working on a project, which is actually a Console Game Engine.

In case you don't know, to rename a console window, you need to use SetConsoleTitle(LPCWSTR winName)

And at first, I used LPCWSTR datatype as an argument for my function. But, when I tried another function, which is actually a drawing function, this is what happens:

A screenshot of the console window

This combination of UNICODE characters that you see here, is a function for drawing a rectangle on the console. The rectangle correctly displays, however, there is some text, or should I say, the Console Title itself, displayed across the rectangle.

I did not add any such thing of displaying the console title on the rectangle, nor did I mess up my code in any way. I burned through my code a few times, and there was nothing wrong, however, somehow, the console title is displayed on the rectangle.

I somehow think that this is related to the LPCWSTR datatype, because when I tried the function, SetConsoleTitle() manually

Help me find a solution, will you?

  • MultiByteToWideChar ? – Alexander Nov 03 '21 at 10:18
  • The first and most up-voted hit in a SO search has an answer that doesn't use ATL. And the type of `SetConsoleTitle`'s parameter depends on whether you're building for unicode or not. You can explicitly use `SetConsoleTitleA` to use a narrow string. – molbdnilo Nov 03 '21 at 10:19
  • Can you provide a [mcve] demonstrating your attempt? It's difficult to believe that setting the console title would break other parts of your code unless you're using the same variable and have something incorrect in your code. The [other answer](https://stackoverflow.com/a/8044550/920069) on the question you recently commented on does not use ATL and should work fine. – Retired Ninja Nov 03 '21 at 10:20
  • `when I tried another function, which is actually a drawing function, this is what happens` - you should post the code, probably you are have uninitialized memory somewhere or buffer overflow or just accidentaly reuse the buffer incorrectly. no standard function would add the window title to the converted text on purpose. – dewaffled Nov 03 '21 at 10:20
  • It sounds like there are bugs in your program, and that your assumption that `SetConsoleTitle` has anything to do with them might be mistaken. – molbdnilo Nov 03 '21 at 10:21
  • BTW, the sentence that ends with "because when I tried the function, SetConsoleTitle() manually" seems to have lost its proper ending. – molbdnilo Nov 03 '21 at 10:21

1 Answers1

0

Since the title is usually constant, you can simply call

SetConsoleTitle(L"My Title"); //or
const wchar_t* title = L"My Title";
SetConsoleTitle(title);

In general, use MultiByteToWideChar to convert ANSI or UTF8, to UTF16

#include <iostream>
#include <string>
#include <windows.h>

std::wstring u16(const char* in, int codepage)
{
    if (!in) return std::wstring(L"");
    int size = MultiByteToWideChar(codepage, 0, in, -1, NULL, 0);
    std::wstring ws(size, L'\0');
    MultiByteToWideChar(codepage, 0, in, -1, ws.data(), size);
    return ws;
}

int main()
{
    const char* str = "My Title"; //<- ANSI string
    SetConsoleTitle(u16(str, CP_ACP).c_str());
    std::string s;
    std::cin >> s;

    str = reinterpret_cast<const char*>(u8"Ελληνικά"); //<- UTF8
    SetConsoleTitle(u16(str, CP_UTF8).c_str());
    std::cin >> s;

    return 0;
}

Note that the title changes back when program exits.

Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • This answer, is helpful, however, as @molbdnilo, said, We can Use `SetConsoleTitleA(LPCSTR lpConsoleTitle)` which is also another helpful solution. – IneoSenigupta Nov 04 '21 at 06:28