Marshal.GetLastWin32Error Metoda

Definicja

Zwraca kod błędu zwrócony przez ostatnią niezarządzaną funkcję, która została wywołana przy użyciu wywołania platformy z ustawioną flagą 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

Zwraca

Ostatni kod błędu ustawiony przez wywołanie funkcji Win32 SetLastError .

Atrybuty

Przykłady

Poniższy przykład wywołuje metodę GetLastWin32Error . W przykładzie najpierw pokazano wywołanie metody bez wystąpienia błędu, a następnie demonstruje wywołanie metody z obecnym błędem.

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

Uwagi

W systemach GetLastWin32Error Windows uwidacznia funkcję Win32 GetLastError z Kernel32.DLL. Ta metoda istnieje, ponieważ nie jest niezawodna, aby wywołać wywołanie platformy bezpośredniej w celu GetLastError uzyskania tych informacji. Jeśli chcesz uzyskać dostęp do tego kodu błędu, musisz wywołać GetLastWin32Error metodę zamiast pisać własną definicję wywołania platformy i GetLastError wywoływać ją. Środowisko uruchomieniowe języka wspólnego może wykonywać wywołania wewnętrzne interfejsów API, które zastępują GetLastError obsługiwane przez system operacyjny.

Tej metody można użyć do uzyskania kodów błędów tylko wtedy, gdy zastosujesz System.Runtime.InteropServices.DllImportAttribute metodę do podpisu metody i ustaw DllImportAttribute.SetLastError pole na truewartość . Proces ten różni się w zależności od używanego języka źródłowego: C# i C++ są false domyślnie, ale Declare instrukcja w języku Visual Basic to true.

Istnieje różnica w zachowaniu GetLastWin32Error metody na platformie .NET Core i .NET Framework, gdy DllImportAttribute.SetLastError ma wartość true. Na .NET Framework GetLastWin32Error metoda może zachować informacje o błędzie z jednego wywołania P/Invoke do następnego. Na platformie .NET Core informacje o błędzie są czyszczone przed wywołaniem P/Invoke, a element GetLastWin32Error reprezentuje tylko informacje o błędzie z ostatniego wywołania metody.

Na platformie .NET 6 i nowszych wersjach ta metoda jest funkcjonalnie równoważna GetLastPInvokeErrornazwie , która jest nazwana w celu lepszego odzwierciedlenia intencji interfejsu API i jej charakteru międzyplatformowego. GetLastPInvokeError opcja powinna być preferowana przez GetLastWin32Errorwartość .

Dotyczy

Zobacz też