Clean inclusion of windows.h

C++

Public Domain

Include windows.h in a more POSIX-friendly way

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Including windows.h messes up stuf unless these are defined first.
#ifdef WIN32
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# define WIN32_EXTRA_LEAN
# define _USE_MATH_DEFINES
# include <windows.h>
#endif

// Windows makes using POSIX annoying
#ifdef WIN32
# define STRNCPY(d,s,n) strncpy_s((d),(n),(s),(n))
# define STRTOK(a,b,c) strtok_s((a),(b),(c))
#else
# define STRNCPY(d,s,n) strncpy((d),(s),(n))
# if defined(_SVID_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_C_SOURCE) || defined(_XOPEN_SOURCE)
#  define STRTOK(a,b,c) strtok_r((a),(b),(c))
# else
#  define STRTOK(a,b,c) strtok((a), (b))
# endif
#endif

#ifdef WIN32
# define SM_SNPRINTF _snprintf
#else
# define SM_SNPRINTF snprintf
#endif