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 Basic の Declare ステートメントの場合は true です。GetLastError と SetLastError の各 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 クラス



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

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

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

   

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



Marshal.GetLastWin32Error メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS