I'm trying to use the Windows API in Visual Studio 2019. The problem is that when I'm trying to create a wide string, using something like this: L"Hello World!"
, I get an error: 'L': identifier not found
Then, I tried using the TEXT()
function, which, from what I understand, is supposed to convert a string to a wide string:
char test[1024]; /// contains some text
SetWindowText(DMG_LABEL, TEXT(test));
Here I get this error: 'Ltest': undeclared identifier
This worked on CodeBlocks using MinGW, so I don't see why it wouldn't work in VS. (Tested this in C++, but I'm pretty sure in C it's the same thing).
What exactly am I doing wrong?
EDIT: Thanks to @anastaciu, I didn't realize that I wasn't using wchar_t
This worked in Code::Blocks, because Code::Blocks sucks.
Code::Blocks still thinks, in 2021, that using codepage encoding were the proper default. As a result, the TEXT
macro expands to nothing.
With an IDE like Visual Studio, that defaults to using Unicode, the TEXT
macro expands to a token pasting sequence, slapping L
to its argument, whatever that argument is. It is meant to be used with string literals, and in that case it does what one would expect. If you use it with a variable name, then, well, it still slaps an L
in front of it.
So instead of test
, the compiler now gets to see Ltest
. It either uses the symbol named Ltest
, or produces an error when it cannot find it.
What exactly am I doing wrong?
You are using a function-like preprocessor symbol, that's meant to be used with string literals, on something that isn't a string literal.
project properties->Advanced->Character Set = Use Unicode Character Set
or before adding <windows.h>
#if !defined(_UNICODE)
#define _UNICODE
#endif
#if !defined(UNICODE)
#define UNICODE
#endif
#include <windows.h>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.