繁体   English   中英

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

[英]Passing parameter in C++ dll function from vb.net

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM