Board index » delphi » Passing string from Delphi exe to C++ Builder dll

Passing string from Delphi exe to C++ Builder dll

Quote
Eric S wrote in message <3c4ea2ba.9683...@news.iprimus.com.au>...
>Hi,

>Does anybody know how to send a string from Delphi exe to C++ Builder
>dll ? I can easily enough send a string from dll to exe, but not the
>other way. A WORKING example would be very appreciated.

Send me licenses for Delphi and C++ Builder, and you'll
have your examples in the returning mail. Latest, most
extensive versions, please.

Sheesh.

Groetjes,
Maarten Wiltink

 

Re:Passing string from Delphi exe to C++ Builder dll


On Wed, 23 Jan 2002 15:06:04 +0100, "Maarten Wiltink"

Quote
<maar...@kittensandcats.net> wrote:
>Eric S wrote in message <3c4ea2ba.9683...@news.iprimus.com.au>...
>>Hi,

>>Does anybody know how to send a string from Delphi exe to C++ Builder
>>dll ? I can easily enough send a string from dll to exe, but not the
>>other way. A WORKING example would be very appreciated.

>Send me licenses for Delphi and C++ Builder, and you'll
>have your examples in the returning mail. Latest, most
>extensive versions, please.

>Sheesh.

>Groetjes,
>Maarten Wiltink

Maarten,

I don't get it. Does this group require that I first apologize for my
ignorance ?
OK, here goes: I'm sorry. In particular I'd like to apologize for any
hardship caused to you personally :-)

Anyway, to get back to my previous request for help:
I can transfer text data between C++ dll and Delphi exe doing the
following:

in DLL:

extern "C" __declspec(dllexport) char* GetLocalString(void)
{
    return "Hello from DLL";

Quote
}

in EXE:

DLLHandle := LoadLibrary('project1.dll');
if DLLHandle <> 0 then
begin
@GetLocalString := GetProcAddress(DLLHandle, '_GetLocalString');
self.Caption := GetLocalString ;
end;
FreeLibrary(DLLHandle);

It works well enough. Now I want to send a string from EXE to DLL. So
far nothing I tried works. The string doesn't need to be very long. It
is just a file name, so I could pass is as PChar or ShortString - then
I wouldn't have to use ShareMem and distribute BORLNDMM.DLL.

Any helpful suggestions?

Thanks
Eric

Re:Passing string from Delphi exe to C++ Builder dll


Quote
Eric S <se...@iprimus.com.au> wrote in message

news:3c4f493d.620133@news.iprimus.com.au...

Quote
> On Wed, 23 Jan 2002 15:06:04 +0100, "Maarten Wiltink"
> <maar...@kittensandcats.net> wrote:

[A little usenet interaction]

Quote

> in DLL:

> extern "C" __declspec(dllexport) char* GetLocalString(void)
> {
>     return "Hello from DLL";
> }

> in EXE:

> DLLHandle := LoadLibrary('project1.dll');
> if DLLHandle <> 0 then
> begin
> @GetLocalString := GetProcAddress(DLLHandle, '_GetLocalString');
> self.Caption := GetLocalString ;
> end;
> FreeLibrary(DLLHandle);

> It works well enough.

Hmmm... maybe well enough, but I can't be sure without seeing the
declaration of GetProcAddress in your Delphi code. Normally you would have
to 'fixup' the length of the returned string to match the null terminator.

Now what you are after. The reverse process  is pretty straightforward....

In your DLL

 extern "C"__declspec(dllexport) void SaySomething(char *szText)
{
  ShowMessage(szText);

Quote
}

In your exe

procedure SaySomething(sText: PChar);cdecl;external 'SpeakDll.dll' name
'_SaySomething';  // I've used a static call for simplicity
implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
 SaySomething(Pchar('Hi'));
end;

Use  the above cast to PChar with care. If you intend to save the value
within your dll, consider the lifespan of the piece of memory it  points to.

Dave

Re:Passing string from Delphi exe to C++ Builder dll


Quote
Eric S wrote in message <3c4f493d.620...@news.iprimus.com.au>...
>On Wed, 23 Jan 2002 15:06:04 +0100, "Maarten Wiltink"
><maar...@kittensandcats.net> wrote:

>>Eric S wrote in message <3c4ea2ba.9683...@news.iprimus.com.au>...
>>>Hi,

>>>Does anybody know how to send a string from Delphi exe to C++ Builder
>>>dll ? I can easily enough send a string from dll to exe, but not the
>>>other way. A WORKING example would be very appreciated.

>>Send me licenses for Delphi and C++ Builder, and you'll
>>have your examples in the returning mail. Latest, most
>>extensive versions, please.

>>Sheesh.

>>Groetjes,
>>Maarten Wiltink

>Maarten,

>I don't get it. Does this group require that I first apologize for my
>ignorance ?

Absolutely not. I meant in no way to imply that you are ignorant.
But your question is just one step above an implementation issue,
and you ask for working (read: tested) code for two rather expensive
environments. You can ask me for advice, for explanations of things,
for design, for comments, and at worst I'll tell you you're stupid
for having problems with something so easy (this occurs with quite
depressing regularity, mainly involving things that are explained
in the help files which of course people have never read). But ask
me for code, and I tend to bounce the question back and tell you
to pay for solving problems. I'm willing to _educate_ anybody who
shows promise. Call it evangelisation. I'm not willing to sit here
and do your job.

Quote
>OK, here goes: I'm sorry. In particular I'd like to apologize for any
>hardship caused to you personally :-)

Sorry for what? The exact amount of actual hardship is zero;
you're apologising for all the things in an empty set. Beep,
EDivisionByZero.

- Show quoted text -

Quote
>Anyway, to get back to my previous request for help:
>I can transfer text data between C++ dll and Delphi exe doing the
>following:

>in DLL:

>extern "C" __declspec(dllexport) char* GetLocalString(void)
>{
>    return "Hello from DLL";
>}

>in EXE:

>DLLHandle := LoadLibrary('project1.dll');
>if DLLHandle <> 0 then
>begin
>@GetLocalString := GetProcAddress(DLLHandle, '_GetLocalString');
>self.Caption := GetLocalString ;
>end;
>FreeLibrary(DLLHandle);

>It works well enough. Now I want to send a string from EXE to DLL. So
>far nothing I tried works. The string doesn't need to be very long. It
>is just a file name, so I could pass is as PChar or ShortString - then
>I wouldn't have to use ShareMem and distribute BORLNDMM.DLL.

See how different this looks? "I have this code, and it works. But
<this> doesn't, I don't get it, what's my problem?" Instead of "Hey,
please solve <this> for me, and mail it to foo....@hotmail.com so
nobody else can learn from it." (Yes, elements of hyperbole there.)

The answer is that Delphi (specific) and C (general) use different
string types, and the C model is the standard to which other people
generally write. So when crossing languages, you mostly use C strings,
which in Delphi are modeled by PChars. Delphi long strings can be
cast to PChars, but there are some caveats, mostly to do with reference
counting. The solution is quite simply to never mess with other people's
strings and always copy them to a buffer of your own at the C side.
(This tends to happen automatically at the Delphi side. This strategy
of compiler magic makes Delphi strings really easy to use, but also
a bit dangerous if you don't _know_ what happens and can code to avoid
the things that shouldn't happen.)

If you get a pointer into the wrong memory space, you have a bit of
a problem. This was touched on in a thread of a while ago, but I don't
remember any details on that, except that I think GlobalAlloc was
mentioned as a source of mutually visible memory. However, it looks
hopeful that you can read the string from the DLL.

If you tried casting long strings to PChars and that didn't work,
note that you told us what worked, not what didn't. It's frustrating,
but you often have to come out with your least presentable parts to
get your problem fixed.

Groetjes,
Maarten Wiltink

Re:Passing string from Delphi exe to C++ Builder dll


Thanks Dave.

On Thu, 24 Jan 2002 15:20:37 +1000, "David Reeve"

Quote
<drscienti...@powerup.com.au> wrote:

>Eric S <se...@iprimus.com.au> wrote in message
>news:3c4f493d.620133@news.iprimus.com.au...
>> On Wed, 23 Jan 2002 15:06:04 +0100, "Maarten Wiltink"
>> <maar...@kittensandcats.net> wrote:

>[A little usenet interaction]

>> in DLL:

>> extern "C" __declspec(dllexport) char* GetLocalString(void)
>> {
>>     return "Hello from DLL";
>> }

>> in EXE:

>> DLLHandle := LoadLibrary('project1.dll');
>> if DLLHandle <> 0 then
>> begin
>> @GetLocalString := GetProcAddress(DLLHandle, '_GetLocalString');
>> self.Caption := GetLocalString ;
>> end;
>> FreeLibrary(DLLHandle);

>> It works well enough.

>Hmmm... maybe well enough, but I can't be sure without seeing the
>declaration of GetProcAddress in your Delphi code. Normally you would have
>to 'fixup' the length of the returned string to match the null terminator.

>Now what you are after. The reverse process  is pretty straightforward....

>In your DLL

> extern "C"__declspec(dllexport) void SaySomething(char *szText)
>{
>  ShowMessage(szText);
>}

>In your exe

>procedure SaySomething(sText: PChar);cdecl;external 'SpeakDll.dll' name
>'_SaySomething';  // I've used a static call for simplicity
>implementation

>{$R *.DFM}

>procedure TForm1.Button1Click(Sender: TObject);
>begin
> SaySomething(Pchar('Hi'));
>end;

>Use  the above cast to PChar with care. If you intend to save the value
>within your dll, consider the lifespan of the piece of memory it  points to.

>Dave

Re:Passing string from Delphi exe to C++ Builder dll


Maarten,

After re-reading my original post I think I know what you mean.
Will keep that in mind next time.

Cheers
Eric

On Thu, 24 Jan 2002 09:56:47 +0100, "Maarten Wiltink"

Quote
<maar...@kittensandcats.net> wrote:
>Eric S wrote in message <3c4f493d.620...@news.iprimus.com.au>...
>>On Wed, 23 Jan 2002 15:06:04 +0100, "Maarten Wiltink"
>><maar...@kittensandcats.net> wrote:

>>>Eric S wrote in message <3c4ea2ba.9683...@news.iprimus.com.au>...
>>>>Hi,

>>>>Does anybody know how to send a string from Delphi exe to C++ Builder
>>>>dll ? I can easily enough send a string from dll to exe, but not the
>>>>other way. A WORKING example would be very appreciated.

>>>Send me licenses for Delphi and C++ Builder, and you'll
>>>have your examples in the returning mail. Latest, most
>>>extensive versions, please.

>>>Sheesh.

>>>Groetjes,
>>>Maarten Wiltink

>>Maarten,

>>I don't get it. Does this group require that I first apologize for my
>>ignorance ?

>Absolutely not. I meant in no way to imply that you are ignorant.
>But your question is just one step above an implementation issue,
>and you ask for working (read: tested) code for two rather expensive
>environments. You can ask me for advice, for explanations of things,
>for design, for comments, and at worst I'll tell you you're stupid
>for having problems with something so easy (this occurs with quite
>depressing regularity, mainly involving things that are explained
>in the help files which of course people have never read). But ask
>me for code, and I tend to bounce the question back and tell you
>to pay for solving problems. I'm willing to _educate_ anybody who
>shows promise. Call it evangelisation. I'm not willing to sit here
>and do your job.

>>OK, here goes: I'm sorry. In particular I'd like to apologize for any
>>hardship caused to you personally :-)

>Sorry for what? The exact amount of actual hardship is zero;
>you're apologising for all the things in an empty set. Beep,
>EDivisionByZero.

>>Anyway, to get back to my previous request for help:
>>I can transfer text data between C++ dll and Delphi exe doing the
>>following:

>>in DLL:

>>extern "C" __declspec(dllexport) char* GetLocalString(void)
>>{
>>    return "Hello from DLL";
>>}

>>in EXE:

>>DLLHandle := LoadLibrary('project1.dll');
>>if DLLHandle <> 0 then
>>begin
>>@GetLocalString := GetProcAddress(DLLHandle, '_GetLocalString');
>>self.Caption := GetLocalString ;
>>end;
>>FreeLibrary(DLLHandle);

>>It works well enough. Now I want to send a string from EXE to DLL. So
>>far nothing I tried works. The string doesn't need to be very long. It
>>is just a file name, so I could pass is as PChar or ShortString - then
>>I wouldn't have to use ShareMem and distribute BORLNDMM.DLL.

>See how different this looks? "I have this code, and it works. But
><this> doesn't, I don't get it, what's my problem?" Instead of "Hey,
>please solve <this> for me, and mail it to foo....@hotmail.com so
>nobody else can learn from it." (Yes, elements of hyperbole there.)

>The answer is that Delphi (specific) and C (general) use different
>string types, and the C model is the standard to which other people
>generally write. So when crossing languages, you mostly use C strings,
>which in Delphi are modeled by PChars. Delphi long strings can be
>cast to PChars, but there are some caveats, mostly to do with reference
>counting. The solution is quite simply to never mess with other people's
>strings and always copy them to a buffer of your own at the C side.
>(This tends to happen automatically at the Delphi side. This strategy
>of compiler magic makes Delphi strings really easy to use, but also
>a bit dangerous if you don't _know_ what happens and can code to avoid
>the things that shouldn't happen.)

>If you get a pointer into the wrong memory space, you have a bit of
>a problem. This was touched on in a thread of a while ago, but I don't
>remember any details on that, except that I think GlobalAlloc was
>mentioned as a source of mutually visible memory. However, it looks
>hopeful that you can read the string from the DLL.

>If you tried casting long strings to PChars and that didn't work,
>note that you told us what worked, not what didn't. It's frustrating,
>but you often have to come out with your least presentable parts to
>get your problem fixed.

>Groetjes,
>Maarten Wiltink

Re:Passing string from Delphi exe to C++ Builder dll


Quote
Eric S wrote in message <3c509ec4.3328...@news.iprimus.com.au>...
>Maarten,

>After re-reading my original post I think I know what you mean.
>Will keep that in mind next time.

Thank you.

Groetjes,
Maarten Wiltink

Other Threads