Guys I need your help, I'm stuck cluelessly what I'm doing wrong. I read trough the basic documentation how to create win32 apps, wanted to try out the code that is given in the documents but my compiler(gcc) don't want to cooperate. I'm using Visual Studio Code, with gcc, standard settings, hopefully importing everything, and I always get the massage that "Can't convert from const wchar_t to LPCWSTR" when it comes to the CLASS_NAME.
#Win32 API: Can't convert from const wchar_t to LPCWSTR
70 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
use the W versions of the APIs explicitly
i.e., WNDCLASSEXW, RegisterClassW, CreateWindowExW, etc.
the generic versions are macros that expand to either the W or A version depending on whether UNICODE is defined or not, and you seem to no be defining it for some reason.
you can notice that by the fact that the names of these APIs are blue instead of yellow, which tells you they're macros.
you want to avoid the macro versions
First question: I thought with simply doing #define UNICODE I do everything that needs to be done. How do I do it correctly?
well, you aren't doing #define UNICODE though?
note the error message
you're calling CreateWindowExA
which is why you get the error, that one expects a const char*, not a const wchar_t*
I do. I mean hopefully Im not blind.
are you doing so after including windows.h?
you need to define it before including windows.h
my recommendation would be to not rely on #define UNICODE and just explicitly use the W API. the whole UNICODE an TCHAR stuff is really obsolete. it existed for the brief moment in time where people needed to write code that could be compiled for both unicode and ansi versions of windows. the last version of windows for which this had even a shred of relevance was windows 98…
so unless you need to support windows 98 for some reason, you shouldn't be doing this imo.
and even windows 98 iirc later got a compat layer that would understand unicode
so yeah dunno
especially if you're not going to also be using the TCHAR stuff, your code isn't gonna work correctly without #define UNICODE anyways.
Okey doing the include before windows.h defintely helped, no errors but still dont work so I will change everything to W.
what error you get now?
note that not all windows APIs have A and W versions, only the ones that do stuff with non-ASCII strings. you can spot the difference based on how your IDE colors the names. if an API function is painted blue, you should look for the W version. if it's painted yellow already without any suffix, you're already using the correct version. e.g.: TranslateMessage vs DispatchMessage in your code up there.
basically, windows.h is full of
#ifdef UNICODE
#define CreateWindowEx CreateWindowExW
#else
#define CreateWindowEx CreateWindowExA
#endif
yeah um
Okey this sounds pretty helpful. Even my 2 braincells can understand.
i guess gcc needs to somehow be told to use the unicode winmain or smth?
i'm not 100% sure tbh, i'm not sure why you'd be using gcc for targeting windows.
the simplest option is to just use visual studio, not vscode. you can find a guide here: https://discord.com/channels/331718482485837825/1165492293810257920
in your case, you'd need to create a win32 application project instead of a console application project. everything else would be the same as in the guide.
(just also make sure to untick the precompiled headers option in the win32 application wizard, you do not want your project to use pch)
the the #define UNICODE stuff will also be unnecessary because visual studio already does that by default.
Okey I have to ask a few dozen stuff because as you can see I'm pretty new all of this stuff.
sure
First of all I went into this blank, knowing that I want to do some UI stuff bc I feel I learned the basics of c++, and the terminal stuff. Made few basic games, and calculators, some banking simulators etc. I know SDL is out there, but bc I use mainly windows (at least so far) I went with the win32 API. I don't know if I have to configure my compiler to do the win32 stuff, but what I got from what you said is that gcc doesn't like win32 at this point and cant compile it properly.
Besides changing IDE which would be pretty uncomfortable what can I do?
well, gcc does not have first-class windows support. it's possible to use gcc to target windows, if you know what you're doing and are prepared to put up with it. i'm just not sure why you'd go through all that. msvc is the native compiler for windows. all the documentation will be assuming that you're using msvc, prebuilt binaries you'll find will almost exclusively be for msvc, etc.
msvc can be used from the command line, vscode can be configured to use msvc. i'm just, once again, not sure why you'd do that when you could use visual studio instead, which will be much simpler, and is also a far better IDE for C++.
like, of the big three compilers, gcc is probably the least well suited for targeting windows.
using gcc to target windows is kinda like using msvc to target linux ^^
The why I use gcc is bc that's what Im familiar with. Or at least thats what I know exist besides clang. I dont know other compilers bc I didn't have to use any other so far. I didn't see anybody talk out the fact that gcc is pretty bad for windows stuff.
it's not bad, it's just not exactly got first-class support for windows
But its pretty good to know. I will definitely read about compilers know.
like, you can look up what flags g++ would need to make wWinMain work. i would assume there's a way, i just don't know what it is because i sure don't use g++ on windows xD
And I use VS Code bc thats what I started to use when I got interested in programing. Its modular and light weight not bloated that much and looked good for someone like me who is soo knew to all of this stuff that even the setup would confuse the shit out of me
Is gcc some linux stuff?
yes, gcc is the GNU Compiler Collection
the primary issue with gcc on windows is that it isn't fully compatible with the windows ABI. in particular, it uses different name mangling, and it insists on keeping the GNU workflow even on windows. so the way building works with gcc is different from how it's normally done on windows. precompiled binaries generally won't work with gcc because they will be compiled for the msvc ABI. to use gcc, you'll generally need something like mingw or msys2, which are basically GNU emulation layers for windows.
if you ask me, use gcc or clang to target linux, and use msvc or clang to target windows. using gcc to target windows is just signing up for completely unnecessary pain.
clang has first-class windows support.
Okey only after a prief google I just know realized that GNU is basicaly a UNIX OS, right? And I tried to do windows stuff with it. XDXD
though i would generally say that using non-standard tools generally requires a decently strong justification. so unless you have a strong reason to not use msvc, you should be using msvc to target windows.
I will definetely switch to msvc know, and also read a lot about it.
GNU is basically a suite of software tools. it's more or less what the linux ecosystem is built around. the GNU project at least historically had very strong ideological reasons to be the exact opposite of compatible with windows to say the least ^^
you don't need to stay away from it. if you're on linux, it's what you should be using.
yeah
but if you're on windows, it's probably not what you should be using ^^
do give visual studio at least a try i would say. you can always set up vscode to use msvc instead if you don't like it. but especially if you're new to all this, i would highly recommend to use VS and not vscode.
VS is the official IDE designed for targeting windows, it comes with everything you need out of the box and ready to go. vscode is a "build your own IDE" kit.
Well okey. I will try it. :DD A lot of thanks for the answers, helped a lot. I will look into msvc also. Have a beautiful day man! ^^
just ping me here if you have any issues getting this set up in VS
okey. I will 😄
@rich spade Has your question been resolved? If so, type !solved :)
!solved