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

Returning String to VB from Delphi DLL

244 views
Skip to first unread message

Med Shakeri [Vista Software]

unread,
Sep 1, 2001, 3:07:24 AM9/1/01
to
Hi All,

The vs_GetString() function in a DLL written in Delphi needs to return a
string to VB app. I am having fun with this. The result is inconsistent
and eratic. I am doing the following:

function ConvertToBSTR(src:pchar):TBStr;
begin
sBSTR := SysAllocStringByteLen(pchar(src), strlen(src) ) ;
Result:= sBSTR;
end;

// this is called by the VB app
function vs_GetTrimString(cpFieldName: PChar): PChar;
begin
Result := PChar(ConvertToBSTR(GetTrimString(pchar(cpFieldName))));
end;

What am I doing wrong? Is there a special trick returning strings to VB
from a Delphi DLL? TIA.

MED


Peter Below (TeamB)

unread,
Sep 1, 2001, 9:08:56 AM9/1/01
to

VB does not understand PChars as a function result. Simply forget the
ConvertToBSTR routine and return a WideString, that is a BSTR behind the
scenes. And use stdcall calling convention, that is the only one VB
understands.

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!
Note: I'm unable to visit the newsgroups every day at the moment,
so be patient if you don't get a reply immediately.

Med Shakeri [Vista Software]

unread,
Sep 1, 2001, 9:47:22 AM9/1/01
to
Peter,

>>Simply forget the ConvertToBSTR routine and return a WideString, that is a
BSTR behind the
>>scenes

Thanks for the quick reply. Does this mean I have no choice other than to
use ShareMem and deploy Borlndmm.dll?

MED


Pamie

unread,
Sep 1, 2001, 12:05:14 PM9/1/01
to
Delphi pChar's are compatible with VB string parameters passed ByVal.

Below is a simple example based on the method I use.

Remember:

(1) Always pass the VB string ByVal, even though the DLL is returning a
value. It's already a pointer.

(2) Always Dim the VB string one character larger than the largest
possible returned value, to provide space for the nul at the end of the
string.

(3) The stdcall calling convention must be explicitly stated.

Many times you will see the maximum allowed length passed as another
parameter, and the length actually returned as a function result. Or you
can use StrPLCopy to limit the returned value to the maximum length - 1.
The choice is yours.

DELPHI DLL
==========

procedure ConvertNumber (I : smallint; S : pChar); stdcall;
begin
StrPCopy(S, IntToStr(I));
end;

exports
ConvertNumber;

end.

VB CALLING APPLICATION
======================

Private Declare Sub ConvertNumber Lib "MyDLL.dll" _
(ByVal I As Integer, ByVal S As String)

Private Sub Command1_Click()
Dim S As String * 20
Text1.Text = ""
ConvertNumber 110, S
Text1.Text = S
End Sub


"Med Shakeri [Vista Software]" wrote:
>

Med Shakeri [Vista Software]

unread,
Sep 1, 2001, 12:19:04 PM9/1/01
to
Pamie,

Thanks for the reply. However, this doesn't answer my question. In your
example, you are using a Sub. I have already got this one covered using
ByVal. It is DLL Function returning a String that I have problems with.

Declare Function vs_GetTrimString Lib "adbx60.dll" (ByVal cpFieldName As
String) As String

MED


Manuel Algora

unread,
Sep 1, 2001, 3:03:04 PM9/1/01
to
On Sat, 1 Sep 2001 12:19:04 -0400, "Med Shakeri [Vista Software]"
<me...@vistasoftware.com> wrote:

>example, you are using a Sub. I have already got this one covered using
>ByVal. It is DLL Function returning a String that I have problems with.

Making a Sub with a var parameter to return a PChar lets you avoid
using ShareMem.

Manuel Algora
m...@encomix.es

Med Shakeri [Vista Software]

unread,
Sep 1, 2001, 3:14:05 PM9/1/01
to
> Making a Sub with a var parameter to return a PChar lets you avoid
> using ShareMem.

But I need a Function not a Sub. vs_GetString() has been around over 10
years. I can't change the declaration.


MED

Med Shakeri [Vista Software]

unread,
Sep 1, 2001, 3:17:37 PM9/1/01
to
> Making a Sub with a var parameter to return a PChar lets you avoid
> using ShareMem.

But I need a Function not a Sub. vs_GetString() has been around over 10

Peter Below (TeamB)

unread,
Sep 2, 2001, 4:31:50 AM9/2/01
to
In article <3b90e7ac$1_2@dnews>, Med Shakeri [Vista Software] wrote:
> Thanks for the quick reply. Does this mean I have no choice other than to
> use ShareMem and deploy Borlndmm.dll?
>

Widestrings do not use the Borland memory manager, they get their memory
from an OS-supplied memory manager. Sharemem has no relevance when you use a
Delphi DLL with a non-Delphi host app.

Med Shakeri [Vista Software]

unread,
Sep 2, 2001, 9:42:25 PM9/2/01
to
It's a go then. Thank you.

MED

"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.0000786...@antispam.compuserve.com...

0 new messages