Board index » delphi » Need Help calling Delphi DLL from Visual Basic

Need Help calling Delphi DLL from Visual Basic

Greetings,

I have written a Delphi 3.0 DLL that I would like to be able to call
from Visual Basic. The DLL works when used with Delphi but I don't know
exactly how to write the Declare statement in Visual Basic or how to
pass the parameters.

The exported Delphi Function is:

function CallEngine(dataBase : PChar; searchString : PChar;
     var hitList : PChar; var nameList : PChar) : integer; stdcall;

The variables dataBase and searchString are passed into the DLL for
processing. I assume they are declared "ByVal database as String".

However, the variables hitList and nameList are strings in Delphi that
are dynamically allocated within the DLL and I need to pass those
strings BACK to Visual Basic. How do I declare them and how do I DIM the
variables to pass to them.

TIA
--
----------------------------------------------------
Michael A. Hess - Programming my first best destiny!

 \|/  Miracle          \
--*--------------------->
 /|\  Concepts, Inc.   /

     Founding Member of the
MultiMedia Developers Consortium

RR #1 Box 74
Pittston, PA  18643
(717) 388-2211         E-mail: mh...@miraclec.com
                               mirac...@mmdc.org
http://www.miraclec.com
http://www.mmdc.org
----------------------------------------------------

 

Re:Need Help calling Delphi DLL from Visual Basic


I had to deal with the same problem of returning strings to VB recently.  I
don't know if this is the best way, but it worked for me.  Seems to me that
any way you slice it you have to get VB involved since it has it's own
memory manager for strings (as does Delphi), so I defined a VB module that
implemented a string-assignment "server" Sub :

    sub VBAssignString( byVal src as String, dst as String )
    dst = src
    end  sub

Note that "src" is "byVal" and "dst" is "Byref" The VB app must provide the
server to your DLL, say at startup:

    InstallServer addressOf VBAssignString

Your Delphi DLL will use this procedure pointer to convert form PChar.

Finally, provide a VB-friendly interface, say, VbCallEngine.  The VB module
can alias the name if you want VB apps to see "CallEngine".

Note:

1) VB will be making a copy of your returned strings, so you'll need to free
them in VbCallEngine.
2) VB passes a pointer to a pointer to a BSTR type for ByRef strings. You'll
need to declare them appropriately when receiving them, and when passing
"dst" to the server.  A BSTR is a 32-bit length-preceded, null-terminated
string, and the pointer is to the string, not the length (similar to Delphi
strings).
3) You must define the length of "src" - VB seems to check this even though
the string is null-terminated.  This means you have to copy the returned
string to a buffer area, like

    type
        TBstr = record
            len: Integer;
            str: array[0..MAXLEN] of Char;
        end;

and pass @Bstr.str as "src".

BTW, my DLL wasn't in Delphi, so I had to take the same approach for Delphi
clients (i.e., created a delphi unit containing a delphi string-assignment
server).  The Delphi version was a little more elegant because the server
could be installed automatically by the unit without the app knowing the
details.

If there's an easier way, I'd like to hear about it.

HTH,
Jim Green

Quote
Michael A. Hess wrote:
> Greetings,

> I have written a Delphi 3.0 DLL that I would like to be able to call
> from Visual Basic. The DLL works when used with Delphi but I don't know
> exactly how to write the Declare statement in Visual Basic or how to
> pass the parameters.

> The exported Delphi Function is:

> function CallEngine(dataBase : PChar; searchString : PChar;
>      var hitList : PChar; var nameList : PChar) : integer; stdcall;

> The variables dataBase and searchString are passed into the DLL for
> processing. I assume they are declared "ByVal database as String".

> However, the variables hitList and nameList are strings in Delphi that
> are dynamically allocated within the DLL and I need to pass those
> strings BACK to Visual Basic. How do I declare them and how do I DIM the
> variables to pass to them.

> TIA
> --
> ----------------------------------------------------
> Michael A. Hess - Programming my first best destiny!

>  \|/  Miracle          \
> --*--------------------->
>  /|\  Concepts, Inc.   /

>      Founding Member of the
> MultiMedia Developers Consortium

> RR #1 Box 74
> Pittston, PA  18643
> (717) 388-2211         E-mail: mh...@miraclec.com
>                                mirac...@mmdc.org
> http://www.miraclec.com
> http://www.mmdc.org
> ----------------------------------------------------

Re:Need Help calling Delphi DLL from Visual Basic


Quote
Jim Green wrote:

> I had to deal with the same problem of returning strings to VB recently.  I
> don't know if this is the best way, but it worked for me.  Seems to me that
> any way you slice it you have to get VB involved ...

That was my problem. For what I was doing I couldn't get VB involved.

<snip>

Quote
> 3) You must define the length of "src" - VB seems to check this even though
> the string is null-terminated.

This was the crux of my problem. The size or length of the string to be
returned was unknown. The DLL was dynamically allocating the memory for
the string so VB couldn't reserve space to call the DLL because VB
didn't know how much space to reserve until it called the DLL. Catch 22.

I just ended up rewriting the DLL. I was originally trying to pass back
a TStringList.GetText() string. Then I was going to let VB parse each of
the individual lines. I just rewrote it so that a separate routine
called for each TStringList.Item. Slower but it doesn't give me a GPF
now.  I can use ByVal now because I do know the maximum length of each
Item I just had no way of knowing how many items there would be.

Hey thanks for your reply. It help confirm what I had figured out.

--
----------------------------------------------------
Michael A. Hess - Programming my first best destiny!

 \|/  Miracle          \
--*--------------------->
 /|\  Concepts, Inc.   /

     Founding Member of the
MultiMedia Developers Consortium

RR #1 Box 74
Pittston, PA  18643
(717) 388-2211         E-mail: mh...@miraclec.com
                               mirac...@mmdc.org
http://www.miraclec.com
http://www.mmdc.org
----------------------------------------------------

Other Threads