Unicode strings containing unicode characters are always empty

Viewed 116

I am compiling with -municode -DUNICODE -D_UNICODE flags and using _tmain to enable unicode support.

But when I perform operations on any TCHAR arrays containing unicode characters, the strings ends up truncated at wherever that character was.

For example:

TCHAR buffer[255];
wcscpy(buffer, L"test-");
wcscat(buffer, L"Азәрбајҹан");
/* buffer now contains "test-" */

My actual use-case is retrieving a username, which, if it contains special chars, will end up empty wether it's from GetEnvironmentVariable, GetUsername or even a hardcoded string like above.

EDIT:

Here is a complete minimal reproducible example:

Compiled with gcc -o error.exe error.c -municode under:

gcc.exe (Rev3, Built by MSYS2 project) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Code:

#define _UNICODE
#define UNICODE

#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>

int _tmain(int argc, TCHAR* argv[]) {
    FILE* fp;
    TCHAR buffer[255];

    _tcscpy(buffer, _T("test-"));
    _tcscat(buffer, _T("Азәрбајҹан"));
    _tprintf(_T("Length: %d, Content: %ls\n"), _tcslen(buffer), buffer);

    fp = _tfopen(_T("test.txt"), _T("w"));
    _ftprintf(fp, _T("%ls"), buffer);
    fclose(fp);
    return 0;
}

This example prints 15 test- and puts test- into test.txt.

1 Answers

For wide characters I usually use wchar_t.

If that is an option you can use something like:

#include <tchar.h>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>

int _tmain() {

#ifdef UNICODE
    _setmode(_fileno(stdin), _O_WTEXT);
    _setmode(_fileno(stdout), _O_WTEXT);
#endif

    wchar_t buffer[255];

    wcscpy(buffer, L"test-");
    wcscat(buffer, L"Азәрбајҹан");
    wprintf(L"%s\n", buffer);

    return 0;
}

Output:

With VS 2019 with MSVC and Use Unicode Character Set enabled:

enter image description here

With gcc version 9.2.0 (tdm64-1):

enter image description here

Related