Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strings from VB6 to Delphi 7

304 views
Skip to first unread message

D M Wheeler

unread,
Mar 1, 2006, 10:59:33 PM3/1/06
to
I want to be able to transfer strings from VB6 to Delphi through a function
call that is in a dll.
The dll uses standard Delphi string variables throughout. I am having
trouble getting the variable in the function call to be assigned the the
Delphi string variable within the dll.
Does anyone have a method(s) for passing strings between VB6 and Delphi

We have managed the reverse, that is pass a string from Delphi to VB6
through a dll function using a widestring variable.


danny heijl

unread,
Mar 2, 2006, 3:27:16 AM3/2/06
to
D M Wheeler schreef:

> Does anyone have a method(s) for passing strings between VB6 and Delphi

VB6 uses BSTR (see SysAllocString en SysFreeString), you can access them
as a PWideChar. I think you have to specify ByVal to get VB to pass it
by reference (MS logic) to make it work.

The other way round worked because Widestring also uses SysAllocString
to be compatible with COM.

Danny
---

Thomas Mueller

unread,
Mar 2, 2006, 3:58:50 PM3/2/06
to
Hi,

D M Wheeler wrote:

> I want to be able to transfer strings from VB6 to Delphi through a
> function call that is in a dll.
> The dll uses standard Delphi string variables throughout. I am having
> trouble getting the variable in the function call to be assigned the the
> Delphi string variable within the dll.
> Does anyone have a method(s) for passing strings between VB6 and Delphi

You mean you want to pass strings between a VB6 application and a Delphi
dll? That's easy, since VB does some magic for you:

In Delphi, declare those string parameters as PChar:

function dosomething(_astring: PChar): integer; stdcall;
var
s: string;
begin
s := _astring;
Result := Length(s);
end;

In VB, you import this function as

declare function dosomething lib "mydll.dll" (byval astring as string) as
long

And then you can just call it as:

debug.print dosomething("trallala")

VB automatically converts its internal string data type to a PChar when
calling the function.

That was the easy part, it is a little bit more complex if you want to
return a string from the dll to VB, because you must preallocate memory for
it and afterwards remove everything after the first #0 from the string:

function returnstring(_astring: PChar; _MaxLen: integer): integer; stdcall;
const
RETURNVALUE = 'hallo';
begin
StrPLCopy(_astring, RETURNVALUE, _MaxLen);
Result := Length(RETURNVALUE);
end;

declare function returnstring lib "mydll.dll" (byval astring as string,
byval maxlen as long) as long

dim s as string
dim length as long

s = space$(255)
length = returnstring(s, len(s))
s = left$(s, length)
debug.print s

Alternatively:

dim s as string
dim i as long

s = space$(255)
call returnstring(s, len(s))
i = 1
while mid$(s, i 1) <> chr(0)
i = i + 1
wend
s = left$(s, i -1)
debug.print s

(all code above is untested, it might not even compile)

twm

Peter Below (TeamB)

unread,
Mar 2, 2006, 5:40:37 PM3/2/06
to
In article <44066da4$1...@newsgroups.borland.com>, D M Wheeler wrote:

> I want to be able to transfer strings from VB6 to Delphi through a function
> call that is in a dll.
> The dll uses standard Delphi string variables throughout. I am having
> trouble getting the variable in the function call to be assigned the the
> Delphi string variable within the dll.
> Does anyone have a method(s) for passing strings between VB6 and Delphi

If you declare a DLL function parameter as ByVal A as string on the VB side
it will arrive as a PChar on the Delphi side. THat is VBs mechanism to call
API functions that require PChars as input. Use stdcall calling convention.

By the way, the easiest way for VB programmers to interact with a DLL written
in another language is to make the DLL an OLE automation servers and export an
interface based on IInvoke. If the DLL contains a type library (which OLE
server DLLs generated by the IDE wizards do) the VB programmer can just add a
reference to the DLL to his/her project and use VBs poor excuse for object
types to work with the DLLs "objects".

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


0 new messages