Convert errno.h error values to Win32 GetLastError

2020-02-28 05:37发布

问题:

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).

回答1:

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.



回答2:

For each errno there are in general many possible GetLastError values, so what you're thinking may not necessarily be feasible.

Anyway, I just googled "errno to getlasterror", and the first google hit provided this correspondence list from the Cygwin sources.

It is the wrong way, GetLastError to errno, but perhaps helpful?

Cheers & hth.