There is no relationship between GetLastError and errno.
GetLastError gets the last error that was set by a Windows API function (for the current thread), while errno contains the last error that was stored into it by a C runtime library function (also for the current thread).
Almost all WinAPI functions, that return errors to their callers, will indicate in some way when an error occurs, and then set the error for the caller to get by calling GetLastError.
NOTE: Not all WinAPI functions return errors to their callers.
For example, the documentation for the WinAPI function CreateFile says:
If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot. If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.
The C runtime library functions that return errors to their callers, will indicate in some way when an error occurs and then store a value in errno.
NOTE: Not all C runtime library functions return errors to their callers.
For example, the documentation for the C runtime library function sqrt says:
The sqrt function computes the nonnegative square root of its argument. A domain error occurs if its argument is negative.
The documentation on domain errors says:
On a domain error, the function returns an implementation-defined value; and the value EDOM is stored in errno.
The values returned by GetLastError are not the same as the values stored in errno, so I think that answers the question about using FormatMessage and strerror_s.
I don't know if WSAGetLastError always returns the same values as GetLastError (although I notice that the list of error codes returned by GetLastError does include the error codes that WSAGetLastError can return). See System Error Codes (9000-11999). If you look at the error codes starting at around 10000 you will see the WSAGetLastError error codes.
In any case, I personally would not rely on them returning the same values. Why would that be useful? Just follow the documentation and call WSAGetLastError for Winsock2 functions, and GetLastError for other WinAPI functions. NOTE: You can use FormatMessage on the error codes returned by either function.
GetLastError
andFormatMessage
are for Win32 errors.errno
andstrerror
are for C runtime errors. They are unrelated. Each function documents which error code it sets. – Gunzburgerrno
related functions in my program. Does this means that on Windows theperror()
andstrerror()
is different from one's Win32 API? It is really interesting question: I am porting right now a little application from a GNU/Linux, and it wouldn't be good ifstrerror()
suddenly refused to print a network errors. – AnthraceneFormatMessage
function can be used to obtain the message string for the returned error. – Blatterrno
at all in Windows. What kind of errors in principle could I got? In *nix I can got viaerrno
almost everything, but Windows doesn't seem to use it at all. Although it is a standard. This is sad. – Anthracenefopen
in a way like this:fopen("test.txt", "ABC")
, thenEINVAL
might be set toerrno
... (I haven't tested it by myself, sorry) – Blatt