Marshal.GetLastWin32Error Méthode

Définition

Retourne le code d'erreur retourné par la dernière fonction non managée appelée en utilisant l'appel de code non managé dont l'indicateur SetLastError est activé.

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

Retours

Dernier code d’erreur défini par un appel à la fonction Win32 SetLastError.

Attributs

Exemples

L’exemple suivant appelle la GetLastWin32Error méthode . L’exemple montre d’abord l’appel de la méthode sans erreur, puis illustre l’appel de la méthode avec une erreur présente.

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

Remarques

Sur les systèmes Windows, GetLastWin32Error expose la fonction Win32 GetLastError à partir de Kernel32.DLL. Cette méthode existe, car il n’est pas fiable de faire appel à une plateforme directe pour GetLastError obtenir ces informations. Si vous souhaitez accéder à ce code d’erreur, vous devez appeler GetLastWin32Error au lieu d’écrire votre propre définition d’appel de plateforme pour GetLastError et l’appeler. Le Common Language Runtime peut effectuer des appels internes aux API qui remplacent le GetLastError géré par le système d’exploitation.

Vous pouvez utiliser cette méthode pour obtenir des codes d’erreur uniquement si vous appliquez le System.Runtime.InteropServices.DllImportAttribute à la signature de méthode et si vous définissez le DllImportAttribute.SetLastError champ sur true. Le processus pour cela varie en fonction du langage source utilisé : C# et C++ sont false par défaut, mais l’instruction Declare en Visual Basic est true.

Il existe une différence dans le comportement de la GetLastWin32Error méthode sur .NET Core et .NET Framework quand DllImportAttribute.SetLastError est true. Sur .NET Framework, la GetLastWin32Error méthode peut conserver les informations d’erreur d’un appel P/Invoke au suivant. Sur .NET Core, les informations d’erreur sont effacées avant l’appel P/Invoke et représente uniquement les GetLastWin32Error informations d’erreur du dernier appel de méthode.

Sur .NET 6 et versions ultérieures, cette méthode est fonctionnellement équivalente à GetLastPInvokeError, qui est nommée pour mieux refléter l’intention de l’API et sa nature multiplateforme. GetLastPInvokeError doit être préféré à GetLastWin32Error.

S’applique à

Voir aussi