Changed error checking on Windows API function. Adresses #856 (ported from sandbox-branches/birbacher/fix_iostreams)