Why does the function WSASetLastError exist?

Date:September 8, 2005 / year-entry #255
Tags:history
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050908-19/?p=34283
Comments:    10
Summary: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...

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(), …"

    http://paginas.fe.up.pt/~jmcruz/etc/sockets/winsock22.pdf

  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:

    456,

    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.

    Please.

  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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index