Sign in to follow this  
ynkm169

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

Recommended Posts

ynkm169    107
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 this post


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


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


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

Share this post


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

Share this post


Link to post
Share on other sites
Evil Steve    2017
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 _UNICODE
typedef std::wstring String
#else
typedef 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 this post


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

Share this post


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


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

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