DllImportAttribute.SetLastError 字段

定义

指示被调用方在从属性化方法返回之前是否调用 SetLastError Windows API 函数。Indicates whether the callee calls the SetLastError Windows API function before returning from the attributed method.

public: bool SetLastError;
public bool SetLastError;
val mutable SetLastError : bool
Public SetLastError As Boolean 

字段值

Boolean

示例

在某些情况下, Visual Basic 开发人员会使用DllImportAttribute代替 Declare 语句在托管代码中定义 DLL 函数。In some cases, Visual Basic developers use the DllImportAttribute, instead of using the Declare statement, to define a DLL function in managed code. 设置 SetLastError 字段便是这其中的一种情况。Setting the SetLastError field is one of those cases.

[DllImport("user32.dll", SetLastError = true)]
int MessageBoxA(IntPtr hWnd, String^ Text,
    String^ Caption, unsigned int Type);
internal static class NativeMethods
{
    [DllImport("user32.dll", SetLastError = true)]
    internal static extern int MessageBoxA(
        IntPtr hWnd, string lpText, string lpCaption, uint uType);
}
Friend Class NativeMethods
    <DllImport("user32.dll", SetLastError:=True)>
    Friend Shared Function MessageBoxA(hWnd As IntPtr, lpText As String,
        lpCaption As String, uType As UInteger) As Integer
    End Function
End Class

注解

true 指示被调用方将调用 SetLastError;否则,falsetrue to indicate that the callee will call SetLastError; otherwise, false. 默认为 falseThe default is false.

运行时封送拆收器调用 GetLastError 并缓存返回的值,以防止其他 API 调用覆盖它。The runtime marshaler calls GetLastError and caches the value returned to prevent it from being overwritten by other API calls. 可以通过调用 GetLastWin32Error来检索错误代码。You can retrieve the error code by calling GetLastWin32Error.

适用于

另请参阅