Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert errno.h error values to Win32 GetLastError() equivalents

I'm writing a layer between a POSIX filesystem, and Windows using Dokan, and need to convert error values of the errno kind (EINVAL, ENOENT, etc.), to the Win32 equivalents you'd receive when calling GetLastError() (such as ERROR_INVALID_PARAMETER).

Is there an existing function, library, or reference I can use to perform these conversions?

I typically poke through the Python source for inspiration on these matters, but Python neatly avoids this need (at least as far as I can tell).

As an example, EINVAL (22) would convert to ERROR_INVALID_PARAMETER (87).

like image 229
Matt Joiner Avatar asked Oct 17 '10 07:10

Matt Joiner


People also ask

What is the return type of GetLastError () function?

GetLastError() returns an integer value, not a text message.

What can GetLastError () function do explain?

An application can retrieve the last-error code by using the GetLastError function; the error code may tell more about what actually occurred to make the function fail. The documentation for system functions will indicate the conditions under which the function sets the last-error code.

Does Errno work on Windows?

The errno values in a 32-bit Windows operating system are a subset of the values for errno in UNIX systems. The errno value isn't necessarily the same as the actual error code returned by a system call from the Windows operating system.

How do you use last-error?

If your application needs more details about an error, it can retrieve the last-error code using the GetLastError function and display a description of the error using the FormatMessage function. The following example includes an error-handling function that prints the error message and terminates the process.


1 Answers

I had done an experiment about this subject in the past, mostly based on Microsoft DOSMAP.CPP unit. However, I cancelled the project at that time because the error mapping was not always right for particular error codes. For Example, not every POSIX version return EINVAL for ERROR_INVALID_ACCESS, some of them return EACCES instead. I also had made a comparison between errno.h system error numbers of POSIX.1-2008 and DOSMAP.CPP, mingw.c, Postgresql error.c, tclWinError.c, MySQL my_winerr.c and many more; sometimes, the mapping rule differ among them for particular error codes. Personally, I suggest you to deal with only the consistent error-code mapping among them.

like image 98
Vantomex Avatar answered Oct 05 '22 08:10

Vantomex