Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

char to wide char conversion problem

This topic is 3816 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

i trying to convert a char string to a wide char string but im having some problems.... AddRow("CScene : Successfully Loaded : %s", pFilename); AddRow(const CHAR* strMsg, ...) { WCHAR strTemp[MAX_PATH]; MultiByteToWideChar( CP_ACP, 0, strMsg, -1, strTemp, MAX_PATH ); /*WCHAR strBuffer[MAX_PATH]; va_list args; va_start(args, strMsg); StringCchVPrintf( strBuffer, MAX_PATH, strTemp, args ); strBuffer[MAX_PATH-1] = L'\0'; va_end(args);*/ m_vText += strTemp; m_vText += L"\n"; } this doesnt work... but if i remove "%s" it works fine... why do i get an memory error when using formatting?

Share this post


Link to post
Share on other sites
Advertisement
What's pFilename? If it's a narrow character string, then you've got some bad mojo going down, as the %s format specifier refers to a string of the type of the width of the function. That is, with printf() it means a narrow character string, and wprintf() means a wide character string. In this case, it seems you are using StringCchVPrintf() as a wprintf() version.

To paraphrase myself: Ellipses bad. No type safety.

Share this post


Link to post
Share on other sites
pFileName is a "char*"

whats the printf version?

ive tried this... but now i get strTemp = ""

CHAR strBuffer[MAX_PATH];
va_list args;
va_start(args, strMsg);
printf( strBuffer, MAX_PATH, strMsg, args );
strBuffer[MAX_PATH-1] = L'\0';
va_end(args);

WCHAR strTemp[MAX_PATH];
MultiByteToWideChar( CP_ACP, 0, strBuffer, -1, strTemp, MAX_PATH );

m_vText += strTemp;
m_vText += L"\n";

Share this post


Link to post
Share on other sites
I would do formatting the char* string first, then convert it to wchar_t* string later.


void log(const char *format, ...)
{
char buffer[LOG_MAX_STRING];

// do the formating
va_list valist;
va_start(valist, format);
_vsnprintf(buffer, LOG_MAX_STRING, format, valist);
va_end(valist);

// convert to wchar_t*
toWchar(buffer);
...
}



const wchar_t* toWchar(const char *src)
{
mbstowcs(dst, src, WCHAR_MAX_LENGTH); // copy string as wide char
dst[WCHAR_MAX_LENGTH-1] = L'\0'; // in case when source exceeded max length

return dst; // return string as wide char
}



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!