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

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
Quote:
 Original post by hh10kIt'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]

##### Share on other sites
string s = "lala";
wstring ws(s.begin(), s.end());

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

##### Share on other sites
Quote:
 Original post by tabystring 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

##### Share on other sites
Quote:
 Original post by ynkm169So I did the following and it doesn't work. please suggest how I should correct it to handle both multibyte and unicode. #ifdef _UNICODEtypedefine 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.

##### Share on other sites
I changed it to this:
mMainWndCaption.c_str()
but it gives me this
cannot convert parameter 3 from 'const char *' to 'LPCWSTR'

##### Share on other sites
Quote:
 Original post by ynkm169I changed it to this:mMainWndCaption.c_str() but it gives me thiscannot 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.

##### Share on other sites
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'

##### Share on other sites
Whoops, I mean:
#ifdef _UNICODE   typedef std::wstring String;#else   typedef std::string String;#endif

##### Share on other sites
i get like 22 errors (including '_UNICODE' : macro redefinition warning)
error C2061: syntax error : identifier 'String'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int...

I am not familiar with VC++ compiler. It is very frustrating to debug

If someone is willing to download my small game project and debug this for me, I will really really appraciate it!!!!!!

My game demo

Thanks!

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978516

• 10
• 10
• 12
• 22
• 13