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 并调用它。 公共语言运行时可以对覆盖操作系统维护的 GetLastError API 进行内部调用。

仅当应用System.Runtime.InteropServices.DllImportAttribute方法签名并将字段true设置为DllImportAttribute.SetLastError时,才能使用此方法获取错误代码。 此过程因所使用的源语言而异:C# 和 C++ 默认false,但Visual Basic中的 Declare 语句true

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

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

适用于

另请参阅