visual studio swprintf is making all my %s formatters want wchar_t * instead of char *

Go To StackoverFlow.com

5

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

2012-04-03 20:17
by Medran


4

Visual Studio 14 CTP1 and later will always treat %s as 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. For sprintf %Ts is char* and for swprintf %Ts is wchar_t*.


In Visual Studio 13 and earlier %s/%c is mapped to the natural width of the function/format string and %S/%C is mapped to the opposite of the natural with:

printf("%c %C %s %S\n", 'a', L'B', "cd", L"EF");
wprintf(L"%c %C %s %S\n", L'a', 'B', L"cd", "EF");

You can also force a specific width by using a length modifier: %ls, %lc, %ws and %wc always mean wchar_t and %hs and %hc are always char. (Documented for VS2003 here and VC6 here (Not sure about %ws and when it was really added))

Mapping %s to the natural width of the function was really handy back in the days of Win9x vs. WinNT, by using the tchar.h header you could build narrow and wide releases from the same source. When _UNICODE is defined the functions in tchar.h map to the wide functions and TCHAR is wchar_t, otherwise the narrow functions are used and TCHAR is char:

_tprintf(_T("%c %s\n"), _T('a'), _T("Bcd"));

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 UNICODE and TEXT and not _UNICODE and _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:

#ifdef _WIN32
#define PRIs "s"
#define WPRIs L"hs"
#else
#define PRIs "s"
#define WPRIs L"s" 
#endif
printf("%" PRIs " World\n", "Hello");
wprintf(L"%" WPRIs L" World\n", "Hello");

3) Create your own custom version of swprintf and use it with Visual Studio 13 and earlier.

2012-04-03 20:54
by Anders
Im really not sure how to use this, in my tests when UNICODE is defined or not defined the behaviour is the same swprintf %s expects a wchart and not a char*. As for the MS extensions do you know if there is a way to get gcc to use them - Medran 2012-04-03 21:04
Are you sure your environment is not defining it for you (In the project settings in VS)? If by gcc you mean MinGW then yes, MinGW can use the MS C run time, see http://stackoverflow.com/questions/6729013/mingw-printf-size-specification-character- - Anders 2012-04-03 21:11
no gcc on mac... In the project settings you specific the character type as 'not defined' or 'use unicode' or 'use multibyte'. If you say 'not defined' then _UNICODE is not defined, if you say 'use unicode' then _UNICODE is defined - Medran 2012-04-03 21:16
You will probably only find support for %hs and %ls on the windows platform, to stay portable you can make up your own PRN define for strings like inttypes.h does for numbers if you want to avoid #ifdef' - Anders 2012-04-03 21:25
..I somehow missed the fact that you said swprintf, swprintf is the wide version no matter what the define is, sprintf is the narrow version and stprintf (tchar.h) depends on the define. Assuming your destination is wchar* the problem is unchanged and you will need MS specific format strings.. - Anders 2012-04-03 21:33
thanks for your help anders, and neil you've been good sounding boards - Medran 2012-04-04 04:26


2

Use the %ls format which always means wchar_t*.

2012-04-03 20:25
by Neil
its not an issue with wchart being seen as char* its an issue with char * being seen as wchart. Plus I dont think %ls works on old visual studios... Thanks anyways neil - Medran 2012-04-03 20:32
Sorry, I couldn't find a compatible way to specify a char*, but I actually looked up %ls on the VS7.1 documentation, so that's quite a way back - Neil 2012-04-03 20:42
Yup I can confirm that %ls does work at least as far back as VS2008 which is what I'm on, thanks for that. I also found it in the documentation and apparently its what I should have been using instead of %S all along as I guess %S is very non standard thing. But really what could be more non standard than making %s equal to a wchar_t. The only thing I thought of is redirecting all my wprint to a wrapper function that I could write that would change the %s as necesary - Medran 2012-04-03 20:47


2

You can use "%Ts" format specifier or you can define _CRT_STDIO_LEGACY_WIDE_SPECIFIERS=1.

Read this:

http://blogs.msdn.com/b/vcblog/archive/2014/06/18/crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1.aspx

2014-11-22 00:36
by paveo
Ads