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);
}
C++ Errors
Can you help me with something? I have this code:
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?
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.
The same is likely going for wcscat_s as well.
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
See MSDN - WM_CHAR
Try this, just cast the wParam value to a WCHAR/wchar_t type. From the way MSDN reads, it should already be one.
Like this?
When I run this and press a key, the program freezes then closes.
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.
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).
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);
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement