Use snprintf instead of sprintf.

For Windows compatibility, as snprintf is not available. Also defines
snprintf as _snprintf for Visual Studio.
Uses equivalents of getcwd and getpid for MSVC

Also test WIN32 for correct inclusion of header files.