|
-
Sep 27th, 2002, 08:46 AM
#1
Thread Starter
New Member
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
-
Sep 27th, 2002, 02:09 PM
#2
Hyperactive Member
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.
-
Sep 28th, 2002, 09:09 PM
#3
Fanatic Member
VB Code:
Public Declare Sub CopyMemory _
Lib "kernel32" _
Alias "RtlMoveMemory" _
(ByVal pDest As Long, _
ByVal pSource As Long, _
ByVal Length As Long)
Dim udtSize As Long
Function PassToVC(ByRef udtVal() As TheType)
Dim Buffer() As Byte
Dim iSize As Long
Dim i As Integer
Dim j As Integer
udtSize = LenB(udtVal(LBound(udtVal)))
iSize = UBound(udtVal) - LBound(udtVal)
iSize = udtSize * iSize
Redim Buffer(0 To iSize) As Byte
While i < iSize
PutInMem ByVal VarPtr (Buffer(i)), udtVal(j)
i = i + udtSize
j = j + 1
Wend
' Now call the VC function
SomeVCFunction (Buffer(0))
End Function
Sub PutInMem (ByVal Dest As Long, Src As TheType)
CopyMemory Dest, ByVal VarPtr(Src), udtSize
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|