Board index » delphi » Creating a with Delphi - Passing strings to VB

Creating a with Delphi - Passing strings to VB

Hi,

I'm trying to enhance my VB application with a DLL I'm writing in
Delphi. The only problem is that I need to return a string as the
result of a declared function of my DLL and it's returning nothing...
I know I could return it as a parameter but really need to do it that
way... I included a copy of my VB declaration and Delphi code for the
Gurus!

Here's a sample of my VB declaration and code:

'******************************************************
Declare Function MyStr Lib "C:\MYDLL.DLL" () As String

'******************************************************

 Dim sMyString As String

    sMyString = Space(255)
    sMyString = MyStr()    '*** Here nothing is returned...

    LabMyLabel.caption = sMyString

'******************************************************

Here's a sample of my Delphi Dll

{******************************************************}
Library Mydll;

Uses
  MinMax in 'MINMAX.PAS';

exports
      MyStr;

begin
end.
{******************************************************}
unit MinMax;

Interface

  Uses
    VbxCtrl;

  Function MyStr               : TBasicString;  export;

implementation

function MyStr: TBasicString;

  begin
    {MyStr := BStrPCopy('Allo');} {Also tried this without any
success}

    Result := BStrPCopy('Allo');
  end;

End.
{******************************************************}

If a use a PChar or string it give me a GPF the TBasicString type
don't give GPF but I must be missing something because it's returning
nothing...

Does anyone know what's wrong in the way I return that string to
Visual Basic? or any hints of how you return a string to VB.

Thanks in advance for you help!

-Pierre

.:.:.:.:.:.:..:.:.:.:.:.:.:.:.:..:.:.:.:.:.:..:.:.:.:.:.:..:.
Pierre Fillion                    pfill...@infobahnos.com
Synetics Consultation             Pierr...@ceo.sts-systems.ca
Montreal, Quebec, Canada          CIS: 71162,51

 

Re:Creating a with Delphi - Passing strings to VB


I dont think its wise to true to return the string via a function return
value. You can send fixed length strings as C strings by declaring them as
ByVal as a parameter.  

Even though they appear to be passing the value not the reference in fact
the opposite is the case. You receive the return value through the string.

ie:

'******************************************************
Declare Sub MyStr Lib "C:\MYDLL.DLL" ( ByVal Str As String)

'******************************************************

 Dim sMyString As String

    sMyString = Space(255)
    MyStr( sMyString)    '*** Here nothing is returned...

    LabMyLabel.caption = sMyString

'******************************************************

Here's a sample of my Delphi Dll

{******************************************************}
Library Mydll;

Uses
  MinMax in 'MINMAX.PAS';

exports
      MyStr;

begin
end.

{******************************************************}
unit MinMax;

Interface

  Uses
    VbxCtrl;

Procedure MyStr( Str :PChar);  export;

implementation

Procedure MyStr( Str :PChar);  export;

  begin
    StrCopy( Str, 'Allo');
  end;

End.

I haven't tested the above code but the key points are there...

1. use a fixed length basic string.
2. pass it as a parameter declared as ByVal.
 (this will translate it into a C string) and pass it by reference.
3. treat it like a PChar at the the Delphi end.

re-read the chapter on using DLLs in the VB manual, particularly about
strings.

Look at how common windows API functions that use strings for results are
declared in VB  WINAPI module... (eg verFindFile)

=================================================================
John Hardy            83 Whiteman St        voice: 61-3-9686-6611
Show-Ads Interactive  South Melbourne 3205  fax:   61-3-9686-6146
Shomega Ltd           AUSTRALIA

Re:Creating a with Delphi - Passing strings to VB


Quote
pfill...@infobahnos.com (Pierre Fillion) wrote:
>Hi,
>I'm trying to enhance my VB application with a DLL I'm writing in
>Delphi. The only problem is that I need to return a string as the
>result of a declared function of my DLL and it's returning nothing...
>I know I could return it as a parameter but really need to do it that
>way... I included a copy of my VB declaration and Delphi code for the
>Gurus!

VB strings are not the same as Pascal or null-terminated strings. They
are of a totally different sort. I dont know how they are organised in
memory and the VB manual does not say either. Instead you can return a
null-terminated string to VB, if you just make sure that the VB-string
has space for the characters you want to return. Declare your external
function in VB as :

Declare Function MyStr Lib "C:\MYDLL.DLL" (ByVal TestStr As String) As
Integer

The "ByVal TestStr..." does NOT transfer the value of TestStr but
instead transfers a pointer to a null-terminated string that contains
the value of TestStr (talk about inconsistency!). Then use this
pointer in your Delphi-DLL to copy the data to the string, like this:

function MyStr(TestStr: PChar): Integer;
  export;
var
  pasTemp: PChar;
begin
  pasTemp:= { whatever your result is }
  StrCopy(vbFileName, pasTemp);
  Result:= 0;
end;

Before you call MyStr(SomeString) make sure that memory is allocated
to hold the returned data from your Delphi-DLL:

   SomeString = String(255, 0)  ' or more if you return more than 255
bytes
   X = MyStr(SomeStr)

That should do it.

-- Jan Holst Jensen, Denmark

Other Threads