[DllImport("kernel32.dll")]staticexternuintFormatMessage(uint dwFlags,IntPtr lpSource,uint dwMessageId,uint dwLanguageId, [Out] StringBuilder lpBuffer,uint nSize,IntPtr Arguments);// the version, the sample is built upon:[DllImport("Kernel32.dll", SetLastError=true)]staticexternuintFormatMessage( uint dwFlags,IntPtr lpSource,uint dwMessageId,uint dwLanguageId,refIntPtr lpBuffer,uint nSize,IntPtr pArguments);// the parameters can also be passed as a string array:[DllImport("Kernel32.dll", SetLastError=true)]staticexternuintFormatMessage( uint dwFlags,IntPtr lpSource,uint dwMessageId,uint dwLanguageId,refIntPtr lpBuffer,uint nSize,string[] Arguments);// simplified for usability[DllImport("kernel32.dll")]staticexternintFormatMessage(FORMAT_MESSAGE dwFlags,IntPtr lpSource,int dwMessageId,uint dwLanguageId,outStringBuilder msgOut,int nSize,IntPtr Arguments);
<DllImport("Kernel32.dll", EntryPoint:="FormatMessageW", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As [String], ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function
<DllImport("Kernel32.dll", EntryPoint:="FormatMessageW", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, <MarshalAs(UnmanageType.LPWStr)> ByRef lpBuffer As String, ByVal nSize As Integer, ByVal Arguments As IntPtr) As Integer
End Function
<DllImport("Kernel32.dll", EntryPoint:="FormatMessageW", SetLastError:=True, CharSet:=CharSet.Unicode, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As IntPtr) As Integer
End Function
enum FORMAT_MESSAGE : uint
{
ALLOCATE_BUFFER = 0x00000100,
IGNORE_INSERTS = 0x00000200,
FROM_SYSTEM = 0x00001000,
ARGUMENT_ARRAY = 0x00002000,
FROM_HMODULE = 0x00000800,
FROM_STRING = 0x00000400
}
[DllImport("kernel32.dll")]
static extern int FormatMessage(FORMAT_MESSAGE dwFlags, IntPtr lpSource, int dwMessageId, uint dwLanguageId, out StringBuilder msgOut, int nSize, IntPtr Arguments);
//
public static int GetLastError()
{
return (Marshal.GetLastWin32Error());
}
public static string GetLastErrorString()
{
int lastError = GetLastError();
if (0 == lastError) return ("");
else
{
StringBuilder msgOut = new StringBuilder(256);
int size = FormatMessage(FORMAT_MESSAGE.ALLOCATE_BUFFER | FORMAT_MESSAGE.FROM_SYSTEM | FORMAT_MESSAGE.IGNORE_INSERTS,
IntPtr.Zero, lastError, 0, out msgOut, msgOut.Capacity, IntPtr.Zero);
return (msgOut.ToString().Trim());
}
}
<System.Runtime.InteropServices.DllImport("Kernel32.dll", EntryPoint:="FormatMessageW", SetLastError:=True, CharSet:=System.Runtime.InteropServices.CharSet.Unicode)>
Public Shared Function FormatMessage(ByVal dwFlags As Integer, ByRef lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByRef lpBuffer As IntPtr, ByVal nSize As Integer, ByRef Arguments As IntPtr) As Integer
End Function
<System.Runtime.InteropServices.DllImport("Kernel32.dll", SetLastError:=True)>
Public Shared Function LocalFree(ByVal hMem As IntPtr) As IntPtr
End Function
Enum Format_Message
FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100 'The caller should use the LocalFree function to free the buffer when it is no longer needed
FORMAT_MESSAGE_IGNORE_INSERTS = &H200
FORMAT_MESSAGE_FROM_SYSTEM = &H1000
'FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
'FORMAT_MESSAGE_FROM_HMODULE = &H800
''FORMAT_MESSAGE_FROM_STRING = &H400
End Enum
Private Sub Error_Message()
'DllImport filed SetLastError must be set True
'so Runtime.InteropServices.Marshal.GetLastWin32Error() returns a valid error
Dim nLastError As Integer = Runtime.InteropServices.Marshal.GetLastWin32Error
Dim lpMsgBuf As IntPtr = IntPtr.Zero
Dim dwChars As Integer = FormatMessage(Format_Message.FORMAT_MESSAGE_ALLOCATE_BUFFER Or Format_Message.FORMAT_MESSAGE_FROM_SYSTEM Or Format_Message.FORMAT_MESSAGE_IGNORE_INSERTS,
IntPtr.Zero, nLastError, 0, lpMsgBuf, 0, IntPtr.Zero)
If dwChars = 0 Then
Error_Message()
Else
Dim sRet As String = Runtime.InteropServices.Marshal.PtrToStringAuto(lpMsgBuf, dwChars)
MessageBox.Show(sRet, "Error_Message()", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
'Format_Message.FORMAT_MESSAGE_ALLOCATE_BUFFER
'The caller should use the LocalFree function to free the buffer when it is no longer needed
If LocalFree(lpMsgBuf) <> 0 Then
Error_Message()
End If
End Sub