GetLastError(), errno, FormatMessageA() and strerror_s()?
I'm confused as to the exact relationship between GetLastError() and errno. Are they the same numerical values, or something completely different? How do I know which one I should check?
And if I want to convert an error code to a string for debugging, can I use FormatMessageA() interchangeably with strerror_s()?
Lastly, is it true that WSAGetLastError() always returns the same as GetLastError(), or could they both return different values?
There is no relationship between GetLastError and errno. GetLastError gets the last error that occured in a Windows API function (for the current thread). errno contains the last error that occurred in the C runtime library (I think this is also thread specific for the Microsoft C runtime library). So if you call a winapi function like CreateFile you check GetLastError (assuming that the function call failed), while if you call a C runtime library function like fopen you check errno (again assumming that the call failed).
I think that also 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.