'CreateWindowExW' : cannot convert parameter 3 from 'std::string' to 'LPCWSTR' ERROR

Started by
10 comments, last by ynkm169 16 years, 6 months ago
Hi, I am trying to convert multibyte version of my game to unicode and I encountered error in the following line. mhMainWnd = CreateWindow(L"D3DWndClassName", mMainWndCaption, WS_OVERLAPPEDWINDOW, 100, 100, R.right, R.bottom, 0, 0, mhAppInst, 0); 'CreateWindowExW' : cannot convert parameter 3 from 'std::string' to 'LPCWSTR' ERROR Here parameter 3 is WS_OVERLAPPEDWINDOW. what should I do to correctly handle the error?
Advertisement
It's complaining about parameter #2 of CreateWindow. mMainWndCaption is a std::string, but you need a const wchar_t *. I see a few options:

- Redefine mMainWndCaption as a std::wstring. It may be easier typedef'ing your own String as either std::string or std::wstring based on _UNICODE, and then wrapping literal strings in the TEXT() macro to allow you to compile as either multibyte or unicode at will. This is my preferred method because it makes porting easier.
- Use mbstowcs to convert mMainWndCaption.c_str() to a wchar_t string.
- Use ATL/MFC CString class for the conversion, but that may include more baggage than you want.
hh10k's answer covers it, but I've moved this to 'For Beginners' should any further discussion be necessary. This sort of error is not really anything to do with DirectX and is more of a fundamental Windows programming issue.

Unicode can be a bit of a pain to get to grips with, but investing some time to read up on the technology in general and how the Windows OS's and compilers deal with it (including related Win32/ATL functions) will be time well spent.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by hh10k
It's complaining about parameter #2 of CreateWindow. mMainWndCaption is a std::string, but you need a const wchar_t *. I see a few options:

"- Redefine mMainWndCaption as a std::wstring. It may be easier typedef'ing your own String as either std::string or std::wstring based on _UNICODE, and then wrapping literal strings in the TEXT() macro to allow you to compile as either multibyte or unicode at will. This is my preferred method because it makes porting easier."


So I did the following and it doesn't work. please suggest how I should correct it to handle both multibyte and unicode.

#ifdef _UNICODE
typedefine string
#else
typedefine wstring

//function prototype
SpriteDemo app(std::string winCaption);


SpriteDemo app(TEXT("Sprite Demo"));
mhMainWnd = CreateWindow(TEXT("D3DWndClassName"), TEXT(mMainWndCaption),
WS_OVERLAPPEDWINDOW, 100, 100, R.right, R.bottom,
0, 0, mhAppInst, 0);

[Edited by - ynkm169 on October 12, 2007 2:45:24 PM]
string s = "lala";
wstring ws(s.begin(), s.end());

FunctionW(ws.c_str()); // conversion to pointer
Quote:Original post by taby
string s = "lala";
wstring ws(s.begin(), s.end());

FunctionW(ws.c_str()); // conversion to pointer


I don't c how I can use this code to correct my code above. If it works, could you please show me? I am trying to handle both multibyte and unicode
Quote:Original post by ynkm169
So I did the following and it doesn't work. please suggest how I should correct it to handle both multibyte and unicode.

#ifdef _UNICODE
typedefine string
#else
typedefine wstring
I assume you mean typedef? And it's the wrong way around. You want something like:
#ifdef _UNICODEtypedef std::wstring String#elsetypedef std::string String

And as taby said, you need to use the c_str() function to convert the string to a pointer. A string is a string, and not a pointer to a character, in the same way a dog is a dog and not a cat.
I changed it to this:
mMainWndCaption.c_str()
but it gives me this
cannot convert parameter 3 from 'const char *' to 'LPCWSTR'
Quote:Original post by ynkm169
I changed it to this:
mMainWndCaption.c_str()
but it gives me this
cannot convert parameter 3 from 'const char *' to 'LPCWSTR'


That's because you're still using a std::string for mMainWndCaption, and not one typedef'd to the correct type.
I tried to put

#ifdef _UNICODE
typedef std::wstring String
#else
typedef std::string String

at the end of stdafx.h
and #include "stdafx.h" at the beginning of the file that contains main().

I get this in all the places I used String.
error C2061: syntax error : identifier 'String'

This topic is closed to new replies.

Advertisement