about: LPCWSTR error

Started by
6 comments, last by MJP 14 years, 7 months ago
Dear readers, I am quit new to c++ but have alot of other programming languages so i pick up everything pretty fast. I have just made my first little windows application using dev c++ (bloodshed) But because i rather work in visual express 2008 i reopened everything in there but now i get the following error (that didn't occur in dev c++ bloodshed) 1>c:\LOCATION_TO_MY_PROJECT\capplication.cpp(61) : error C2440: '=' : cannot convert from 'const char [10]' to 'LPCWSTR' The code: WindowClass.cbSize = sizeof(WNDCLASSEX); WindowClass.style = CS_HREDRAW | CS_VREDRAW; WindowClass.lpfnWndProc = WindowProcedure; WindowClass.cbClsExtra = 0; WindowClass.cbWndExtra = 0; WindowClass.hInstance = GetModuleHandle(NULL); WindowClass.hIcon = NULL; WindowClass.hCursor = NULL; WindowClass.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); WindowClass.lpszMenuName = NULL; WindowClass.lpszClassName = "ClassName"; WindowClass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); And the line with the error: WindowClass.lpszClassName = "ClassName"; My guess is "ClassName" is a char type (so not the proper type) but it should be a LPCWSTR somehow I am working on a x64 winxp dual core amd x2 cpu with 4gb 677hmz Could someone please explain why this aint working and why i did in dev c++ bloodshed?? Kind regards, MasterX
Advertisement
BTW: i am doing the www.two-kings.de tutorials (DirectX Graphics section)
Hello ... The difference between Dev C++ and Visual C++ 2008 is that the latter uses Unicode (wide strings). You can solve your problem by placing an L in front of your window class name like this:

WindowClass.lpszClassName = L"ClassName";

By doing this you are basically saying that you want the wide version of the string(the Unicode version).

BTW, Unicode is just a character set just like ASCII only bigger, much bigger.

-------------------------------------------------------------------------------------------
My Website - Portfolio, Tutorials
My blog
For classnames it makes no difference at all whetever or not you use unicode, but it's essential when reading files from disk, or you might end up with some piece of shit like Pix, crashing everytime it encounters anything odd ;)
Here, you might want to read through this.
Quote:Original post by masterx
1>c:\LOCATION_TO_MY_PROJECT\capplication.cpp(61) : error C2440: '=' : cannot convert from 'const char [10]' to 'LPCWSTR'
* The string literal "ClassName" is a 'const char [10]' which can decay into a 'const char *'.
* The variable lpszClassName is a LPCWSTR which means 'Long Pointer to a Constant Wide STRing', in other words it's a 'const wchar_t *'.

Clearly the above are different, and incompatible, types. What you want is to use the string literal L"ClassName" which is a 'const wchar_t [10]' and would decay to a const wchar_t * as required for lpszClassName.

Quote: WindowClass.hIcon = NULL;
...
WindowClass.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
It makes more sense to swap those over, specify a large icon and use NULL for the small icon to indicate you want it to use the small version of the large one. It just so happens that for IDI_APPLICATION it makes no difference but it's just strange the way you've written it.

Quote:WindowClass.hCursor= NULL;
Just checking, are you aware of what the documentation says about this?..
Quote:hCursor
Handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application's window.


Quote:Could someone please explain why this aint working and why i did in dev c++ bloodshed??
DevC++ is old and defaults to a multi-byte character strings rather than unicode strings. If you really want you can disable unicode for Visual Studio under your project's settings.
I just checked and the type is actually LPCTSTR (which casts to either ANSI or Unicode depending on the building settings). That means that the string shouldn't be hard-coded to be Unicode either, but be written using the TEXT() macro:

WindowClass.lpszClassName = TEXT("ClassName");
Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.
Quote:Original post by Sik_the_hedgehog
I just checked and the type is actually LPCTSTR (which casts to either ANSI or Unicode depending on the building settings). That means that the string shouldn't be hard-coded to be Unicode either, but be written using the TEXT() macro:

*** Source Snippet Removed ***


No, it means that you should use the TEXT macro if you want your app to be able to compile for both wide-char and multi-byte strings. If you only want your app to use wide characters then it's perfectly fine to just use wide characters and wide-char strings everywhere.

All of the Windows API functions have Unicode and ANSI versions because they can't force every app to use wide-character strings, since there's tremendous amounts of legacy code out there that's still in use. That restriction usually doesn't apply to people here.

This topic is closed to new replies.

Advertisement