Marshal.GetLastWin32Error-Methode: ()

 

Veröffentlicht: Oktober 2016

Gibt den Fehlercode zurück, der durch die letzte nicht verwaltete Funktion zurückgegeben wurde, die mit einem Plattformaufruf aufgerufen wurde und bei der das DllImportAttribute.SetLastError-Flag festgelegt wurde.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (in mscorlib.dll)

Syntax

[SecurityCriticalAttribute]
public static int GetLastWin32Error()
public:
[SecurityCriticalAttribute]
static int GetLastWin32Error()
[<SecurityCriticalAttribute>]
static member GetLastWin32Error : unit -> int
<SecurityCriticalAttribute>
Public Shared Function GetLastWin32Error As Integer

Rückgabewert

Type: System.Int32

Legen Sie der letzte Fehlercode durch einen Aufruf der Win32- SetLastError Funktion.

Hinweise

GetLastWin32Error macht die Win32 GetLastError -Funktion von Kernel32.DLL. Diese Methode ist vorhanden, da sie nicht sicher ist, stellen Sie eine direkte Plattformaufruf ist GetLastError um diese Informationen abzurufen. Wenn Sie diesen Fehlercode zugreifen möchten, müssen Sie aufrufen GetLastWin32Error anstelle des Schreibens von Ihrer eigenen Definition des Plattformaufrufs für GetLastError und aufzurufen. Die common Language Runtime kann interne Aufrufe von APIs, mit denen überschreiben stellen die GetLastError vom Betriebssystem verwaltet.

Verwenden Sie diese Methode, um Fehlercodes zu erhalten, nur, wenn Sie anwenden der System.Runtime.InteropServices.DllImportAttribute auf die Methodensignatur und legen die SetLastError Feldtrue. Dieser Prozess variiert in Abhängigkeit von der verwendeten Quellsprache: c# und C++ sind false standardmäßig die Declare -Anweisung in Visual Basic ist true.

Beispiele

Das folgende Beispiel veranschaulicht den Aufruf der GetLastWin32ErrorMethode. Im Beispiel veranschaulicht zunächst den Aufruf der Methode kein Fehler aufgetreten ist, und dann wird das Aufrufen der Methode ein Fehler aufgetreten ist.

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

Sicherheit

SecurityCriticalAttribute

requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Versionsinformationen

Universelle Windows-Plattform
Verfügbar seit 8
.NET Framework
Verfügbar seit 1.1
Portierbare Klassenbibliothek
Unterstützt in: portierbare .NET-Plattformen
Silverlight
Verfügbar seit 2.0
Windows Phone Silverlight
Verfügbar seit 7.0
Windows Phone
Verfügbar seit 8.1

Siehe auch

DllImportAttribute
Marshal-Klasse
System.Runtime.InteropServices-Namespace

Zurück zum Anfang