c++ std::string to LPCWSTR

Hi,

I am using the function SetDlgItemTextW() which takes LPCWSTR as the third parameter, in a C++ program, see code fragment:

std::string someText( "hello world!" );
SetDlgItemTextW( hwnd, result_EDIT, someText );

but I get the following error.

error C2664: 'SetDlgItemTextW' : cannot convert parameter 3 from 'std::string' to 'LPCWSTR'

Does anyone know how to convert the above c++ std::string to LPCWSTR?

Please note I must use SetDlgItemTextW() not SetDlgItemTextA() because my program must be Unicode.

thank you,
Ola.
A simple method is this:

1
2
3
LPCWSTR a;
std::string s = "LOL";
a = (LPCWSTR)s.c_str();


and is the same if you want to use LPCSTR :)

Last edited on
Now it complies without any errors however when I attempt to run the program, it crashes and when I debug I get the following:

Unhandled exception at 0x7726dd08 in testing.exe: 0xC0000005: Access violation writing location 0x00262256.

any ideas???
arack's solution won't work (don't cast around compiler errors!!!)

std::string is not a wide string. LPCWSTR is. You have 3 options (listed in the order in which I recommend them):

1) Use std::wstring instead of std::string. Then you have a wide string and can just do whatever.c_str();

2) Don't use SetDlgItemTextW() (which takes a wide string). Instead use SetDlgItemTextA() (which takes a non-wide string). Then you can just do whatever.c_str();

3) Manually copy your std::string to a wchar_t buffer (or to a std::wstring) -- typically this is done char-by-char in a loop -- or with one of the MultiByteToWideChar WinAPI functions, or one of the std lib functions that does the same thing (although I never understoo how they worked -- so I can't really help with how to use them and/or really recommend them). Then pass the copy to SetDlgItemTextW.


Note that there is absolutly no reason to do #3 unless the original string is UTF-8 encoded or something. If you're just going to do a naive char-by-char copy, then forget about #3 and just do #2 instead because Windows will do that automatically for you.

The way I see it, if you don't have a wide string to begin with, there's no reason for you to use the wide version of the WinAPI function.
Thank you both,

As advised I used std::wstring instead of std::string and then put mywstring.c_str() in SetDlgItemTextW(); and it worked!!!

std::wstring someText( L"hello world!" );
SetDlgItemTextW( hwnd, result_EDIT, someText.c_str() );

Problem solved :-)


thanks again,
Ola.
I still find it surprising that all of the functions in the Win32 API can't take an actual string, they work with char mystring [] = "whatever" but wont accept a string mystring("whatver") and instead require a pointer to the string with c_str()
The API functions were written in C.
exactly my point, why didn't microsoft write them in C++ was there any reason for that? besides this is off topic to this post, nevermind.
MS did write them in C++. They called it MFC.

If you look at MFC it's basically all of WinAPI rewritten to be more C++ and OOP-ish.
Last edited on
I was going to add that Disch but even tho I complain, I actually find it easy to use. I have not touched MFC but from what I hear, people don't like it.
From what I hear people don't like WinAPI either ;P (at least I don't)
Windows API introduce the infamous Hungarian notation which is so "un-friendly" at least to me. I guess the notation is invented by one Hungarian programmer and it has stucked ever since.
Windows API introduce the infamous Hungarian notation which is so "un-friendly" at least to me.
In twenty years someone will say the same about camel case when we're off using the next big paradigm.

In C, you're always dealing with a lot more variables than you do in C++. And Windows C programming exposed you to huge WindProc functions with more variables than you'd see in any console app. Hungarian notation was a friendly convention for dealing with this explosion of names.
Topic archived. No new replies allowed.