Board index » delphi » Passing String type in DLL

Passing String type in DLL

Someone told me that I should't use string type for DLL Parameters or/and
Result.
If I want to use them, then I must add : USES ShareMem.
Is it true ?

Because of that, then I used PChar.
But, I don't familiar with that type.
So .. when I made function, then at the run-time many many error will be
rise.
What shoul I do ?

 

Re:Passing String type in DLL


Example:

DLL code:

procedure SendStringFromAppToDLL(Str: PChar; Len: Integer);
var
  S: String;
begin
  SetString(S, Str, Len);
  // do someting with string S
end;

function SendStringFromDLLToApp(Str: PChar; MaxLen: Integer): Integer;
var
  S: String;
begin
  // assign something to string S
  Result := StrLCopy(Str, PChar(S), MaxLen) - Str;
end;

App code:

var
  S: String;
begin
  // assign something to string S
  SendStringFromAppToDLL(PChar(S), Length(S));
end;

var
  Buffer: array[0..255] of Char;
  S: String;
begin
  SetLength(S, Buffer, SendStringFromDLLToApp(PChar(Buffer),
SizeOf(Buffer));
  // do someting with string S
end;

"evi" <e...@datacom.rad.net.id> schreef in bericht
news:0dSZ5.11604$K6.85518@news...

Quote
> Someone told me that I should't use string type for DLL Parameters or/and
> Result.
> If I want to use them, then I must add : USES ShareMem.
> Is it true ?

> Because of that, then I used PChar.
> But, I don't familiar with that type.
> So .. when I made function, then at the run-time many many error will be
> rise.
> What shoul I do ?

Re:Passing String type in DLL


Quote
evi <e...@datacom.rad.net.id> wrote:
> Someone told me that I should't use string type for DLL Parameters or/and
> Result.
> If I want to use them, then I must add : USES ShareMem.
> Is it true ?
> Because of that, then I used PChar.
> But, I don't familiar with that type.
> So .. when I made function, then at the run-time many many error will be
> rise.
> What shoul I do ?

Hi,

I had the same problem some time ago.

I didnt use ShareMem and PChar is quite difficult to handle.

I read somewhere (I think in the help of DELPHI) that you can use
ShortString if you are using strings up to 255 chars. I used this and
everything works alright.

If you have strings that are longer than 255 character then you have to use
PChar. But you must be sure whether you wanna put in a PChar or you make one
in the routine and return this...
But be careful, if you return the PChar defined in the routine it may be a
false pointer because the original PChar is already deleted.

Hope it helps.

cu,
Toralf

Re:Passing String type in DLL


Yes, you must use SHAREMEM as first in USES list in both application
and DLL.
This is a real pain in the neck!

Use PChar as you did. I do it all the time and never had problems.
Use PChar(MyString) to convert string and StrPas(MyPChar) to do
inverse.

If you know how to work with pointers, then you can pass memory
address and length.

On Wed, 13 Dec 2000 13:46:25 +0700, "evi" <e...@datacom.rad.net.id>
wrote:

Quote
>Someone told me that I should't use string type for DLL Parameters or/and
>Result.
>If I want to use them, then I must add : USES ShareMem.
>Is it true ?

>Because of that, then I used PChar.
>But, I don't familiar with that type.
>So .. when I made function, then at the run-time many many error will be
>rise.
>What shoul I do ?

Regards,
E.Beli
http://www.greenhousemm.com

Re:Passing String type in DLL


Delphi is rather picky about Strings and DLLs - hence the load of text
generated as a warning when you start a new DLL project

Others have covered the PChar approach

One cludge is to pass back : OleVariants

Another is to 'create' your own type of String which really just uses
pure memory - I think that there is a pretty strong case for this -
effectively it is encapsulating the PChar approach once and for all.

On Wed, 13 Dec 2000 13:46:25 +0700, "evi" <e...@datacom.rad.net.id>
wrote:

Quote
>Someone told me that I should't use string type for DLL Parameters or/and
>Result.
>If I want to use them, then I must add : USES ShareMem.
>Is it true ?

>Because of that, then I used PChar.
>But, I don't familiar with that type.
>So .. when I made function, then at the run-time many many error will be
>rise.
>What shoul I do ?

Other Threads