|
Question : Unicode/STL/Varargs problem
|
|
Hi. I am very stumped by this problem. I have a small Unicode MFC project (_UNICODE is defined). VC 6.0 / Win2K . Also it uses STL strings.
Building the code below works ok. But uncommenting the line in test_msgs() causes incorrect strings to pop up in the dialog. And if the function is called a 2nd time, the app crashes.
Any ideas? Thank you very much.
#include #include
typedef unsigned short unichar;
class ustring : public std::basic_string { public: ustring() {} ustring(const unichar *rhs) : std::basic_string(rhs) {} ustring(const ustring &rhs) : std::basic_string(rhs) {} };
static void popup_msg(const ustring &msg, ...) { wchar_t buf[2048];
va_list args; va_start(args, msg); vswprintf(buf, msg.c_str(), args); va_end(args);
AfxMessageBox(CString(buf),MB_OK); }
void test_msgs(void) { wchar_t wbuf[80];
lstrcpy(wbuf,L"WChar test: %s"); CString cstr="test 10";
// When I uncomment the following line, it breaks //ustring ustr1=L"test 11";
ustring ustr2(wbuf); popup_msg(ustr2,cstr); }
|
|
Answer : Unicode/STL/Varargs problem
|
|
ahhh - sorry I should have looked more closely - this is a known problem with using references and the va_* macros see
http://support.microsoft.com/support/kb/articles/Q119/3/94.asp
you can use their fix or you could just change your code
static void popup_msg2(const unistr *msg, ...) { wchar_t buf[2048];
va_list args; va_start(args,msg); // the following line crashes vswprintf(buf,msg->c_str(),args); va_end(args);
DISPLAY_STR(buf); }
...
popup_msg2(&ustr,wbuf2);
|
|
|
|