Need help: char array to string... string becomes garbage

Hi. I'm trying to "tokenize" a string using std::string functions, but I stored the text in a char array. When I try to convert it to a string, the string has the first character right but the rest is garbage.

I'm a noob so if you spot other things I'm doing wrong, feel free to rip me apart.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessage(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);
// (Some testing to see what happens if I convert the string back to a char array.)
char newVal[256];
memset(newVal, NULL, 256);
strcpy(newVal, val.c_str());
// Original text: Right
MessageBox(hWnd, (LPCWSTR)val.cstr(), L"", 0);
// ^ Result: R쳌쳌쳌쳌쳌쳌쳌
MessageBox(hWnd, (LPCWSTR)newVal, L"", 0);
// ^ Result: R
parser.extract(selectedValue);
SendMessage(GetDlgItem(hwnd, IDC_E_VALUE), WM_SETTEXT, selectedIndex, (LPARAM)parser.getAction().c_str());
// ^ Result: R쳌쳌쳌쳌쳌쳌쳌
break;
You are using WinAPI incorrectly:

#1: MessageBox does not take LPCWSTRs... it takes LPCTSTRs

#2: casting a char* to a LPCWSTR is a bad cast. Likewise, casting a char* to a LPCTSTR is also a bad cast... so don't try that either.


Possible solutions:

1) Use TCHAR arrays (and TCHAR strings... std::basic_string<TCHAR> instead of std::string) instead of char arrays/strings.

or

2) Use the charversion of WinAPI functions instead of the TCHAR version. IE: use MessageBoxA instead of MessageBox.



I recommend solution #2 because TCHARs are retarded.


--------------------------
EDIT:
--------------------------

Your code... fixed. Note all you have to do is get rid of the bad casts... and call the 'A' version of the WinAPI functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Get value from ListBox.
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessageA(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
// Convert to string.
string val(selectedValue);
// (Some testing to see what happens if I convert the string back to a char array.)
char newVal[256];
memset(newVal, NULL, 256);
strcpy(newVal, val.c_str());

MessageBoxA(hWnd, val.cstr(), "", 0);

MessageBoxA(hWnd, newVal, "", 0);

parser.extract(selectedValue);
SendMessageA(GetDlgItem(hwnd, IDC_E_VALUE), WM_SETTEXT, selectedIndex, (LPARAM)parser.getAction().c_str());
Last edited on
Actually upon further investigation... I'm not sure SendMessageA will be enough. The LB_GETTEXT and WM_SETTEXT messages seem to only have TCHAR versions (at least I don't see any indication that they support an ANSI version on MSDN).


So you might have to go with solution #1 above.
Thanks Disch! Using the ANSI versions solved the problem; no complaints from LB_GETTEXT or WM_SETTEXT.

I tried replacing char with TCHAR and string with basic_string but there were problems with typedef-ing std::basic_string.

This is my final code for future reference:
1
2
3
4
5
char selectedValue[256];
memset(selectedValue, NULL, 256);
SendMessageA(GetDlgItem(hWnd, IDC_LB_CURRENTSCRIPT), LB_GETTEXT, selectedIndex, (LPARAM)selectedValue);
parser.extract(selectedValue);
SendMessageA(GetDlgItem(hwnd, IDC_E_VALUE), WM_SETTEXT, selectedIndex, (LPARAM)parser.getAction().c_str());


1
2
3
4
5
private:
	string action;
public:
	void extract(char*);
	string getAction(){return action;}
Last edited on
Topic archived. No new replies allowed.