Sign in to follow this  

This topic is 2812 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
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
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
Know that wcscat_s isn't making Text bigger. It just keeps adding to the end till it runs out of space. You'll need to make it bigger if you expect a large amount of input, with the method you are using.

That's the advantage of using the sizeof(Text) / sizeof(Text[0]) method. It will automatically put it the correct size of Text based on how big you declared Text.

[edit]
Would definately agree with fastcall's suggestion of using std::wstring.

Share this post


Link to post
Share on other sites

This topic is 2812 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this