Board index » delphi » How to pass strings to Visual Basic

How to pass strings to Visual Basic

I am trying to write a Delphi DLL which should be able to pass strings to a
calling application. I am having trouble to realise this with Visual Basic
for applications.

My Delphi code is something like

Library K;
Uses Functions;
Exports
 K_Description;
begin
end;

Unit Functions;
interface
function K_Description(var  Key:PChar) ; Pchar; stdcall;
implementation
function K_Description(var  Key:PChar) ; Pchar
begin
  K_Description := 'test string';
end;

Delphi creates the DLL properly. In a module for VBA, I have created the
following;

Public Declare Function K_Description Lib "K" (ByRef Key as String) as
String

Sub Test
Dim Desc as String
Dim Fluid as String
Fluid = "Dummy"
Desc = K_Description (Fluid)
end Sub

When I run test, it seems to call the DLL properly, but the string Desc does
not contain the proper value. Any suggestions how to work this out. I have
found info on passing doubles, integer etc as function results but could not
found examples with strings.

Thanks in advance,
Martien

 

Re:How to pass strings to Visual Basic


Hi!

I am just trying to write something similar. Here is what I found out:

1.) Strings should be passed ByVal to the dll from VB
2.) PChar should by without var in Delphi.
3.) You can not use function result to return the string. More even. You
must allocate a string in VB to certain length and then pass it ByVal to the
dll procedure. Delphi should make changes to that string, but the problem
can be the length. (String from VB has to be long enough or you get AV).

Delphi in VB strings (and PChar) have a different header and you can not
allocate string in Delphi and pass a pointer to it to VB. One option I have
not tried yet would be:

1.) allocate the string in Delphi
2.) typecast to PChar
3.) Return the PChar in the function result.
4.) Use a VB function (?? which one?) to type cast LPSTR (same as PChar)
type string (zero terminated) to VB string type.

Regards!
Janez.

Quote
> My Delphi code is something like

> Library K;
> Uses Functions;
> Exports
>  K_Description;
> begin
> end;

> Unit Functions;
> interface
> function K_Description(var  Key:PChar) ; Pchar; stdcall;
> implementation
> function K_Description(var  Key:PChar) ; Pchar
> begin
>   K_Description := 'test string';
> end;

> Delphi creates the DLL properly. In a module for VBA, I have created the
> following;

> Public Declare Function K_Description Lib "K" (ByRef Key as String) as
> String

> Sub Test
> Dim Desc as String
> Dim Fluid as String
> Fluid = "Dummy"
> Desc = K_Description (Fluid)
> end Sub

> When I run test, it seems to call the DLL properly, but the string Desc
does
> not contain the proper value. Any suggestions how to work this out. I have
> found info on passing doubles, integer etc as function results but could
not
> found examples with strings.

> Thanks in advance,
> Martien

Re:How to pass strings to Visual Basic


Thanks Janez for the reply. Now I have read something more about passing a
string as a function result to a VB program. However, most info comes down
to your third point and suggest not to use this approach as it seems
complicated or even impossible. Therefore I have given up on this one and I
am now transferring the strings via the parameter list using the method you
mentioned.

Regards,

Martien

Quote
"Janez Makovsek" <janez.makov...@usa.net> wrote in message

news:9nfn5e$kke$1@strelovod.uni-mb.si...
Quote
> Hi!

> I am just trying to write something similar. Here is what I found out:

> 1.) Strings should be passed ByVal to the dll from VB
> 2.) PChar should by without var in Delphi.
> 3.) You can not use function result to return the string. More even. You
> must allocate a string in VB to certain length and then pass it ByVal to
the
> dll procedure. Delphi should make changes to that string, but the problem
> can be the length. (String from VB has to be long enough or you get AV).

> Delphi in VB strings (and PChar) have a different header and you can not
> allocate string in Delphi and pass a pointer to it to VB. One option I
have
> not tried yet would be:

> 1.) allocate the string in Delphi
> 2.) typecast to PChar
> 3.) Return the PChar in the function result.
> 4.) Use a VB function (?? which one?) to type cast LPSTR (same as PChar)
> type string (zero terminated) to VB string type.

> Regards!
> Janez.

> > My Delphi code is something like

> > Library K;
> > Uses Functions;
> > Exports
> >  K_Description;
> > begin
> > end;

> > Unit Functions;
> > interface
> > function K_Description(var  Key:PChar) ; Pchar; stdcall;
> > implementation
> > function K_Description(var  Key:PChar) ; Pchar
> > begin
> >   K_Description := 'test string';
> > end;

> > Delphi creates the DLL properly. In a module for VBA, I have created the
> > following;

> > Public Declare Function K_Description Lib "K" (ByRef Key as String) as
> > String

> > Sub Test
> > Dim Desc as String
> > Dim Fluid as String
> > Fluid = "Dummy"
> > Desc = K_Description (Fluid)
> > end Sub

> > When I run test, it seems to call the DLL properly, but the string Desc
> does
> > not contain the proper value. Any suggestions how to work this out. I
have
> > found info on passing doubles, integer etc as function results but could
> not
> > found examples with strings.

> > Thanks in advance,
> > Martien

Other Threads