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

How to pass a dynamic array by DLL

4 views
Skip to first unread message

walking

unread,
Oct 2, 2002, 1:43:04 PM10/2/02
to
Hi everyone,

I have a problem in pass a dynamic array in Delphi to a Fortran DLL.
And there is a access violation problem
I think there is something wrong about the pointer, but I am not familiar
with it.


-----Here is my delphi code------
implementation
function dnyarray(var dnya:array of integer;var max:integer):integer;
stdcall;
external '4.dll';
procedure TForm1.BitBtn1Click(Sender: TObject);
var i:integer;
var dnya:array of integer;
begin
i:=15;
setlength(dnya,i);
for i:=0 to 14
do dnya[i]:=1;
k:=dnyarray(dnya,i);
end;
-----Following is the Fortran DLL-------
function dnyarray(dnya,vbmax)
!DEC$ ATTRIBUTES DLLEXPORT, alias:'dnyarray' ::dnyarray
integer,pointer :: dnya(:)
vvvb(vbmax-1)=123
dnyarray=vvvb(vbmax-1)
end function

Thanks !!
Lee


walking

unread,
Oct 2, 2002, 1:43:04 PM10/2/02
to

Jugoslav Dujic

unread,
Oct 3, 2002, 3:40:17 AM10/3/02
to
"walking" <wal...@ew.ee.ntu.edu.tw> wrote in message
news:anfbdh$m84$1...@gemini.ntu.edu.tw...

| Hi everyone,
|
| I have a problem in pass a dynamic array in Delphi to a Fortran DLL.
| And there is a access violation problem
| I think there is something wrong about the pointer, but I am not familiar
| with it.
|
| -----Here is my delphi code------
| function dnyarray(var dnya:array of integer;var max:integer):integer;
| stdcall;
| external '4.dll';
| -----Following is the Fortran DLL-------
| function dnyarray(dnya,vbmax)
| !DEC$ ATTRIBUTES DLLEXPORT, alias:'dnyarray' ::dnyarray
| integer,pointer :: dnya(:)

DON'T use assumed-shape or pointer arrays when doing mixed-language.
Fortran pointers are not just addresses -- they are actually structured
array descriptors, which are tough to mimic in other
languages. (IIRC CVF descriptor for an one-dimensional array
is a structure of 28 bytes, passed by reference -- but that's
not portable even accross Fortran compilers)

If you use assumed-size array instead you should be fine:

integer:: dnya(*)

If the length of the array is necessary, you can pass it as an
additional argument.

--
Jugoslav
___________
www.geocities.com/jdujic


John Mansell

unread,
Oct 3, 2002, 6:17:28 AM10/3/02
to
In order to make the Length function work in the Delphi procedure, the
compiler pushes the length (length-1?) of the dynamic array onto the
stack as a hidden argument. This has implications for mixed-language
programming. If there's a way to tell cvf to expect a parameter passed
by value, then I might be persuaded to inspect some code at assembler
level in order to determine whether its length or length-1; and whether
the hidden argument is before or after the array.

The best advice has already been given - don't use dynamic arrays in
mixed language programming. Keep it simple and use assumed size arrays
on the fortran side.
Avoid assumed shape arrays on the fortran side: these require extra
parameters on the Delphi side in order to say things about maximum
subscript value and stride.

In message <angs4r$e29pg$1...@ID-106075.news.dfncis.de>, Jugoslav Dujic
<jdujic...@uns.ns.ac.yu> writes

--

John Mansell jo...@wcompsys.co.uk

0 new messages