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

Dim returnValue As Integer returnValue = Marshal.GetLastWin32Error
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

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

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


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

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