Translate Visual C++ to VB.NET

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M. Bruil

    #1

    Translate Visual C++ to VB.NET

    I tried to do some 'DLLImport' on a DLL. The actual source code was written
    in Visual C++. Unfortunately I can't get the VB.NET syntax right for this
    function call. Can someone provide me with the correct VB.NET syntax?

    ----------------------------------------




    #define S00X_ANTICOLL_A RRAY 64

    int MSTX_EXPORT S00X_ReadC220(B OOL useconfig, BOOL changeconfig, BOOL
    anticollision, double listeningtime, unsigned char *nbchips, unsigned char
    chips[][S00X_ANTICOLL_A RRAY], unsigned char *cmdstatus);



    <DllImport("Med ioSTX.dll")> _
    Private Function S00X_ReadC220(B yVal useconfig As Boolean, ByVal
    changeconfig As Boolean, ByVal anticollision As Boolean, ByVal listeningtime
    As Double, ByRef nbchips As Char, ByRef chips()() As Char, ByRef cmdStatus
    As Char) As Integer
    End Function


  • Dragon

    #2
    Re: Translate Visual C++ to VB.NET

    Hi,

    First of all, char * is a String (LPStr actually).

    But the chips parameter poses a difficulty indeed, since AFAIK [][]
    means an continuous sequence of strings, not even an array of LPStr's
    8=[.
    And I don't see a way to pass this except allocating the memory and
    positioning strings manually. So that's the declarations:

    ~
    Const S00X_ANTICOLL_A RRAY As Integer = 64

    Declare Ansi Function S00X_ReadC220 Lib "MedioSTX.d ll" ( _
    ByVal useconfig As Boolean, _
    ByVal changeconfig As Boolean, _
    ByVal anticollision As Boolean, _
    ByVal listeningtime As Double, _
    <MarshalAs(Unma nagedType.LPStr )> ByVal nbchips As String, _
    ByVal chips As IntPtr, _
    <MarshalAs(Unma nagedType.LPStr )> ByVal cmdstatus As String _
    ) As Integer
    ~

    And the usage:

    ~
    Protected Overrides Sub OnClick(ByVal e As System.EventArg s)
    MyBase.OnClick( e)
    Dim ChipsA() As String = {"JC Denton", "Alex Jacobson", "Jaime
    Reyes", "Joseph Manderley"}
    ' I presume you have a string array called ChipsA

    ' strings' length must not exceed S00X_ANTICOLL_A RRAY,
    ' or you'll get garbage!

    Dim base As IntPtr, cur As Integer
    base = Marshal.AllocHG lobal(S00X_ANTI COLL_ARRAY * ChipsA.Length)
    cur = base.ToInt32

    For Each str As String In ChipsA
    'Copy the string...
    Marshal.Copy(Sy stem.Text.Encod ing.Default.Get Bytes(str), 0,
    New IntPtr(cur), str.Length)
    '... and append the terminating NULL.
    Marshal.WriteBy te(New IntPtr(cur), str.Length, CByte(0))
    cur += S00X_ANTICOLL_A RRAY
    Next

    'Use function with base as chips:
    'S00X_ReadC220( blah blah blah...)

    Marshal.FreeHGl obal(base)
    End Sub
    ~

    Whew... This was a bit harder that I initially expected.. 8=]

    Unfortunately, I don't have a library to test this on, but it should
    work.

    Hope this helps,
    Roman


    Comment

    Working...