gpt4 book ai didi

c++ - 从 vb.net 在 C++ dll 函数中传递参数

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:23:16 33 4
gpt4 key购买 nike

我正在尝试在 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 = 失败

传参有什么问题吗?

最佳答案

参数不正确。试试这个:

<DllImport("abc.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?MyFunction@@YAEPEAEK0K0AEAK@Z")> _
Shared Function VBFunction(ByVal p1() As System.Byte, ByVal p2 As System.UInt32,
ByVal p3() As System.Byte, ByVal p4 As System.UInt32,
ByVal p5() As System.Byte, ByRef p6 As System.UInt32) As System.Byte
End Function

请注意,在 VB 中,数组条目从 1 开始计数。在 C++ 中 - 从 0 开始。您应该像这样声明 p5:

 Dim p5(0 to 50) As Char

以下是我的完整VB源代码

Imports System.Runtime.InteropServices
Class App

<DllImport("abc.dll", CallingConvention:=CallingConvention.Cdecl, EntryPoint:="?MyFunction@@YAEPEAEK0K0AEAK@Z")> _
Shared Function VBFunction(ByVal p1() As System.Byte, ByVal p2 As System.UInt32,
ByVal p3() As System.Byte, ByVal p4 As System.UInt32,
ByVal p5() As System.Byte, ByRef p6 As System.UInt32) As System.Byte
End Function


Shared Function Main(ByVal args As String()) As Integer

Dim p1() As System.Byte = {1, 2, 3}
Dim p3() As System.Byte = {4, 5, 6}

Dim p5(0 to 50) As System.Byte

Dim p6 As System.UInt32 = 1234

Dim ErrorCode As System.Byte = VBFunction(p1, p1.Length, p3, p3.Length, p5, p6)

System.Console.WriteLine( ErrorCode )

if( 0 = ErrorCode ) then
System.Console.WriteLine( "p6={0}", p6 )
for X as System.UInt32 = 0 to p6-1
System.Console.WriteLine( p5(X) )
Next

end if

Return 1
End Function
End Class

关于c++ - 从 vb.net 在 C++ dll 函数中传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22838288/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com