【问题标题】:Passing parameter in C++ dll function from vb.net从 vb.net 在 C++ dll 函数中传递参数
【发布时间】: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


【解决方案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

【讨论】:

    猜你喜欢
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-29
    • 1970-01-01
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多