11

In my code, I have asynchronous I/O with I/O Completion Ports, and for the read/write completion callbacks, I get a HANDLE (that of course can be a socket, file handle, named pipe and so on).

So if something is wrong in such routine, I want to check the error, but how to know if its a "network" HANDLE (a SOCKET, so I should call WSAGetLastError()) or a "non-network" HANDLE (named pipes, files and so on, so I should call GetLastError())? I'm using a simple flag for that, but its ugly, and inconvenient.

If someone can confirm that WSAGetLastError() is just an alias for GetLastError(), I will use only the latter.

It seems so:

http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.networks/2007-08/msg00034.html

http://us.generation-nt.com/wsagetlasterror-just-an-alias-getlasterror-help-28256642.html

But can someone confirm that? MSDN is not much clear on this topic.

And would it be safe to use GetLastError() instead of WSAGetLastError()? I mean, if WSAGetLastError() is even an alias of GetLastError() since Windows95 as someone claim, I could assume that it will be true for the next version of Windows -- but we can't write good code on assuming things :)

Marco Pagliaricci
  • 1,366
  • 17
  • 31

2 Answers2

13

It is just a wrapper to GetLastError if you reverse engineering ws2_32.dll, you'll find it.

Jichao
  • 40,341
  • 47
  • 125
  • 198
  • Yeah I saw that, but would you use GetLastError() instead of WSAGetLastError() ? I don't think they will separate those 2 functions, by now – Marco Pagliaricci Mar 23 '13 at 12:13
  • 1
    I think that is safer use WSAGetLastError due to specific Winsock errors. But this is only my opinion. – Xearinox Mar 23 '13 at 12:29
  • 1
    It is cleaner to use the function the documentation suggests, however in this particular case it is highly unlikely that these two will ever break into separate functions. – Roman R. Mar 23 '13 at 13:44
  • 3
    They used to be different, when Winsock wasn't built into the kernel like it must be now if they are the same, and there's nothing to guarantee they won't be different in some future system. Use WSAGetLastError(). – user207421 Mar 23 '13 at 23:53
8

Reason behind having two similar functions: http://blogs.msdn.com/b/oldnewthing/archive/2005/09/08/462402.aspx

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.

Community
  • 1
  • 1
Alex
  • 5,477
  • 2
  • 36
  • 56