It is not possible to assign a const char value to a unit of type LPCWSTR (C++, how can this be solved?)

Off-topic discussion/chat/argue area with special rules of engagement.
Forum rules
The Off-Topic area is a general community discussion and chat area with special rules of engagement.

Enter, read and post at your own risk. You have been warned!
While our staff will try to guide the herd into sensible directions, this board is a mostly unrestricted zone where almost anything can be discussed, including matters not directly related to the project, technology or similar adjacent topics.

We do, however, require that you:
  • Do not post anything pornographic.
  • Do not post hate speech in the traditional sense of the term.
  • Do not post content that is illegal (including links to protected software, cracks, etc.)
  • Do not post commercial advertisements, SEO links or SPAM posts.
We also ask that you keep strongly polarizing topics like politics and religion to a minimum. This forum is not the right place to discuss such things.
Please do exercise some common sense. How you act here will inevitably influence how you are treated elsewhere.
User avatar
Piotr Kostrzewski
Lunatic
Lunatic
Posts: 305
Joined: 2018-08-14, 15:08

It is not possible to assign a const char value to a unit of type LPCWSTR (C++, how can this be solved?)

Unread post by Piotr Kostrzewski » 2024-05-18, 15:32

Good evening. Here's my current code snippet:
#include <Windows.h>
#include <gdiplus.h>

#pragma comment(lib, "Gdiplus.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wPARAM, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
static const char ClassName[] = "YangaImageBrowser";
WNDCLASSEX wc;
MSG msg;
HWND hwnd;

Gdiplus::GdiplusStartupInput gdiStartup;
ULONG_PTR gdiToken;

Gdiplus::GdiplusStartup(&gdiToken, &gdiStartup, NULL);

wc.cbClsExtra = 0;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
Line28 wc.lpszClassName = ClassName;
wc.lpszMenuName = NULL;
Gdiplus::GdiplusShutdown(gdiToken);
return msg.wParam;
}
And here is my description of the error: (in Polish)

Błąd (aktywny) E0513 nie można przypisać wartości typu "const char *" do jednostki typu "LPCWSTR" Nasza Przeglądarka Zdjęć C:\Users\ACER\Desktop\ImageBrowser\Nasza Przeglądarka Zdjęć\Main.cpp 28
Any idea?
Have a nice weekend.

User avatar
Moonchild
Pale Moon guru
Pale Moon guru
Posts: 37640
Joined: 2011-08-28, 17:27
Location: Motala, SE

Re: It is not possible to assign a const char value to a unit of type LPCWSTR (C++, how can this be solved?)

Unread post by Moonchild » 2024-05-18, 17:59

Piotr Kostrzewski wrote:
2024-05-18, 15:32
static const char ClassName[] = "YangaImageBrowser";
You want to remove the "const" there because WNDCLASSEX doesn't allow const assignments (since the classname is mutable and you're passing a pointer to a const in your case which is immutable).

Not really sure why you didn't look up the Microsoft documentation for WNDCLASSEX because it's all in there, including requirements to register nonstandard classes.
"A dead end street is a place to turn around and go into a new direction" - Anonymous
"Seek wisdom, not knowledge. Knowledge is of the past; wisdom is of the future." -- Native American proverb
"Linux makes everything difficult." -- Lyceus Anubite

User avatar
adoxa
Lunatic
Lunatic
Posts: 424
Joined: 2019-03-16, 13:26
Location: Qld, Aus.

Re: It is not possible to assign a const char value to a unit of type LPCWSTR (C++, how can this be solved?)

Unread post by adoxa » 2024-05-19, 02:30

That's not right. It would seem UNICODE is defined somewhere, since it wants a wide string, not narrow. Remove the define or use static const wchar_t ClassName[] = L"YangaImageBrowser";.

User avatar
Piotr Kostrzewski
Lunatic
Lunatic
Posts: 305
Joined: 2018-08-14, 15:08

Re: It is not possible to assign a const char value to a unit of type LPCWSTR (C++, how can this be solved?)

Unread post by Piotr Kostrzewski » 2024-07-20, 13:31

adoxa wrote:
2024-05-19, 02:30
Remove the define or use static const wchar_t ClassName[] = L"YangaImageBrowser";.
Thank you very much.
Using const wchar_t helped.
Have a nice weekend.