Marshal.GetHRForLastWin32Error メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Dim returnValue As Integer returnValue = Marshal.GetHRForLastWin32Error
最終 Win32 エラー コードに対応する HRESULT。

対象の関数には、setLastError メタデータ フラグが設定されている必要があります。たとえば、System.Runtime.InteropServices.DllImportAttribute の SetLastError フィールドは true にする必要があります。このプロセスは、使用するソース言語によって異なります。C# および C++ の場合は false が既定値ですが、Visual Basic の Declare ステートメントの場合は true です。
![]() |
---|
このメソッドは SecurityAction.LinkDemand を使用して、信頼関係のないコードからの呼び出しを防ぎます。SecurityPermissionAttribute.UnmanagedCode アクセス許可は、直前の呼び出し元にのみ要求されます。信頼性が一部しか確認されていないコードから呼び出すことができるコードの場合、ユーザー入力を検証せずに Marshal クラスに渡すことは避けてください。LinkDemand メンバの使用に関する重要な制約事項については、「Demand と LinkDemand」を参照してください。 |

GetHRForLastWin32Error メソッドを使用して、Win32 エラー コードに対応する HRESULT を取得する方法を次のコード例に示します。
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)> _ 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
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)] 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

- SecurityPermission (アンマネージ コードを呼び出すために必要なアクセス許可)。 UnmanagedCode (関連する列挙体)。LinkDemand (セキュリティ アクション)。

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からMarshal.GetHRForLastWin32Error メソッドを検索する場合は、下記のリンクをクリックしてください。

- Marshal.GetHRForLastWin32Error メソッドのページへのリンク