【发布时间】:2013-07-14 00:00:54
【问题描述】:
我已经编写了自己的函数,在 C 中会这样声明,使用标准的Win32 调用约定:
int Thing( char * command, char * buffer, int * BufSize);
我已经弄清楚了以下数量的 Visual Basic 代码,它们应该导入 DLL 文件并调用此函数,将其包装起来以便于调用 Thing("CommandHere",GetDataBackHere)。
更新:此代码现在是一个有效的解决方案,如下所示:
Imports Microsoft.VisualBasic
Imports System.Runtime.InteropServices
Imports System
Imports System.Text
Namespace dllInvocationSpace
Public Class dllInvoker
' I tried attributes, but I could not make it build:
' <DllImport("thing1.dll", False, CallingConvention.Cdecl, CharSet.Ansi, "Thing", True, True, False, True)>
Declare Ansi Function Thing Lib "thing1.dll" (ByVal Command As String, ByRef Buffer As StringBuilder, ByRef BufferLength As Integer) As Integer
' This part contributed by helpful user:
Shared Function dllCall(ByVal Command As String, ByRef Results As String) As Integer
Dim Buffer As StringBuilder = New StringBuilder(65536)
Dim Length As Integer = Buffer.Capacity
Dim retCode As Integer = Thing(Command, Buffer, Length)
Results = Buffer.ToString()
'Debug.Assert(Results.Length = Length) ' This assertion is not true for me
Return retCode
End Function
End Class
End Namespace
我按照这里收到的帮助获得了要构建的代码,然后我忘记了As Return Type(它给了我一个 MarshalDirectiveException PInvokeRestriction)。然后我的 DLL 中出现了断言失败,这导致了 SEHException。一旦修复,这将非常有效。谢谢各位。有些新闻组里有人说这不能做,Visual Basic 只加载托管的 DLL 程序集(我猜这是大多数 Visual Basic 用户习惯的正常做法)。
【问题讨论】:
标签: vb.net dll unmanaged dllimport