Marshal.GetHRForLastWin32Error メソッド
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
Marshal を使用して実行した Win32 コードが原因の最終エラーに対応する HRESULT を返します。
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 エラー コードに対応する HRESULT を取得する方法を GetHRForLastWin32Error 示します。
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
。
製品 | バージョン |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET に関するフィードバック
.NET はオープンソース プロジェクトです。 フィードバックを提供するにはリンクを選択します。