Marshal.GetLastWin32Error メソッド

定義

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

public:
 static int GetLastWin32Error();
[System.Security.SecurityCritical]
public static int GetLastWin32Error ();
public static int GetLastWin32Error ();
[<System.Security.SecurityCritical>]
static member GetLastWin32Error : unit -> int
static member GetLastWin32Error : unit -> int
Public Shared Function GetLastWin32Error () As Integer

戻り値

Win32 SetLastError 関数への呼び出しで最後に設定されたエラー コード。

属性

次の例では、 メソッドを GetLastWin32Error 呼び出します。 この例では、最初にエラーが存在しないメソッドの呼び出しを示し、次にエラーが存在するメソッドの呼び出しを示します。

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

注釈

Windows システムでは、 GetLastWin32Error Kernel32.DLLから Win32 GetLastError 関数を公開します。 このメソッドは、この情報を取得するために直接プラットフォーム呼び出しを行う信頼性がないために GetLastError 存在します。 このエラー コードにアクセスする場合は、独自のプラットフォーム呼び出し定義GetLastErrorを記述して呼び出GetLastWin32Errorす代わりに を呼び出す必要があります。 共通言語ランタイムは、オペレーティング システムによって管理される を上書き GetLastError する API に対して内部呼び出しを行うことができます。

このメソッドを使用すると、メソッド シグネチャに をSystem.Runtime.InteropServices.DllImportAttribute適用し、フィールドtrueを に設定した場合にのみエラー コードをDllImportAttribute.SetLastError取得できます。 このプロセスは、使用されるソース言語によって異なります。C# と C++ は false 既定でですが、 Declare Visual Basic の ステートメントは です true

.NET Core での メソッドの動作と が GetLastWin32Error の場合DllImportAttribute.SetLastErrortrueは.NET Frameworkの動作に違いがあります。 .NET Framework、メソッドはGetLastWin32Error、ある P/Invoke 呼び出しから次の呼び出しまでのエラー情報を保持できます。 .NET Core では、P/Invoke 呼び出しの前にエラー情報がクリアされ GetLastWin32Error 、 は最後のメソッド呼び出しからのエラー情報のみを表します。

.NET 6 以降のバージョンでは、このメソッドは 機能的には と同等 GetLastPInvokeErrorです。これは、API の意図とそのクロスプラットフォームの性質をより適切に反映するようにという名前が付けられています。 GetLastPInvokeError より優先される GetLastWin32Error必要があります。

適用対象

こちらもご覧ください