Board index » delphi » Delphi DLL strings and Visual Basic

Delphi DLL strings and Visual Basic

Hi,

I've been trying in vain to pass Delphi (pascal type)strings to
VB via a DLL. I've read in a message that this either possible
via BORLNDMM.DLL or by using Wide Strings. The problem is that
I can't seem to find any examples of either these methods.

I am not sure how to tell a VB project to use BORLNDMM.DLL and
when I try to define my functions for example in Delphi like :

function Compress(wstrDecompressed : WideString)   : WideString;
stdcall;

and in VB as

Private Declare Function Compress Lib "MYDLL" Alias "#4" (ByVal
strDecompressed As String) As String

VB just keels over...

TIA
Andre

--
"Lost in the wheels of confusion
 Running through furnace of tears
 Eyes full of angered illusion
 Hiding in every day things" - Wheels of Confusion

 

Re:Delphi DLL strings and Visual Basic


Quote
> I've been trying in vain to pass Delphi (pascal type)strings to
> VB via a DLL. I've read in a message that this either possible
> via BORLNDMM.DLL or by using Wide Strings. The problem is that
> I can't seem to find any examples of either these methods.

The Problem of Delphi is, that the delphistrings are unknown by Windows.
You don't have to deklare ana functions out of the BORLNDMM.DLL if you use
PChars to sent strings between a proggy and a dll.
I could not say, how you convert strings to PCahrs in VisualBasic, but in
Delphi it is a easy thing:

String to PChar:

stringvariable: string;
charactervariable: PChar;

...

charactervariable:= PChar(stringvariable);

PChar to String:

stringvariable: string;
charactervariable: PChar;

...

stringvariable:= StrPas(charactervariable);

if you use this to sent strings to dll's, you won't have the problem with
the BORLNDMM.DLL.

Clemenz

Re:Delphi DLL strings and Visual Basic


Unfortunately I can't use PChar due to the fact that we are use binary
and ASCII conversions that will cause an null terminator to pop up
too early and invalidate the whole string - so I need to ansi type
strings...

On Wed, 29 Nov 2000 21:46:16 +0100, "Clemenz Nafe"

Quote
<KaasbomberS...@gmx.de> wrote:

>> I've been trying in vain to pass Delphi (pascal type)strings to
>> VB via a DLL. I've read in a message that this either possible
>> via BORLNDMM.DLL or by using Wide Strings. The problem is that
>> I can't seem to find any examples of either these methods.

>The Problem of Delphi is, that the delphistrings are unknown by Windows.
>You don't have to deklare ana functions out of the BORLNDMM.DLL if you use
>PChars to sent strings between a proggy and a dll.
>I could not say, how you convert strings to PCahrs in VisualBasic, but in
>Delphi it is a easy thing:

>String to PChar:

>stringvariable: string;
>charactervariable: PChar;

>...

>charactervariable:= PChar(stringvariable);

>PChar to String:

>stringvariable: string;
>charactervariable: PChar;

>...

>stringvariable:= StrPas(charactervariable);

>if you use this to sent strings to dll's, you won't have the problem with
>the BORLNDMM.DLL.

>Clemenz

Re:Delphi DLL strings and Visual Basic


Quote
On Thu, 30 Nov 2000 04:21:09 GMT, mwd...@mweb.co.za wrote:
> so I need to ansi type
>strings...

I've had a similar problem. Using a procedure like

Type
          AChArray = array[0..255] of Char;

Procedure TestProc (TestArr : AChArray); StdCall;
begin
      { Process }
end;

Exports
           TestProc;

I declare it in VB as
     Declare Sub TestProc  Lib "Project1.dll" (ByVal strTest  As
String):

I can pass the string from Visual Basic and it processes correctly in
the Delphi Dll. BUT the first time I load the test program up, run the
procedure from VB and check Err.LastDLLError, the Err function reports
the error as 127 although subsequent runs show the error as 0 (OK).
Sooner or later, though, if I keep running the procedure, the dll will
crash with an illegal access error.

If I modify the Pascal procedure to
       Procedure TestProc (Var TestArr : AChArray); StdCall;

and the VB Declaration to
      Declare Sub TestProc  Lib "Project1.dll" (ByRef strTest  As
String)

then VB will crash, either immediately I run the test program or after
I close it .

The problems occur EVEN IF THERE IS NO PROCESSING AT ALL IN THE DELPHI
PROCEDURE.

The problem is clearly a memory overwrite one, but does anyone have
any idea of how to cure it?

Mike

Re:Delphi DLL strings and Visual Basic


If you're not using character data, don't use a string type.  Why not
declare a structure:

MyData = record
    len: Integer;
    data: array[0..MAX_LEN-1] of byte;
end;

If you have null-values within your 'string', it really shouldn't be a
string type at all.

Quote
<mwd...@mweb.co.za> wrote in message news:3a25d51f.398028@news.mweb.co.za...
> Unfortunately I can't use PChar due to the fact that we are use binary
> and ASCII conversions that will cause an null terminator to pop up
> too early and invalidate the whole string - so I need to ansi type
> strings...

> On Wed, 29 Nov 2000 21:46:16 +0100, "Clemenz Nafe"
> <KaasbomberS...@gmx.de> wrote:

> >> I've been trying in vain to pass Delphi (pascal type)strings to
> >> VB via a DLL. I've read in a message that this either possible
> >> via BORLNDMM.DLL or by using Wide Strings. The problem is that
> >> I can't seem to find any examples of either these methods.

> >The Problem of Delphi is, that the delphistrings are unknown by Windows.
> >You don't have to deklare ana functions out of the BORLNDMM.DLL if you
use
> >PChars to sent strings between a proggy and a dll.
> >I could not say, how you convert strings to PCahrs in VisualBasic, but in
> >Delphi it is a easy thing:

> >String to PChar:

> >stringvariable: string;
> >charactervariable: PChar;

> >...

> >charactervariable:= PChar(stringvariable);

> >PChar to String:

> >stringvariable: string;
> >charactervariable: PChar;

> >...

> >stringvariable:= StrPas(charactervariable);

> >if you use this to sent strings to dll's, you won't have the problem with
> >the BORLNDMM.DLL.

> >Clemenz

Other Threads