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)
.
GetLastError() returns an integer value, not a text message.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With