Results 1 to 3 of 3

Thread: Passing udt array to a vc++ dll

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Posts
    1

    Passing udt array to a vc++ dll

    Hi I'm trying to pass an UDT array to a vc++ dll. The DLL receive the udt array and fill it with data.

    My UDT look like this :
    Type Teacher
    age as long
    name as string * 30
    school as string * 30
    end Type

    If you have an idea for the VB and/or the VC++ part I will be very glad because I have no clue how to do this

    Thanks

  2. #2
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Okinawa, Japan
    Posts
    271
    Hmm this look familiar.


    I think the best way to do it is to pass an array of bytes into function
    call. Then copy the array into the UDT using memcopy.
    The C++ dll should fill the array.
    Then do a memcopy from the array of bytes into the array of your UDT
    Best I give example.
    If the function call is

    'In module
    Private Declare Function GetTeacherData Lib "WhatEverDll.dll" ( _
    lpSomeByteBuffer As Any) As Long
    'the lpSomeByteBuffer is what get filled.
    'say for example the Long returned is the number of UDT that is sent into the buffer.

    Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef
    pDest As Any, ByRef pSource As Any, ByVal Length As Long)
    'We need this to copy the Buffer of Bytes filled in the call to the UDT
    array
    'end module

    'somewhere, form another module or whatever
    Dim ByteBuffer(SomeSize) As Byte
    Dim NumberOfUDTsSent as Long
    'make sure the size of the buffer is big enough for the dll to fill.

    then call the function passing the ByteBuffer(0)
    NumberOfUDTsSent=GetTeacherData (ByteBuffer(0))

    Upon return the ByteBuffer will have all the data.

    'Now if this is your UDT
    Type Teacher
    age as long
    name as string * 30
    school as string * 30
    end Type

    Dim TeacherData (SomeUDTSize) as Teacher
    Again be sure the array size of the UDT is big enough to hold the data

    CopyMemory TeacherData(0), ByteBuffer(0),
    Len(TeacherData(0)*NumberOfUDTsSent)


    Now the only problem is with the strings. If the dll doesnt fill the string to 30 characters then you may have problems with the strings being correct.

  3. #3
    Fanatic Member MoMad's Avatar
    Join Date
    Oct 2000
    Location
    Seattle, WA
    Posts
    625
    VB Code:
    1. Public Declare Sub CopyMemory _
    2.         Lib "kernel32" _
    3.         Alias "RtlMoveMemory" _
    4.         (ByVal pDest As Long, _
    5.         ByVal pSource As Long, _
    6.         ByVal Length As Long)
    7.  
    8. Dim udtSize As Long
    9.  
    10. Function PassToVC(ByRef udtVal() As TheType)
    11.   Dim Buffer() As Byte
    12.   Dim iSize As Long
    13.   Dim i As Integer
    14.   Dim j As Integer
    15.  
    16.   udtSize = LenB(udtVal(LBound(udtVal)))
    17.  
    18.   iSize = UBound(udtVal) - LBound(udtVal)
    19.   iSize = udtSize * iSize
    20.  
    21.   Redim Buffer(0 To iSize) As Byte
    22.  
    23.   While i < iSize
    24.     PutInMem ByVal VarPtr (Buffer(i)), udtVal(j)
    25.     i = i + udtSize
    26.     j = j + 1
    27.   Wend
    28.  
    29.   ' Now call the VC function
    30.   SomeVCFunction (Buffer(0))
    31. End Function
    32.  
    33. Sub PutInMem (ByVal Dest As Long, Src As TheType)
    34.   CopyMemory Dest, ByVal VarPtr(Src), udtSize
    35. End Function

    If this doesnt work, reply and ill see whats wrong.

    **note** I havent tested this, its all off the top of my head.
    :MoMad:
    Nice Sig!

    http://go.to/momad/ Status: Not Ready

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width