views:

168

answers:

1

I have following C++ code sample:

void SetVaArgs(const char* fmt, const va_list argList)
{
  setlocale( LC_ALL, "C" );

  // 1
  m_FormatBufferLen = ::_vsnprintf(m_FormatBuffer, Logger::MAX_LOGMESSAGE_SIZE, fmt, argList); 

  setlocale( LC_ALL, "" );

  //2
  m_FormatBufferLen = ::_vsnprintf(m_FormatBuffer, Logger::MAX_LOGMESSAGE_SIZE, fmt, argList);

  _locale_t locale = _create_locale(LC_ALL, "C");;

  //3
  m_FormatBufferLen = ::_vsnprintf_l(m_FormatBuffer, Logger::MAX_LOGMESSAGE_SIZE, fmt,locale, argList);

The arglist contains LPCTSTR with extended ascii characters. Command //1 copies it to the buffer, as expected. Command //2 stops copying at first character from range 129-161 (few exceptions there).

I'd like to address this issue without changing global locale for process, but command //3 works like //2, why? I'm passing "C" locale, so I would expect effect from command //1.

By default I'm using Polish locale on english Windows XP.

+3  A: 

It turned out to be a CRT bug in VS2005 and above (2008 and 2010). Submitted to Microsoft here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=523503#details

Bug applies to _sprintf_l, _vsnprintf_l, _sprintf_s_l, _vsnprintf_s_l and possibly other relatives.

tomash