vb.net - PInvoke FormatMessage Message ByRef lpBuffer As [String] 什么都不是
问题描述
声明这个函数:
<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
方案
我目前正在 PInvoking 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 从来没有被赋值并且什么都没有。我只能假设我以某种方式滥用了这种技术,我尝试了在线论坛和这个网站本身描述的各种方法,但我没有成功。
解决方案
这里有一些有效的示例代码:
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
推荐阅读
- javascript - 我如何通过键更改js中的数组
- r - 我正在尝试在 r studio 中绘制回归但无法弄清楚
- python - 使用 Python/Django 打印热收据?
- node.js - 为什么我必须在发送给客户端之前将文件转换为 base64?
- wordpress - 自定义我的 wordpress 网站,以在有人将鼠标悬停在产品上时显示添加到购物车按钮。我已附上碎石
- java - 在不使用循环结构的情况下使用布尔值对重复的单词表示真或假
- c# - 错误:C:\Program Files\dotnet\dotnet.exe 失败,返回码:1
- python - 当我们通过自定义函数对 groupby 的结果使用 apply 时,所有列都不会传递
- python - 在 Python 中添加 3 个变量
- java - 如何断言为私有字段分配了适当的值?