Marshal.GetHRForLastWin32Error メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Marshal.GetHRForLastWin32Error メソッドの意味・解説 

Marshal.GetHRForLastWin32Error メソッド

Marshal使用して実行した Win32 コード原因最終エラー対応する HRESULT返します

名前空間: System.Runtime.InteropServices
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function GetHRForLastWin32Error
 As Integer
Dim returnValue As Integer

returnValue = Marshal.GetHRForLastWin32Error
public static int GetHRForLastWin32Error
 ()
public:
static int GetHRForLastWin32Error ()
public static int GetHRForLastWin32Error
 ()
public static function GetHRForLastWin32Error
 () : int

戻り値
最終 Win32 エラー コード対応する HRESULT

解説解説

対象関数には、setLastError メタデータ フラグ設定されている必要があります。たとえば、System.Runtime.InteropServices.DllImportAttribute の SetLastError フィールドtrue にする必要があります。このプロセスは、使用するソース言語によって異なりますC# および C++ の場合false既定値ですが、Visual BasicDeclare ステートメント場合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
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Marshal クラス
Marshal メンバ
System.Runtime.InteropServices 名前空間
DllImportAttribute クラス



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Marshal.GetHRForLastWin32Error メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Marshal.GetHRForLastWin32Error メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS