Marshal.GetLastWin32Error 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回由上一个非托管函数返回的错误代码,该函数是使用设置了 SetLastError 标志的平台调用来的。
public:
 static int GetLastWin32Error();[System.Security.SecurityCritical]
public static int GetLastWin32Error();public static int GetLastWin32Error();[<System.Security.SecurityCritical>]
static member GetLastWin32Error : unit -> intstatic member GetLastWin32Error : unit -> intPublic Shared Function GetLastWin32Error () As Integer返回
通过调用 Win32 SetLastError 函数设置的最后一个错误代码。
- 属性
示例
以下示例调用 GetLastWin32Error 方法。 该示例首先演示如何调用没有出现错误的方法,然后演示如何调用存在错误的 方法。
using System;
using System.Runtime.InteropServices;
internal class Win32
{
    // Use DllImportAttribute to inport the Win32 MessageBox
    // function.  Set the SetLastError flag to true to allow
    // the function to set the Win32 error.
    [DllImportAttribute("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hwnd, String text, String caption, uint type);
}
class Program
{
    static void Run()
    {
        // Call the MessageBox with normal parameters.
        Console.WriteLine("Calling Win32 MessageBox without error...");
        Win32.MessageBox(new IntPtr(0), "Press OK...", "Press OK Dialog", 0);
        // Get the last error and display it.
        int error = Marshal.GetLastWin32Error();
        Console.WriteLine("The last Win32 Error was: " + error);
        // Call the MessageBox with an invalid window handle to
        // produce a Win32 error.
        Console.WriteLine("Calling Win32 MessageBox with error...");
        Win32.MessageBox(new IntPtr(123132), "Press OK...", "Press OK Dialog", 0);
        // Get the last error and display it.
        error = Marshal.GetLastWin32Error();
        Console.WriteLine("The last Win32 Error was: " + error);
    }
    static void Main(string[] args)
    {
        Run();
    }
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox without error...
// The last Win32 Error was: 0
// Calling Win32 MessageBox with error...
// The last Win32 Error was: 1400
Imports System.Runtime.InteropServices
Module Win32
    ' Use DllImportAttribute to inport the Win32 MessageBox
    ' function.  Set the SetLastError flag to true to allow
    ' the function to set the Win32 error.
    <DllImportAttribute("user32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Function MessageBox(ByVal hwnd As IntPtr, ByVal text As String, ByVal caption As String, ByVal type As UInt32) As Integer
    End Function
End Module
Module Program
    Sub Run()
        ' Call the MessageBox with normal parameters.
        Console.WriteLine("Calling Win32 MessageBox without error...")
        Win32.MessageBox(New IntPtr(0), "Press OK...", "Press OK Dialog", 0)
        ' Get the last error and display it.
        Dim errorVal As Integer
        errorVal = Marshal.GetLastWin32Error()
        Console.WriteLine("The last Win32 Error was: " + errorVal)
        ' Call the MessageBox with an invalid window handle to
        ' produce a Win32 error.
        Console.WriteLine("Calling Win32 MessageBox with error...")
        Win32.MessageBox(New IntPtr(123132), "Press OK...", "Press OK Dialog", 0)
        ' Get the last error and display it.
        errorVal = Marshal.GetLastWin32Error()
        Console.WriteLine("The last Win32 Error was: " + errorVal)
    End Sub
    Sub Main(ByVal args() As String)
        Run()
    End Sub
End Module
' This code example displays the following to the console: 
'
' Calling Win32 MessageBox without error...
' The last Win32 Error was: 0
' Calling Win32 MessageBox with error...
' The last Win32 Error was: 1400
注解
在 Windows 系统上, GetLastWin32Error 从 Kernel32.DLL 公开 Win32 GetLastError 函数。 之所以存在此方法,是因为对 进行直接平台调用 GetLastError 来获取此信息是不可靠的。 如果要访问此错误代码,则必须调用 GetLastWin32Error 而不是编写 GetLastError 自己的平台调用定义并调用它。 公共语言运行时可以对覆盖操作系统维护的 的 API GetLastError 进行内部调用。
仅当将 应用于 System.Runtime.InteropServices.DllImportAttribute 方法签名并将 字段设置为 DllImportAttribute.SetLastErrortrue时,才能使用此方法获取错误代码。 此过程因所使用的源语言而异:默认情况下,C# 和 C++ 为 false ,但 Declare Visual Basic 中的语句为 true。
当 为 时DllImportAttribute.SetLastErrortrue,方法在 .NET Core 和 .NET Framework 上的行为GetLastWin32Error存在差异。 在 .NET Framework 上 GetLastWin32Error ,方法可以保留对下一个 P/Invoke 调用的错误信息。 在 .NET Core 上,错误信息在 P/Invoke 调用之前被清除,并且 GetLastWin32Error 仅表示最后一个方法调用中的错误信息。
在 .NET 6 及更高版本中,此方法在功能上等效于 GetLastPInvokeError,它命名为 以更好地反映 API 的意图及其跨平台性质。 GetLastPInvokeError 应优先于 GetLastWin32Error。