Declare Function vbTest Lib "adelphi.dll" (A() As atype) As Integer
Type atype
date As Long
price As Double
name As String
End Type
In the DLL, I have tried:
function vbTest(const A: array of atype): integer; stdcall; export;
where atype is defined exactly as the in the VB program. I have also
tried using fixed-size arrays, pointers to arrays, all with no success.
Even if I try with a simpler type of array element, such as integer, it
still doesn't work. I guess the idea is to construct a type that matches
the information sent by VB and use it to define the receiving parameter
in the DLL. Would anybody know how to do this?
Thanks. JF
as far as i know VB (at least VB 5) uses OLE safearrays to pass arrays to
external functions. The relevant stuff is declared in Delphi's ActiveX
unit. Try this declaration:
Function vbTest(Var aSafeArray: PSafeArray ):Integer; stdcall;
I'm not completely positive about the Var here but since VB passes anything
by reference as default i would expect it to be correct. If you run into
problems, try it without the Var.
To get at the array data you can either use SafeArrayGetElement or do a
SafeArrayLock and retrieve a pointer to the array data from
aSafeArray^.pvData. This pointer can be cast to a pointer to a matching
Pascal array type but this assumes that VB and Delphi use the same way to
arrange array data, which is generally not a safe assumption to make.
The record type is a problem in itself, because of the "name as String"
field. VB strings are different from Delphi strings. VB 5 uses the BSTR
OLE type, which translates to either a Delphi PChar or a PWideChar,
depending on whether the VB app uses UNICODE or not.
atype = record
date: LongInt;
price: Double;
name: Pchar; // or PWideChar
End;
A suitable array type to use with the SafeArrayLock approach mentioned
above would be
TVBRecArray = Array [0..High(Cardinal) div Sizeof(atype) - 1 ] of atype;
PVBRecArray = ^TVBrecArray;
Peter Below (TeamB) 10011...@compuserve.com)
Thanks very much Peter for the detailed information. One further
question however: Is the ActiveX unit available to Delphi 2.0 or does
it only come with Delphi 3? My compiler does not recognise the
'PSafeArray' type. Many thanks again. JF