.5 ' using System.Runtime.InteropServices;
<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
Private Shared Function FormatMessage(ByVal dwFlags As UInteger, ByVal lpSource As IntPtr, ByVal dwMessageId As UInteger, ByVal dwLanguageId As UInteger, ByRef lpBuffer As IntPtr, ByVal nSize As UInteger, _
ByVal Arguments As IntPtr) As UInteger
End Function
Public Function GetLastWin32ErrorMessage() As String
Return GetLastWin32ErrorMessage(Marshal.GetLastWin32Error())
End Function
Public Function GetLastWin32ErrorMessage(ByVal errorCode As Integer) As String
Dim buffer As IntPtr = IntPtr.Zero
Dim cnt As UInteger = FormatMessage(256 Or 512 Or 4096, IntPtr.Zero, DirectCast(errorCode, UInteger), 0, buffer, 0, _
IntPtr.Zero)
If cnt = 0 OrElse buffer = IntPtr.Zero Then
Return [String].Format("Unknown error: {0}.", errorCode)
End IfDim errorMessage As String = Marshal.PtrToStringAuto(buffer, DirectCast(cnt, Integer))
Marshal.FreeHGlobal(buffer)
Return errorMessage
End Function