Board index » delphi » Pls Help interface from C/C++ to DLL created in Delphi

Pls Help interface from C/C++ to DLL created in Delphi

I have to interface to a DLL created in Delphi and I am running into all
types of problems.

For example ..

1) How is a delphi array organized ?

If I have a function in delphi ..

procedure SayIt3( N : array of integer ); stdcall;
var
  I : integer;
begin
  Writeln('Hello from SayIt3');
  Writeln('Array High Is ', High(N));
  for I := 0 to High(N) do
    begin
      Writeln(I,' => ',N[I]);
    end;
end;

and in "C" I have

typedef void (_stdcall *FP_SAYIT3) (int *);
FP_SAYIT3    sayit3 = ( FP_SAYIT3 ) GetProcAddress( hinst, "SayIt3" );
int ia[10] = { 3,1,2,3,4,5,6,7,8,9 };
(*sayit3)(ia);

The output is

Hello from SayIt3
Array High Is 3
0 => 3
1 => 1
2 => 2
3 => 3

I assume ia[0] is used as the array length indicator .. .. but in that case
how come ia[0] can be accessed from delphi as a regular element ?.

2) I know that delphi shortstring values are byte arrays where the first
byte indicates the length, and for the most part passing the value to/from
delphi is fine unless the delphi parameter is marked as "var" in which case
I run into a problem because the delphi routine is poping 4 bytes too many
off the stack when it returns.

ie  This routine works fine as far as cleaning up the stack ...

procedure SayIt(  N : shortstring ); stdcall;
begin
writeln('Hello from SayIt');
writeln(N);
N := 'Goodbye';
end;

but this routine causes 4 bytes too many to be popped off on the "ret"
instruction (its doing a "ret 8" instead of a "ret 4") .. it's almost as if
there is some hidden parameter due to the "var" .. but if I use "integer"
instead of "shortstring" then both versions ( with & without "var" ) do a
"ret 4".

procedure SayIt(  var N : shortstring ); stdcall;
begin
writeln('Hello from SayIt');
writeln(N);
N := 'Goodbye';
end;

for both versions I using the following in "C"

typedef void (_stdcall *FP_SAYIT)(void *);
FP_SAYIT     sayit  = (FP_SAYIT) GetProcAddress( hinst, "SayIt" );

char    p[256];

strcpy( &(p[1]),"Hello David..." );
p[0] = (char) strlen(&p[1]);
(*sayit)(p);
printf("Result = %d,%s\n", (int)(p[0]), &(p[1]));

Actually, I should also mention that for the routine from question #1, in
regard to the "array of integer", it too has the problem of popping an extra
4 bytes off the stack .. and in that case it happens irregardless of whether
the parameter is "var" or not.

Any Help is most appreciated ...

Thanks .. Dave

 

Re:Pls Help interface from C/C++ to DLL created in Delphi


well this won't answer your questions but it might be of use in your
case :

I would advise you not to use arrays as parameters in your dll, use
pointers instead (integer^, or PChar )

For the stdcall, you better use cdecl, which is a C call covention. Of
course you can compile your C/C++ app as stdcall too, but is seems a
little bit tricky to me (you will have to define all functions as
stdcall ...).

well, hoping that this might help ...

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---

Other Threads