-2

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

H-005
  • 467
  • 3
  • 15
  • 4
    Maybe using `wchar_t`. – anastaciu Apr 20 '21 at 17:44
  • Your question would be better if you showed a 10 line program that someone could test. Currently we have to guess what bug you have. – drescherjm Apr 20 '21 at 17:49
  • [Relevant docs](https://learn.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp?view=msvc-160) – Cory Kramer Apr 20 '21 at 17:53
  • 1
    Note: You misunderstand `TEXT`. It is a macro that turns a string literal into a wide character string literal (`TEXT("string)"` becomes `L"string"`) at compile time if `UNICODE` is defined. Otherwise it does nothing. It cannot transform a string at runtime. `TEXT(test)` will become `Ltext` and probably result in an odd-looking compiler error. – user4581301 Apr 20 '21 at 17:54
  • 1
    The TEXT macro defined in `winnt.h` is only designed to use string literals, not variables. To define unicode strings, use either `wchar_t` instead of `char` or the correlating WinAPI typedef `WCHAR`. One additional advice is that you always specify if you want to use the Ansi or Unicode version of the WinAPI function instead of the macro to prevent possible mistakes due to configuration errors. – Louis Bernard Apr 20 '21 at 17:57
  • I realized now that TEXT() is a macro (or, better said, I knew TEXT() was a macro, but for some reason it didn't click that I cannot use it without having an actual string literal – H-005 Apr 20 '21 at 18:04
  • 1
    @H-005 "*This worked on CodeBlocks using MinGW*" - then there is something seriously wrong with CB/MinGW if it allows this. "*I don't see why it wouldn't work in VS*" - because it is not supposed to work, for the reason already stated (`TEXT()` only works on literals, not on variables). – Remy Lebeau Apr 20 '21 at 19:49
  • @rem `TEXT` is **meant** to be used on string literals, but it still 'works' on anything, as long as neither the `UNICODE` nor `_UNICODE` preprocessor symbols are defined. Incidentally, Code::Blocks doesn't. – IInspectable Apr 20 '21 at 21:29

2 Answers2

0

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.

IInspectable
  • 46,945
  • 8
  • 85
  • 181
-3

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>
Hernando Abella
  • 286
  • 2
  • 13