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

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

Marshal.GetLastWin32Error メソッド

DllImportAttribute.SetLastError フラグ設定したプラットフォーム呼び出し使用して呼び出した最終アンマネージ関数によって返されるエラー コード返します

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

Public Shared Function GetLastWin32Error
 As Integer
Dim returnValue As Integer

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

戻り値
Win32 SetLastError API メソッドへの呼び出し最後に設定されエラー コード

解説解説

GetLastWin32Error は、Kernel32.DLL の Win32 GetLastError API メソッド公開します。このメソッド存在する理由は、GetLastError へのプラットフォーム呼び出し直接行ってこの情報取得することが安全ではないためです。このエラー コードアクセスするには、GetLastErrorプラットフォーム呼び出し独自に定義して呼び出すのではなくGetLastWin32Error呼び出す必要があります共通言語ランタイムは、オペレーティング システム保持している GetLastError上書きする API内部呼び出しできます

System.Runtime.InteropServices.DllImportAttribute をメソッドシグネチャ適用しSetLastError フィールドtrue設定した場合限り、このメソッド使用してエラー コード取得できます。このプロセスは、使用するソース言語によって異なりますC# および C++ の場合false既定値ですが、Visual BasicDeclare ステートメント場合true です。GetLastErrorSetLastError の各 Win32 API メソッド詳細については、MSDN ライブラリ参照してください

メモメモ

このメソッドは SecurityAction.LinkDemand を使用して信頼関係のないコードからの呼び出し防ぎます。SecurityPermissionAttribute.UnmanagedCode アクセス許可は、直前呼び出し元にのみ要求されます。信頼性一部しか確認されていないコードから呼び出すことができるコード場合ユーザー入力検証せずに Marshal クラスに渡すことは避けてくださいLinkDemand メンバ使用に関する重要な制約事項については、「Demand と LinkDemand」を参照してください

使用例使用例

GetLastWin32Error メソッド呼び出すコード例次に示します。このコード例では、まずエラーのない状態でのメソッド呼び出し示し次にエラー発生した状態でのメソッド呼び出し示します

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

        Console.WriteLine("Calling Win32 MessageBox without error...")

        Win32.MessageBox(New IntPtr(0), "Press
 OK...", "Press OK Dialog", 0)

        ' Get the last error and display it.
        Dim errorVal As Integer

        errorVal = Marshal.GetLastWin32Error()

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

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

        errorVal = Marshal.GetLastWin32Error()

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

    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 without error...
' The last Win32 Error was: 0
' Calling Win32 MessageBox with error...
' The last Win32 Error was: 1400
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 normal parameters.

        Console.WriteLine("Calling Win32 MessageBox without error...");

        Win32.MessageBox(new IntPtr(0), "Press OK...",
 "Press OK Dialog", 0);

        // Get the last error and display it.
        int error = Marshal.GetLastWin32Error();

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

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

        error = Marshal.GetLastWin32Error();

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

    static void Main(string[]
 args)
    {
        Run();
    }
}
// This code example displays the following to the console: 
//
// Calling Win32 MessageBox without error...
// The last Win32 Error was: 0
// Calling Win32 MessageBox with error...
// The last Win32 Error was: 1400
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Marshal クラス
Marshal メンバ
System.Runtime.InteropServices 名前空間
DllImportAttribute クラス


このページでは「.NET Framework クラス ライブラリ リファレンス」からMarshal.GetLastWin32Error メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMarshal.GetLastWin32Error メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMarshal.GetLastWin32Error メソッド を検索

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

辞書ショートカット

すべての辞書の索引

「Marshal.GetLastWin32Error メソッド」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS