Tithi Patel
Tithi Patel

Reputation: 777

Passing parameter in C++ dll function from vb.net

I am trying to call C++ "abc.dll" function in VB.NET

C++ Function:

    /* 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);

this will return 0 if success and 1 if failure.

VB.NET code:

 <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

This is always returning 1 = Failure

Is any thing wrong in passing parameter?

Upvotes: 2

Views: 1960

Answers (1)

Dmytro Ovdiienko
Dmytro Ovdiienko

Reputation: 1106

Argument parameters are not correct. Trys this:

<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

Please note in VB array entries are counted from 1. In C++ - from 0. You should declare p5 like this:

 Dim p5(0 to 50) As Char

Following is my full source VB code

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

Upvotes: 1

Related Questions