声明此功能:
<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
基于此处的定义:https://pinvoke.net/default.aspx/kernel32/FormatMessage.html
场景
我目前正在Pwinvoking wininet.dll对服务器执行FTP事务。如果遇到错误,我会从 Err.LastDllError 返回错误代码。我定义了一个函数,该函数获取dll错误并根据错误代码返回一条消息,但是它无法按预期工作。这是我用来引发dll错误的函数:
Private Sub ThrowLastDllError()
Dim iLastErrorID As Integer = Err.LastDllError
Dim iMessageBuffer As IntPtr = Nothing
Dim iModuleHandle As IntPtr = GetModuleHandle("wininet.dll")
Dim sMessageBuffer As String = Nothing
If iLastErrorID > 12000 And iLastErrorID < 12157 Then
FormatMessage(FORMAT_MESSAGE_FROM_HMODULE Or
FORMAT_MESSAGE_IGNORE_INSERTS Or
FORMAT_MESSAGE_ALLOCATE_BUFFER,
iModuleHandle,
iLastErrorID,
0,
sMessageBuffer,
256,
Nothing)
Debugger.Break()
'TODO: Throw exception with error code message here
End If
End Sub
基于此处描述的技术:https://docs.microsoft.com/en-us/windows/desktop/wininet/appendix-c-handling-errors我希望基于此特定dll的错误代码获得某种字符串消息,例如,如果我得到的错误代码为12110(ERROR_FTP_TRANSFER_IN_PROGRESS。参考: https://support.microsoft.com/en-au/help/193625/info-wininet-error-codes-12001-through-12156)如果不相同,我希望得到一条类似于以下内容的消息(在变量sMessageBuffer中):“由于正在进行某项操作,因此无法在FTP会话句柄上进行所请求的操作”。但是,sMessageBuffer永远不会分配值,并且什么也不会保留。我只能假设我以某种方式滥用了此技术,我尝试了在线论坛和此站点本身上描述的各种方法,但未成功。
答案 0 :(得分:2)
以下一些有效的示例代码:
Sub Main()
Dim h As IntPtr = LoadLibrary("wininet.dll") ' or GetModuleHandle ...
Dim sb = New StringBuilder(1024)
FormatMessage(Format_Message.FORMAT_MESSAGE_FROM_HMODULE Or Format_Message.FORMAT_MESSAGE_IGNORE_INSERTS,
h,
12002,
0,
sb,
sb.Capacity,
Nothing)
Console.WriteLine(sb) ' prints "The operation timed out"
' FreeLibrary, etc.
End Sub
Enum Format_Message
FORMAT_MESSAGE_IGNORE_INSERTS = &H200
FORMAT_MESSAGE_FROM_SYSTEM = &H1000
FORMAT_MESSAGE_FROM_HMODULE = &H800
End Enum
<DllImport("Kernel32", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Function FormatMessage(ByVal dwFlags As Format_Message, ByVal lpSource As IntPtr, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, lpBuffer As StringBuilder, ByVal nSize As Integer, ByVal Arguments As IntPtr) As Integer
End Function
<DllImport("kernel32", SetLastError:=True, CharSet:=CharSet.Unicode)>
Public Function LoadLibrary(ByVal lpFileName As String) As IntPtr
End Function