我是新来的,如果我使用的术语不正确,请原谅我。我有一个 c 编译的 dll,我从 vb 2013 调用它。我遇到的问题是调用 c dll 中的指针。我尽了最大努力并设法从 int * 中读取,但是 u_int * 或 float * 呢?以下是我到目前为止编写的代码。
我的.dll :
BCAPDLL_API int * __stdcall myfunc1(int * localPtr){
localPtr[0] = 100;
localPtr[1] = 200;
localPtr[2] = 300;
localPtr[3] = 400;
return localPtr;
}
BCAPDLL_API u_int * __stdcall myfunc2(u_int * localPtr){
localPtr[0] = 100;
localPtr[1] = 200;
localPtr[2] = 300;
localPtr[3] = 400;
return localPtr;
}
BCAPDLL_API float * __stdcall myfunc3(float * localPtr){
localPtr[0] = 100;
localPtr[1] = 200;
localPtr[2] = 300;
localPtr[3] = 400;
return localPtr;
}
在 vb.net 中:
Sub Main()
Dim mycomm(4) As Integer
Dim localPtr As IntPtr = Marshal.AllocHGlobal(4)
Dim ptr As IntPtr = testFunc(localPtr)
Marshal.Copy(ptr, mycomm, 0, 4)
For i As Integer = 0 To 3
MsgBox(Marshal.ReadInt32(mycomm, i * Marshal.SizeOf(GetType(Int32))))
Next i
Marshal.FreeHGlobal(localPtr)
End Sub
此函数使用 int * 从 dll 获取函数的所有值,但是对于 u_int * 和 float *,我不能使用相同的方法,因为 Marshal 类不支持 UIntPtr;而 FloatPtr 在 vb.net 中不存在。我该怎么做?提前致谢。
最佳答案
Dim oSize As Integer = 4
Dim mycomm(oSize) As Byte
Dim localPtr As IntPtr
Dim ptr As IntPtr = testFunc3(localPtr)
For i = 0 To oSize - 1
Marshal.Copy(ptr, mycomm, 0, oSize)
MsgBox(BitConverter.ToSingle(mycomm, 0))
ptr = IntPtr.Add(ptr, Marshal.SizeOf(GetType(Single)))
Next i
关于在 vb.net 中调用 C 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001360/