Home > Mobile >  "cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc&quo
"cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc&quo

Time:12-11

So, I'm trying to use "Windows.h" to output unicode characters on console with wchar_t*, and I found on internet that this is the code I'm supposed to use:

wchar_t *screen = new wchar_t[nScreenWidth*nScreenHeight];
    HANDLE hConsole = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE, 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
    SetConsoleActiveScreenBuffer(hConsole);
    DWORD dwBytesWritten = 0;

   while(1) {
      screen[nScreenWidth * nScreenHeight - 1] = '\0';
       WriteConsoleOutputCharacter(hConsole, screen, nScreenWidth * nScreenHeight, { 0,0 }, &dwBytesWritten);

   
   }

However, when I try to compile, I get this error: cannot convert 'wchar_t*' to 'LPCSTR' {aka 'const char*'}gcc

I'm compiling with mingw64 and I'm using this command:

            "-std=c  20",
            "-g",
                "${workspaceFolder}/*.cpp",
            "-L",
                "${workspaceFolder}\\src\\lib",
            "-I",
                "${workspaceFolder}\\src\\include",
            "-l",
                "mingw32"   ,"-l",
                "SDL2main"  ,"-l",
                "SDL2",
            "-o",
                "main",

(I'm technically also using SDL2, but I don't need it at the moment)

Thank you!!

CodePudding user response:

WriteConsoleOutputCharacter is a macro of WriteConsoleOutputCharacterW or WriteConsoleOutputCharacterA depends on the charset compiler option.

WriteConsoleOutputCharacterW accepts LPCWSTR (a.k.a const WCHAR* a.k.a const wchar_t *, or const unsigned short * if wchar_t is not supported by the compiler) as parameter.

WriteConsoleOutputCharacterA accepts LPCSTR (a.k.a const char *) as parameter.

So, checkout your compiling settings, and make sure which version you are actually calling, and define your screen as the right type.

If not sure or you want support both, you could use TCHAR string/buffer instead. TCHAR is a macro of WCHAR or char depends on the same compiler option. And LPCSTR/LPSTR is macros of the pointer types.

  • Related