Marshal.GetHRForLastWin32Error 메서드

정의

Marshal을 사용하여 실행된 Win32 코드에서 발생한 마지막 오류에 해당하는 HRESULT를 반환합니다.Returns the HRESULT corresponding to the last error incurred by Win32 code executed using Marshal.

public:
 static int GetHRForLastWin32Error();
[System.Security.SecurityCritical]
public static int GetHRForLastWin32Error ();
public static int GetHRForLastWin32Error ();
[<System.Security.SecurityCritical>]
static member GetHRForLastWin32Error : unit -> int
static member GetHRForLastWin32Error : unit -> int
Public Shared Function GetHRForLastWin32Error () As Integer

반환

Int32

마지막 Win32 오류 코드에 해당하는 HRESULT입니다.The HRESULT corresponding to the last Win32 error code.

특성

예제

다음 예제에서는 메서드를 사용 하 여 Win32 오류 코드에 해당 하는 HRESULT를 검색 하는 방법을 보여 줍니다 GetHRForLastWin32Error .The following example demonstrates how to retrieve an HRESULT corresponding to a Win32 error code using the GetHRForLastWin32Error method.

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 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.

        int HRESULT = Marshal.GetHRForLastWin32Error();

        Console.WriteLine("The last Win32 Error was: " + HRESULT);
    }

    static void Main(string[] args)
    {
        Run();
    }
}
// This code example displays the following to the console:
//
// Calling Win32 MessageBox with error...
// The last Win32 Error was: -2147023496
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 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.
        Dim HRESULT As Integer

        HRESULT = Marshal.GetHRForLastWin32Error()

        Console.WriteLine("The last Win32 Error was: " + HRESULT)

    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 with error...
' The last Win32 Error was: -2147023496

설명

대상 함수에는 setLastError 메타 데이터 플래그가 설정 되어 있어야 합니다.The target function must have had the setLastError metadata flag set. 예를 들어 SetLastError 의 필드는 System.Runtime.InteropServices.DllImportAttribute 이어야 합니다 true .For example, the SetLastError field of the System.Runtime.InteropServices.DllImportAttribute must be true. 이 플래그를 설정 하는 프로세스는 사용 된 소스 언어에 따라 달라 집니다. c # 및 c + +는 false 기본적으로 이지만 Declare Visual Basic의 문은 true 입니다.The process for setting this flag depends on the source language used: C# and C++ are false by default, but the Declare statement in Visual Basic is true.

적용 대상

추가 정보