Board index » delphi » String passing between a Delphi DLL and VB app

String passing between a Delphi DLL and VB app

I'm pulling my hair out on this one, and I haven't got much left...so I
thought I would try to tap into all the Delphi/VB pros out there.

I have written a DLL in Delphi that is collecting data from a real-time
feed and buffering it.  A VB app calls a function in the DLL to get data
out of the buffer.  This data is a fixed length string.

Following all the guidelines I can find, it should work.  However, what
I get is GP faults, obviously because I am pointing off to the wrong
place in memory.  I wrote a smaller example that does the same thing.
The code is below:

Thanks in advance for the help.  Below is my latest incantation to make
this work.

This code is written in VB 3.0 and Delphi 1.0

-----------------------------------------------------

VB CODE SECTION

'''''''''''''''''''''''''''''
' FROM DLLMOD.BAS
'''''''''''''''''''''''''''''
' DECLARATION OF DLL PROCEDURE
'''''''''''''''''''''''''''''
Declare Sub GetBuffer Lib "C:/.../dllsmp.dll" (ByVal XBuf As String)

'''''''''''''''''''''''''''''
' FROM TEST.FRM
'''''''''''''''''''''''''''''
Sub ButtonTest_Click ()
    Dim XBuf As String*13
    Call GetBuffer(XBuf)
    Label1.Caption = XBuf
End Sub

<<  EXECUTION OF THIS FUNCTION RETURNS A GPF  >>

{'''''''''''''''''''''''''''''
 ' DELPHI CODE SECTION
 ' for dllsmp.dll
 '''''''''''''''''''''''''''''}
interface

type
    TBuf = array[0..11] of Char;

var
  ABuf: TBuf;

procedure GetBuffer ( var xBuff : TBuf ); export;

implementation

procedure GetBuffer ( P: PChar );
var
  i: integer;
begin
  { creation of a short string with 'xxxxxxxxxxx' in it }
  for i:=0 to 10 do
    ABuf[i] := 'x';

  move( ABuf, P, sizeof( TBuf ) );
end;

 

Re:String passing between a Delphi DLL and VB app


Might be similar to a question I just answered (article subject "D1 DLL and
VB3").
Why does your interface function declaration differs from the implementation?
As a rule of thumb, use PChar arguments for strings when writing DLL functions
in Delphi and (almost) *never* use "var PChar". The latter will result in a
pointer to a pointer to char which is not what VB or most other
environments will pass to your function.

In article <3265A3CB.4...@mnsinc.com>, hha...@mnsinc.com says...

Quote

>I'm pulling my hair out on this one, and I haven't got much left...so I
>thought I would try to tap into all the Delphi/VB pros out there.

>I have written a DLL in Delphi that is collecting data from a real-time
>feed and buffering it.  A VB app calls a function in the DLL to get data
>out of the buffer.  This data is a fixed length string.

>Following all the guidelines I can find, it should work.  However, what
>I get is GP faults, obviously because I am pointing off to the wrong
>place in memory.  I wrote a smaller example that does the same thing.
>The code is below:

>Thanks in advance for the help.  Below is my latest incantation to make
>this work.

>This code is written in VB 3.0 and Delphi 1.0

>-----------------------------------------------------

>VB CODE SECTION

>'''''''''''''''''''''''''''''
>' FROM DLLMOD.BAS
>'''''''''''''''''''''''''''''
>' DECLARATION OF DLL PROCEDURE
>'''''''''''''''''''''''''''''
>Declare Sub GetBuffer Lib "C:/.../dllsmp.dll" (ByVal XBuf As String)

>'''''''''''''''''''''''''''''
>' FROM TEST.FRM
>'''''''''''''''''''''''''''''
>Sub ButtonTest_Click ()
>    Dim XBuf As String*13
>    Call GetBuffer(XBuf)
>    Label1.Caption = XBuf
>End Sub

><<  EXECUTION OF THIS FUNCTION RETURNS A GPF  >>

>{'''''''''''''''''''''''''''''
> ' DELPHI CODE SECTION
> ' for dllsmp.dll
> '''''''''''''''''''''''''''''}
>interface

>type
>    TBuf = array[0..11] of Char;

>var
>  ABuf: TBuf;

>procedure GetBuffer ( var xBuff : TBuf ); export;

>implementation

>procedure GetBuffer ( P: PChar );
>var
>  i: integer;
>begin
>  { creation of a short string with 'xxxxxxxxxxx' in it }
>  for i:=0 to 10 do
>    ABuf[i] := 'x';

>  move( ABuf, P, sizeof( TBuf ) );
>end;

Re:String passing between a Delphi DLL and VB app


Quote
Hal Hayes wrote:

> Following all the guidelines I can find, it should work.  However, what
> I get is GP faults, obviously because I am pointing off to the wrong
> place in memory.  I wrote a smaller example that does the same thing.

I can't see where you 0-terminated the return string?

M.

--
Ettertraktet kaffe, er det ekstra god kaffe?

mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin

Other Threads