Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pesky LPCWSTR pointers!

Started by Swatter555 Apr 13, 2005 at 5:00 AM 3 replies 1.1k views
Original Post
Swatter555
Swatter555
I am using the directx gui, in this case the edit boxes. The edit boxes return the data in the wide char LPCWSTR, but I need it in the multibyte format. I have tried using the WideCharToMultiByte function, but I cant seem to get it to work with even simple examples. How does everyone handle this? Some code examples would be very helpful. Thanks in advance!
Evil Steve
Evil Steve
This should work (untested):
const WCHAR* wszText = L"Some wide-character string";char szText[256];  // Yeah, possible buffer overrun, I know :PWideCharToMultiByte(CP_ACP, 0, wszText, -1, szText, 255, NULL, NULL);
Swatter555
Swatter555
Thanks, I was working on it way too late :)
Alamar
Alamar
How is declaring a character buffer a possible overrun?

No data type is inherently a possible buffer overrun, and every type used improperly could be a 'buffer overrun' (quoted since copying memory into say, an int variable isn't a buffer).

It's the way buffers (most notably string buffers) are used that cause overruns, and just getting into the habit of doing some things can prevent that. Such things can include, setting the last value in the buffer to null (before and not overwriting it, or after). It's a matter of preference, but I like to Zero my buffers and then use them by their size minus one, such as:
char szBuffer[256];ZeroMemory( szBuffer, sizeof( szBuffer ) / sizeof( *szBuffer ));DoSomeCopyOperation( szBuffer, sizeof( szBuffer ) / sizeof( *szBuffer ) - 1 );


DoSomeCopyOperation in this case takes a count of chars, as opposed to a count of bytes (which would be sizeof( szBuffer ) - sizeof( *szBuffer )).

Why use sizeof? Pragmatism. If you used hard values in all your functions that use szBuffer, you need to change them all if you change the size of szBuffer.

Why the '/ sizeof( *szBuffer )'? If you changed char to WCHAR (or the deprecated TCHAR - opinion based on unicows usage), again, the arguments would need no changes. I personally use a macro SizeOfString which is just the sizeof/sizeof set so it doesn't look so scary : )

-Alamar
Evil Steve
Evil Steve
Quote:
Original post by Alamar
How is declaring a character buffer a possible overrun?
Well, if you had that code in a function that can take any length of input string, and you pass a string of > 255 characters, you're likely to get a buffer overrun. WideCharToMultiByte() needs to know the buffer length, but it may not NULL terminate it if it was to overflow (I don't know if it actually does). In the best case, you'll lose the end of your string. In the worst case, it won't be NULL terminated, and you'll run into all sorts of weird problems. Of course that can be solved by adding szText[255] = '\0'; though.
Overall, you should probably dynamically allocate memory in that function, instead of assuming that it won't ever recieve a buffer over 255 characters long.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.