Is it possible to access the "errno" variable in C# when P/Invoking? This is similar to Win32 GetLastError().
I'm fairly sure that there is a way, but it probably is a bad idea. How would you guarantee that the runtime has not called some CRT function during its internal processing that has affected the errno
?
For the same reason, you should not call GetLastError
directly either. The DllImportAttribute
provides a SetLastError
property so the runtime knows to immediately capture the last error and store it in a place that the managed code can read using Marshal.GetLastWin32Error
.
I think the most robust thing you could do in this case is make a C DLL that performs both the actual C work and the capture of the errno
. (Note that just writing a wrapper around the errno
capture would still have the concerns mentioned above.)