DllImportAttribute.SetLastError Field

 

Indicates whether the callee calls the SetLastError Win32 API function before returning from the attributed method.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

public bool SetLastError

Field Value

Type: System.Boolean

true to indicate that the callee will call SetLastError; otherwise, false. The default is false.

The runtime marshaler calls GetLastError and caches the value returned to prevent it from being overwritten by other API calls. You can retrieve the error code by calling GetLastWin32Error.

In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. Setting the SetLastError field is one of those cases.

public class Win32
{
    [DllImport("user32.dll", SetLastError=true)]
    public static extern int MessageBoxA(IntPtr hWnd, String text,
                                String caption, uint type);
}

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show: