Jump to content
  • Advertisement
Sign in to follow this  
pteri498

char* to LPCWSTR

This topic is 4624 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I created a window wrapper class long ago with the Visual C++ 2005 Express beta. The class works fine on all the projects that I've created with that version of vc++. However, when I try to create a new project and use that class in the exact same way, it creates a few errors:
c:\reality 101\c++ game engine\input engine\02 - using separate functions\window.cpp(94) : error C2440: '=' : cannot convert from 'char *' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\reality 101\c++ game engine\input engine\02 - using separate functions\window.cpp(111) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'char *' to 'LPCWSTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


The problem areas are here:
	wcex.lpszClassName = m_ClassName; //Where m_ClassName is char*

	m_hWnd = CreateWindowEx(WS_EX_CLIENTEDGE, m_ClassName, m_WindowTitle, dwstyles,
		rWindow->left, rWindow->top, rWindow->right-rWindow->left,
		rWindow->bottom-rWindow->top, NULL, NULL, *m_phInstance, (void*)this);

I'm sure if the second function had moved beyond the m_ClassName error, m_WindowTitle would have produced the same error. Now this typecasting was never a problem when I was working before, and in fact the old projects that I'm using with the new C++ Express still work fine. Does anyone know what might be wrong here?

Share this post


Link to post
Share on other sites
Advertisement
VC++ 2k5 defaults to UNICODE, so either change your char* to wchar_t* or change the project to MBCS in the project properties.

Cheers,
Pat.

Share this post


Link to post
Share on other sites
wchar_t* sounds like a great start, however my function call no longer works.


g_Window = new Window(hInstance, "class", "DI 2", winTitle, 50, 50, 640, 480);



Where the inputs for "class" and "DI 2" are now of type wchar_t*

Now, however, I'm getting a new error:


c:\reality 101\c++ game engine\input engine\02 - using separate functions\main.cpp(17) : error C2664: 'Window::Window(HINSTANCE &,wchar_t *,wchar_t *,DWORD,int,int,int,int)' : cannot convert parameter 2 from 'const char [6]' to 'wchar_t *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



I've tried typecasting "class" and "DI 2" to (wchar_t*), but then the title of the window shows up as rubbish. Is there another way to do this?

Share this post


Link to post
Share on other sites
project->properties->Conifiguration properties->general and then under project defaults under the character set option change the default "Use Unicode Character Set" to "USe Multi-byte Character Set"

OR


Change:

g_Window = new Window(hInstance, "class", "DI 2", winTitle, 50, 50, 640, 480);



to


g_Window = new Window(hInstance, L"class", L"DI 2", winTitle, 50, 50, 640, 480);



Notice the 'L' macro infront of where you have char strings.

Share this post


Link to post
Share on other sites
A good practice is to always wrap your texts into the predefined TEXT () macro, depends on the UNICODE symbol has been defined or not, the macro will put an appropriate prefix "L" in front of the text if necessary: "AABB" -> TEXT ("AABB")

It sounds a bit overwhelming but you'll feel fortunate if sometime in the future someone somehow requires your project to be Unicode-friendly. As does with string-related common Win32 API functions, there actually have been two versions for each one. For example: MessageBox () is a macro of MessageBoxA () and MessageBoxW ().

Share this post


Link to post
Share on other sites
Ahh thanks guys. This is what worked: Changes all the char* datatypes into wchar_t*, did the same with function members, and whenever I use the function, I put an L in front of the text that I want to use. Works fine.

Share this post


Link to post
Share on other sites
I kinda inclined to Seleton's recommendation, you can switch from MBCS to Unicode, and vice-versa, with just a single compilation flag. But instead of TEXT I prefer to use TCHAR and _T macro:


    TCHAR szString = _T("Content");

Share this post


Link to post
Share on other sites
Quote:
Original post by HaywireGuy
I kinda inclined to Seleton's recommendation, you can switch from MBCS to Unicode, and vice-versa, with just a single compilation flag. But instead of TEXT I prefer to use TCHAR and _T macro:


    TCHAR szString = _T("Content");


Yeah, I agree with HaywireGuy. in tchar.h there are a bunch of things helpful for this kind of problem. its basically like this...

#if defined(_MBCS)
#define TCHAR char
#elif defined(_UNICODE)
#define TCHAR wchar_t
#endif

/*
then there are a bunch of string related functions that are defined to use the
proper MultiByte / Unicode characters..Its very useful so you never have to
worry about which character set your using.
*/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!