Dealing with Win32 Errors C# with Example



Dealing with Win32 Errors C# with Example

When using interop methods, you can use GetLastError API to get additional information on you API calls. 
DllImport Attribute SetLastError Attribute 
SetLastError=true 
Indicates that the callee will call SetLastError (Win32 API function). 
SetLastError=false 
Indicates that the callee will not call SetLastError (Win32 API function), therefore you will not get an error 
information. 
When SetLastError isn't set, it is set to false (Default value). 
You can obtain the error code using Marshal.GetLastWin32Error Method: 
Example: 
[DllImport("kernel32.dll", SetLastError=true)] 
public static extern IntPtr OpenMutex(uint access, bool handle, string lpName); 
If you trying to open mutex which does not exist, GetLastError will return ERROR_FILE_NOT_FOUND. 
var lastErrorCode = Marshal.GetLastWin32Error(); 
 

if (lastErrorCode == (uint)ERROR_FILE_NOT_FOUND) 
{ 
//Deal  with  error 
} 
System Error Codes can be found here: 
https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx 
GetLastError API 
There is a native GetLastError API which you can use as well : 
[DllImport("coredll.dll", SetLastError=true)] 
static extern Int32 GetLastError(); 
When calling Win32 API from managed code, you must always use the Marshal.GetLastWin32Error. 
Here's why: 
Between your Win32 call which sets the error (calls SetLastError), the CLR can call other Win32 calls which could call 
SetLastError as well, this behavior can override your error value. In this scenario, if you call GetLastError you can 
obtain an invalid error. 
Setting SetLastError = true, makes sure that the CLR retrieves the error code before it executes other Win32 calls. 

0 Comment's

Comment Form

Submit Comment