win32 fixes

MinGW32 provides [v]snprintf with posix semantics, so use them instead of MSVCRT's underscore-prefixed versions.

HANDLE leak: caller of _beginthreadex() needs to close thread handle