# Unicode Help

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

## Recommended Posts

Hi all, I'm currently working on fixing some bugs in an C++ unicode project. This unicode stuff is new to me so I'm trying to find my way as I go. I've managed to get rid of most of the bugs but I'm stuck on this one: A messagebox is popping up (as it should) with a correct title but the message text is all square symbols (there's apparentely some type of conversion issue). My Code for this function: void ShowError (const ErrorStatus es, const TCHAR* szMsg) /* PURPOSE: To show an alert dialog box with a message about the error found, and a message supplied by the user */ { const TCHAR* pszErrorText = (TCHAR*)acadErrorStatusText (es) ; TCHAR szBuffer [256] ; if (NULL == pszErrorText) { // An error getting the error message for es _tcscpy (szBuffer, szMsg) ; } else { _tprintf (szBuffer, _T("%s\n(ARX ERROR:<%s>)"), szMsg, pszErrorText) ; } ::MessageBox (NULL, szBuffer, _T("DWGINFO"),0); } MessageBox is expecting a LPCWSTR for arguements but as you can see szBuffer is a TCHAR, I can't figure out how to change it. Any info would be greatly appreciated. Thanks,

##### Share on other sites
/*PURPOSE: To show an alert dialog box with a message about theerror found, and a message supplied by the user*/{const TCHAR* pszErrorText = (TCHAR*)acadErrorStatusText (es) ;TCHAR szBuffer [256] ;if (NULL == pszErrorText) {// An error getting the error message for es_tcscpy (szBuffer, szMsg) ;} else {// This should be _stprintf, not _tprintf_tprintf (szBuffer, _T("%s\n(ARX ERROR:<%s>)"), szMsg, pszErrorText) ;}::MessageBox (NULL, szBuffer, _T("DWGINFO"),0);}

I labeled the error in your code. You're using the wrong function. You need to replace _tprintf with _stprintf. What you're doing now is printing szBuffer with useless arguements, and what it seems you're trying to do is print the value of _T(...) into szBuffer.

##### Share on other sites
Nytegard,

That worked like a charm, you made it looks so easy, I definitely appreciate the help.

MJP,

Haven't read the whole entry yet but it definitely looks intersting, thanks for the info.

##### Share on other sites

Infact, I'd suggest replacing a lot of your code with something like:

void ShowError (const ErrorStatus es, const std::tstring szMsg) /*PURPOSE: To show an alert dialog box with a message about theerror found, and a message supplied by the user*/{	const std::tstring pszErrorText( (TCHAR*)acadErrorStatusText(es) ) ;	std::tstring szBuffer;	if (pszErrorText.empty() )	{		// An error getting the error message for es		szBuffer = szMsg;	}	else	{		std::tstringstream os;		os << szMsg << _T("\n(ARX ERROR:<") << pszErrorText << _T(")");		szBuffer = os.str();	}	::MessageBox (NULL, szBuffer.c_str(), _T("DWGINFO"), 0);}

You can either have tstring && tstringstream defined as follows:

namespace std{	typedef basic_string<TCHAR> tstring;	typedef basic_stringstream<TCHAR> tstringstream;...}ORnamespace std{#if defined UNICODE || defined _UNICODE     typedef wstring         tstring;    typedef wstringstream   tstringstream;...#else    typedef string          tstring;    typedef stringstream    tstringstream;...#endif}

Mainly, this will get rid of a lot of errors which could occur with C Strings, such as buffer overruns, etc, and give you all the handy benefits of C++ strings.

##### Share on other sites
This cast
(TCHAR*)acadErrorStatusText (es)

Is very worrisome. Why do you have it?

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633656
• Total Posts
3013186
×