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?

Answers


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.


Need Your Help

How to disable a layout for a specific mailer?

ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 actionmailer

I need to be able to disable a certain layout for a specific email. I've taken a look at Rails for ActionMailer - How to disable a Layout for a certain mailer however I don't know where to and how to

Slow data load with conversion from nvarchar to varchar

sql sql-server-2008 ssis

I have a table which has approximately 140 columns. The data for this table comes from a transactional system and many of the columns are Unicode.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.