Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

passing arrays between VB and Delphi

156 views
Skip to first unread message

Jean-Francois Alarie

unread,
Dec 17, 1997, 3:00:00 AM12/17/97
to

Hello, I am trying to receive arrays as parameters of external functions
called from a Visual Basic program to the Delphi DLL I am writing. I
know that passing arrays is possible when using the Variant type on both
sides but because I don't have control over the VB program, this option
is not practical. Things are further complicated by the fact that the
array elements are of record types. On the VB side, it looks like this:

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

Peter Below

unread,
Dec 18, 1997, 3:00:00 AM12/18/97
to

Jean-Francois,

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)


Jean-Francois Alarie

unread,
Dec 19, 1997, 3:00:00 AM12/19/97
to 10011...@compuserve.com

Peter Below wrote:
>
> Jean-Francois,
>
> 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...
>

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

Peter Below

unread,
Dec 20, 1997, 3:00:00 AM12/20/97
to

Should be in OLE2 or OLEAUTO units in D2.

Peter Below (TeamB) 10011...@compuserve.com)


0 new messages