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 것이 신뢰할 수 없기 때문에 존재합니다. 이 오류 코드에 액세스하려면 사용자 고유의 플랫폼 호출 정의를 GetLastError 작성하고 호출하는 대신 를 호출 GetLastWin32Error 해야 합니다. 공용 언어 런타임은 운영 체제에서 유지 관리하는 를 덮어쓰 GetLastError 는 API에 대한 내부 호출을 수행할 수 있습니다.

메서드 서명에 를 적용 System.Runtime.InteropServices.DllImportAttribute 하고 필드를 true로 설정하는 DllImportAttribute.SetLastError 경우에만 이 메서드를 사용하여 오류 코드를 가져올 수 있습니다. 이 프로세스는 사용되는 원본 언어에 따라 다릅니다. C# 및 C++는 false 기본적으로 이지만 Visual Basic의 Declare 문은 입니다 true.

.NET Core에서 메서드의 GetLastWin32Error 동작에 차이가 있으며 가 인 경우 DllImportAttribute.SetLastErrortrue.NET Framework. .NET Framework 메서드는 GetLastWin32Error 한 P/Invoke 호출에서 다음 호출로 오류 정보를 유지할 수 있습니다. .NET Core에서 오류 정보는 P/Invoke 호출 전에 지워지고 는 GetLastWin32Error 마지막 메서드 호출의 오류 정보만 나타냅니다.

.NET 6 이상 버전에서 이 메서드는 API의 의도와 플랫폼 간 특성을 더 잘 반영하도록 명명된 와 기능적으로 동일합니다 GetLastPInvokeError. GetLastPInvokeError 은 에 비해 GetLastWin32Error선호되어야 합니다.

적용 대상

추가 정보