Is WSAGetLastError() just an alias for GetLastErro

2020-03-11 04:22发布

问题:

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 :)

回答1:

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



回答2:

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.