Jump to content
  • Advertisement
Sign in to follow this  
Matthew Shockley

C++ Errors

This topic is 3003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Try this, just cast the wParam value to a WCHAR/wchar_t type. From the way MSDN reads, it should already be one.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 );
}

Share this post


Link to post
Share on other sites
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-terminatoe

wcscat_s(Text, (sizeof(Text) / sizeof(Text[0]), &NewKey);


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!