【发布时间】:2014-04-03 12:49:17
【问题描述】:
我正在尝试在 VB.NET 中调用 C++“abc.dll”函数
C++ 函数:
/* input p1 = 16-byte any hex value
/* input p2 = length of the P1 can be 1 to 16 byte
/* input p3 = any string data
/* input p4 = length(given String in p3)
/* output p5 = big enough buffer to be provided at least p4+16 byte.
/* input/output P6 = input P6 length must be p4+18, which is the buffer size for p5
__declspec(dllexport) unsigned char MyFunction( unsigned char *p1, unsigned long p2,
unsigned char *p3, unsigned long p4,
unsigned char *p5, unsigned long &P6);
如果成功则返回 0,如果失败则返回 1。
VB.NET 代码:
<Runtime.InteropServices.DllImport("abc.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?MyFunction@@ttttttt@Z")> _
Public Shared Function VBFunction(ByVal p1() As Char, ByVal p2 As Double, ByVal p3() As Char, ByVal p4 As Double, ByRef p5() As Char, ByRef p6 As Double) As Integer
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ErrorCode As Integer = 0
Dim p1() As Char = {"33", "33", "33"}
Dim p2 As Double = p1.Length
Dim p3() As Char = {"N", "a", "m", "e"}
Dim p4 As Double = p3.Length
Dim p5(50) As Char
Dim p6 As Double = p5.Length
ErrorCode = VBFunction(p1, p2, p3, p4, p5, p6)
End Sub
这总是返回 1 = 失败
传递参数有什么问题吗?
【问题讨论】:
-
input p1 = 16-byte any hex value这是否符合您提供的参数? -
抱歉,十六进制中的“33”所以字符串中的“3”
-
VB.NET 中的
Char不是 2 字节字符吗?所以“3”实际上是 2 个字节?{"33", "33", "33"}比{"3", "3", "3"}好多少。如果第一个参数需要“任何 16 字节”缓冲区,为什么不实际提供 16 字节缓冲区?为什么要使用字符串解决此问题? -
可能存在类型转换问题,您传递的是哪种类型的值? @TithiPatel
-
传递与示例中给出的相同
标签: c++ vb.net parameter-passing dllimport