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
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.
>>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
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:
>
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
>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
But I need a Function not a Sub. vs_GetString() has been around over 10
years. I can't change the declaration.
MED
But I need a Function not a Sub. vs_GetString() has been around over 10
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
"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.0000786...@antispam.compuserve.com...