C++ Errors

Started by
11 comments, last by Rattrap 14 years ago
Can you help me with something? I have this code:

wchar_t Text[32];

void onKeyDown(WPARAM KeyCode, HWND hWnd) {
	char NewKey = static_cast<char>(KeyCode);
	wchar_t nwChar;
	mbstowcs_s(0, &nwChar, sizeof(NewKey)+1, &NewKey, sizeof(NewKey));
	wcscat_s(Text, sizeof(nwChar)+sizeof(Text)+2, &nwChar);
}

When I run the program and press a key, the debug thing says: Run-Time Check Failure #2 - Stack around the variable 'nwChar' was corrupted. Why is this erroring?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Advertisement
I think the problem is the size values your passing, mbstowcs_s wants to know the number of elements in an array, your sending it the number of bytes in a single element.

The same is likely going for wcscat_s as well.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

What should I put then?
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
My first suggestiong would be to look and see if you can catch the WM_CHAR message. It should automatically return an UNICODE character (UTF-16 or as close to it as windows gets).

See MSDN - WM_CHAR

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

I am doing that.

case WM_CHAR:	onKeyDown(wParam, hWnd);	break;
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
Try this, just cast the wParam value to a WCHAR/wchar_t type. From the way MSDN reads, it should already be one.

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

Like this?

void onKeyDown(WPARAM KeyCode, HWND hWnd) {	wchar_t NewKey = static_cast<wchar_t>(KeyCode);	wcscat_s(Text, sizeof(NewKey)+sizeof(Text)+2, &NewKey);}


When I run this and press a key, the program freezes then closes.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?
The second parameter of wcscat_s is the number of characters your destination buffer can hold, which in your case, is 100, or sizeof(Text)/sizeof(Text[0]). The source string is also a null-terminated string, and the null-terminated string stating at the address of a character does not have a null-terminated string. Try this instead:

wchar_t Text[100] = {};void onKeyDown(WPARAM KeyCode, HWND hWnd) {	wchar_t NewKey[2] = { static_cast<wchar_t>(KeyCode), L'\0' };	wcscat_s(Text, sizeof(Text)/sizeof(Text[0]), NewKey );}
The wcscat_s is still wrong. wcscat_s wants to know the length of the destination, which would be (sizeof(Text) / sizeof(Text[0]), since you statically allocated it. The other problem is it wants the source to be a null-terimated string, not a single character.

Try this (untested).

wchar_t NewKey[2];NewKey[0] = static_cast<wchar_t>(KeyCode);NewKey[1] = 0;  // Null-terminatoewcscat_s(Text, (sizeof(Text) / sizeof(Text[0]), &NewKey);

"I can't believe I'm defending logic to a turing machine." - Kent Woolworth [Other Space]

That worked the first 3 times I pressed a key, but when I pressed it the fourth time it errored saying Buffer to Small.
If I asked you for a hundred dollars would the answer to that question be the same as the answer to this question?

This topic is closed to new replies.

Advertisement