Use Windows internal printf to create small executable images
[page last modified 2017-03-25]

Sometimes you write a simple tool and would rather not get a 100k+ executable.
There are C run-time libraries to help you do this, notably the excellent   WCRT by Jørgen Ibsen

WCRT uses the Windows internal user32 wvsprintf() to provide its flavors of printf; however, WCRT outputs to stdout/stderr only in binary mode, not the _O_TEXT mode a Windows C program would normally use.  _O_TEXT mode produces 0xd 0xa pairs when a 0xa is output.  This difference caused my tools to fail.

I wrote the tiny (a couple hundred bytes or so of code) winpf.lib here as a workaround -- it provides a few printf variations that perform the _O_TEXT mode translation.

The functions are named the same as their C runtime equivalents but with a   w_  prefix.  Those that output to a standard device do not use the stdin/stdout handles; instead they take a Windows HANDLE.   They'll also accept, in place of a true Windows HANDLE, one of the   W_  constants #defined in winpf.h -- these constants will produce smaller code sequences and remove the need to call GetStdHandle().

The result of using the w_ functions exclusively is the C runtime printf functions will disappear from your program's link map.  This can be a substantial savings, though you must respect the Windows wvsprintf() restrictions described in winpf.h shown at the bottom of this page.

Examples:

   w_printf("The answer is %u\n", i);
   w_fprintf(W_STDERR, "Fatal error %u\n", err);
   w_sprintf(pbuf, "Fatal error %s\n", pstring);
Source (there's hardly any) is included.  Both 32 and 64 bit versions of the library are provided.


Change Log

Initial version released 3/25/2017

Click here to download  winpf170325.zip


Widget is loading comments...

You are visitor 9926       Go to Home Page


/*---------------------------------------------------------------------------- ; ; winpf.h -- ?printf() replacements using Windows user32 wvsprintf() ; ; Tiny w_xxx "printf" functions to replace equivalent C runtime versions. ; Written to minimize code size by using direct Windows wvsprintf() call. ; ; Differences/notes: ; (1) functions that output do so in _O_TEXT mode (ie \n expands \r\n). ; (2) no float format support. ; (3) output size limited to 1k. ; (4) handles are Win HANDLE type, not stdio FILE (not stdin/stdout). ; one of the defined W_STD... constants can be used as handle arg. ; ; Date Programmer Description ; ------ ---------- ----------- ; 170325 Paul Houle Initial creation, source at paulhoule.com ; ;---------------------------------------------------------------------------*/ #ifndef _WINPF_DEFINED // include guard #define _WINPF_DEFINED #include <stdarg.h> #include <windows.h> #define W_STDIN (void *) (size_t) (short) STD_INPUT_HANDLE #define W_STDOUT (void *) (size_t) (short) STD_OUTPUT_HANDLE #define W_STDERR (void *) (size_t) (short) STD_ERROR_HANDLE int w_printf(char *pFmt, ...); int w_sprintf(char *pOut, char *pFmt, ...); int w_fprintf(HANDLE h, char *pFmt, ...); int w_vfprintf(HANDLE h, char *pFmt, va_list vl); #endif