Marshal.GetLastWin32Error 方法

定义

返回由上一个非托管函数返回的错误代码,该函数是使用设置了 SetLastError 标志的平台调用来的。

public:
 static int GetLastWin32Error();
[System.Security.SecurityCritical]
public static int GetLastWin32Error ();
public static int GetLastWin32Error ();
[<System.Security.SecurityCritical>]
static member GetLastWin32Error : unit -> int
static member GetLastWin32Error : unit -> int
Public 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方法签名并将 字段true设置为 DllImportAttribute.SetLastError 时,才能使用此方法获取错误代码。 此过程因所使用的源语言而异:默认情况下,C# 和 C++ 为 false ,但 Declare Visual Basic 中的 语句为 true

方法在 .NET Core 上的行为GetLastWin32Error存在差异,当 为 时DllImportAttribute.SetLastErrortrue.NET Framework。 在.NET Framework, GetLastWin32Error 方法可以保留从一个 P/Invoke 调用到下一个调用的错误信息。 在 .NET Core 上,错误信息在 P/Invoke 调用之前被清除,并且 GetLastWin32Error 仅表示最后一个方法调用中的错误信息。

在 .NET 6 及更高版本上,此方法在功能上等效于 GetLastPInvokeError,它被命名以更好地反映 API 的意图及其跨平台性质。 GetLastPInvokeError 应优先于 GetLastWin32Error

适用于

另请参阅