Why does the function WSASetLastError exist?

Why does the function WSASetLastError exist when there is already the perfectly good function SetLastError?

Actually, you know the answer too, if you sit down and think about it.

Winsock was originally developed to run on both 16-bit Windows and 32-bit Windows. Notice how the classic Winsock functions are based on window messages for asynchronous notifications. In the 16-bit world, there was no SetLastError function. Therefore, Winsock had to provide its own version for the 16-bit implementation. And since source code compatibility is important, there was a 32-bit version as well. Of course, the 32-bit version looks kind of stupid in retrospect if you aren't aware of the 16-bit version.

Comments (10)
  1. Sven says:

    And GetLastError returns the same error code as WSAGetLastError?

  2. Yaytay says:

    Sven’s comments sums up what’s annoying about WSAGetLastError – the fact that it isn’t documented to return the same as GetLastError.

    In all my experiments it does, and I suspect it just returns GetLastError() on Win32, but it ought to be documented.

    More importantly, why are there a whole bunch of winhttp errors that don’t work with FormatMessage?

  3. 456 says:

    Its documented in the WinSock 2 specification:

    "This function is implemented in WinSock 2 as an alias for the Win32 function GetLastError(), …"


  4. Aaron says:

    Actually it looks kind of stupid even if you are aware of that.

  5. Mike Dunn says:

    How did Win16 communicate errors, if there was no SetLastError (and, I assume, no GetLastError either)?

  6. Yaytay says:


    You are right, thank you, now I can ignore it with impunity.

    Nothing about it in MSDN though.

    So which is definitive, MSDN or the Winsock 2 spec?

    I would have thought that the comment in the Winsock spec is just a comment about a current implementation and that to be officially an alias for GetLastError it would need to say it in MSDN.

    Go on, prove me wrong, show me the dark and dingy corner of MSDN that says it.


  7. Goran says:


    I think somebody from Redmond sys administration is going to kill Raymond’s VSS account! I mean, how long will they put up with him wandering through VSS history to look for trivia of this kind?

  8. Goran, why do you think he looked at the history for this? It’s HIGHLY unlikely this stuff is in the source code history.

    Instead, he probably just figured it out because he understands the context in which the APIs were developed (knowing Raymond, he most likely also went to the guys who wrote winsock (two of them are still at MS) and confirmed it).

  9. mike says:

    Of all the ugly things in Windows this is one of the most tolerable.

  10. Aaron says:

    Thats how my company succeeds in business too. Just keep getting other people to lower the bar until something bad is tolerable :P

Comments are closed.

Skip to main content