today I'm kinda pissed. Here is the situation. I wanted to write a small tool for the game Urban Rivals, so that I don't have to figure out the strength of the opponent by myself. Nothing special so far.
There is the maindialog with some comboboxes and an editbox for the strengthoutput. Let's take a closer look! If you want to write a string to an editbox you have to use this function:
BOOL SetDlgItemText(HWND, int, LPCWSTR);
So what is LPCWSTR, you might ask. Here is the correct answer, from msdn.
A 32-bit pointer to a constant null-terminated string of 16-bit Unicode characters. This type is declared in WinNT.h as follows:
typedef CONST WCHAR *LPCWSTR;
Pretty interesting, isn't it? Ok, a just little problem. So let's google for the solution. 3 hours and 20 different code snippets, which does not work for me, later I'm here to write this. Look at all that crappy string and string converting stuff in C++ ò.Ó
First things first: Some information. You have two different types of characters. Normal characters and unicode so called wide characters.
char character = "H"; wchar_t wide_character " L"H";
Anyway... You have also a string and a wstringclass. A class which holds and manages a const char* or a const wchar_t* value. But you have also a const char* stream and a const wchar_t* stream which is nearly the same, not with the bufferconcept, but it uses the streamtechniques.
And you have a whole bunch of other classes like, basicstringstream<char*>, stringstream, ostringstream and so on. And here comes the point. You can't get one of these things to get work with LPCWSTR. Look:
As you remember, SendDlgItemText needs a widestring. That means wo should write something like this:
wchar_t* mystring = 42; SendDlgItemText(handle, 0, mystring);
Erm yeah. This is bullshit. Because you can't assign a integer to a widecharacterstring. That would be to easy. Even if you made it to do so, the result will be something like this what you saw above. Long story short. I didn't made it to assign an integer to this damn string and to display it without any chinese characters in it.
Here my favourite solutions, because they're so weird, that they illustrate the c++-stinrg-dilemma very well
solution 1 - very confusing, cause nobody knows what _itow_s() really does!
int number = 42; wchar_t mystring; _itow_s(number, mystring, 10);
solution 2 - really cool, because it shows that c+ has to much types of strings and streams
wchar_t test = L"hello"; swprintf(test, 6, L"%i", ergebnis);
solution 3 - let's use a stringstream!
wstringstream mystream; mystream << ergebnis;
Not even one of them used to work fine! And now I am at the limit. I not only dislike all the string stuff in C++, I hate it! As you must know, this was not the first time, that I had trouble with strings in C++.
I know that the problem, doesn't really deal with the string stuff, but with the LPCWSTR and the win32 WinAPI. But if wou would be honest just for only one second, you have to admit, that you don't need morde the 10 different classes to put "hello world" to the screen. Or was it L"hello world"?