Marshal.GetHRForLastWin32Error 方法    
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回 HRESULT,它对应于使用 Marshal 执行的 Win32 代码引起的最后一个错误。
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
	返回
对应于最后一个 Win32 错误代码的 HRESULT。
- 属性
 
示例
以下示例演示如何使用 方法检索对应于 Win32 错误代码的 GetHRForLastWin32Error HRESULT。
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 设置元数据标志。 例如, SetLastError 的 System.Runtime.InteropServices.DllImportAttribute 字段必须是 true。 设置此标志的过程取决于所使用的源语言:默认情况下,C# 和 C++ 为 false ,但 Declare Visual Basic 中的 语句为 true。