Sign in to follow this  
masterx

about: LPCWSTR error

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this