Merrion
Well-known member
I am trying to get the last API error message thus:-
But lErrorMessageLength seems to always return 0? I am using Windows XP.
Thanks in advance,
Duncan
Code:
Public Enum Format_Message_Flags
FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
FORMAT_MESSAGE_IGNORE_INSERTS = &H200
FORMAT_MESSAGE_FROM_STRING = &H400
FORMAT_MESSAGE_FROM_HMODULE = &H800
FORMAT_MESSAGE_FROM_SYSTEM = &H1000
FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
End Enum
<DllImport("kernel32.dll", EntryPoint:="FormatMessageA", _
CharSet:=CharSet.Ansi, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Private Function FormatMessage(ByVal dwFlags As Format_Message_Flags, ByVal lpSource As Int32, ByVal dwMessageId As Int32, ByVal dwLanguageId As Int32, ByVal lpBuffer As String, ByVal nSize As Int32, ByVal Arguments As Int32) As Int32
End Function
Public Function GetAPIErrorMessageDescription(ByVal ApiErrNumber As Int32) As String
Dim sError As New String("", MAX_MESSAGE_LENGTH)
Dim lErrorMessageLength As Int32
lErrorMessageLength = FormatMessage(Format_Message_Flags.FORMAT_MESSAGE_FROM_SYSTEM, 0, ApiErrNumber, 0, sError, MAX_MESSAGE_LENGTH, 0)
If lErrorMessageLength > 0 Then
sError.Substring(0, lErrorMessageLength)
End If
GetAPIErrorMessageDescription = sError
End Function
But lErrorMessageLength seems to always return 0? I am using Windows XP.
Thanks in advance,
Duncan