Ive got a multi platform project that compiles great on mac, but on windows all my swprintf calls with a %s are looking for a wchar_t instead of the char * im passing it. Turns out M$ thought it would be funny to make %s stand for something other than char * in wide character functions… http://msdn.microsoft.com/en-us/library/hf4y5e3w.aspx
Anyways I’m looking for a creative coding trick thats better than putting ifdef else ends around every wide string call
Update: VS 2015 CTP6 reverted the changes and Microsoft are yet again acting different to the standard.
Visual Studio 14 CTP1
and laterwill always treat%sas a narrow string (char*) unless you define_CRT_STDIO_LEGACY_WIDE_SPECIFIERS. It also added the T length modifier extension which maps to what MS calls the “natural” width. Forsprintf%Tsischar*and forswprintf%Tsiswchar_t*.In Visual Studio 13 and earlier
%s/%cis mapped to the natural width of the function/format string and%S/%Cis mapped to the opposite of the natural with:You can also force a specific width by using a length modifier:
%ls,%lc,%wsand%wcalways meanwchar_tand%hsand%hcare alwayschar. (Documented for VS2003 here and VC6 here (Not sure about%wsand when it was really added))Mapping
%sto the natural width of the function was really handy back in the days of Win9x vs. WinNT, by using thetchar.hheader you could build narrow and wide releases from the same source. When_UNICODEis defined the functions intchar.hmap to the wide functions andTCHARiswchar_t, otherwise the narrow functions are used andTCHARischar:There is a similar convention used by the Windows SDK header files and the few format functions that exist there (wsprintf, wvsprintf, wnsprintf and wvnsprintf) but they are controlled by
UNICODEandTEXTand not_UNICODEand_T/_TEXT.You probably have 3 choices to make a multi-platform project work on Windows if you want to support older Windows compilers:
1) Compile as a narrow string project on Windows, probably not a good idea and in your case swprintf will still treat %s as wchar_t*.
2) Use custom defines similar to how inttypes.h format strings work:
3) Create your own custom version of swprintf and use it with Visual Studio 13 and earlier.