Board index » delphi » Converting string to Pchar in D1.0?

Converting string to Pchar in D1.0?

How can I convert string type to PChar type in Delphi 1.0.
If anyone can help me please write to:

szy...@friko5.onet.pl

 

Re:Converting string to Pchar in D1.0?


Quote
Andrzej Szyluk wrote:
> How can I convert string type to PChar type in Delphi 1.0.
> If anyone can help me please write to:

> szy...@friko5.onet.pl

Try StrPCopy(Dest, Source);

\Francois Labonte

Re:Converting string to Pchar in D1.0?


In article <01bd9090$e94bef00$b7f674c3@default>

Quote
"Andrzej Szyluk" <szy...@polbox.com> wrote:
> How can I convert string type to PChar type in Delphi 1.0.

You can copy a string to a pChar by using the StrpCopy
function. But to convert a string to a pChar, use the following
function:

Function StringtoPChar(Var s:String):pChar;
{you need a "VAR" parameter for this function}
Var
s1:String;
Begin
   s1 := s;    {save the value of s}
   s:=s+#0; {add null terminator}
   StringtoPChar := @s[1]; {return the address of s[1]}
   s := s1; {restore s}
End;

Best regards, The Chief
--------
Dr. A{*word*73}la A. Olowofoyeku (The African Chief)
Email: la...@keele.ac.uk
Homepage: http://ourworld.compuserve.com/homepages/African_Chief/
Author of: Chief's Installer Pro v4.25 for Win16 and Win32:
ftp://ftp.demon.co.uk/pub/ibmpc/win3/apps/chief/pro/chief425.zip

Re:Converting string to Pchar in D1.0?


Quote
The African Chief wrote in message <6l97sb$su...@cfs2.kis.keele.ac.uk>...
>You can copy a string to a pChar by using the StrpCopy
>function. But to convert a string to a pChar, use the following
>function:

>Function StringtoPChar(Var s:String):pChar;
>{you need a "VAR" parameter for this function}
>Var
>s1:String;
>Begin
>   s1 := s;    {save the value of s}
>   s:=s+#0; {add null terminator}
>   StringtoPChar := @s[1]; {return the address of s[1]}
>   s := s1; {restore s}
>End;

Gosh!  I just think its worth commenting on how this works, and I also offer
a clearer version of this most excellent method!  I wish I had thought of
it!

@MyString[1]

is the address of the first byte of the string, MyString[0] being the
length.

If the last byte of the string is #0, then @MyString[1] can easily be cast
as a pchar!
But this isn't usually the case.  What the cheif is doing is somehow adding
a #0 to the end of the string, by making the string 1 byte longer with a #0
and then restoring the length, but the #0 is still in the string buffer,
although the string length no longer includes it.  So now the #0 is there
PChar(@MyString[1]) really is a read-only PChar.

[This only works for short strings (or Delphi 1 strings)]

A clearer and fuller version is below.  It has safety checking for length
against string buffer size.

Function StringToPChar(Var S : String) : PChar;
begin
  If Length(S)>=High(S) then
    Raise Exception.Create('Can''t add #0 to string, already at max
length');
  S[length(s)+1]:=#0;
  Result:=PChar(@S[1]);
end;

Note: String to PChar MAY need to be called each time a copy is made, even
in pass-by-value procedure calls!
I.E. don't shortcut and use @S[1] directly, in a certain well-defined set of
circumstances you could be in for a surprise.

FINALLY

if you want to pass this PChar to an API call that returns a string IN the
PChar, but whose return value is the length of the PChar, you can do

S[0]:=Char(byte(SomeWINAPICall(ccc,ccc,StringToPChar(s),high(s))));

High(S) is how you tell the API call the longest string it can write to the
buffer.  This is usually 255, but if you define your string like this:

S : String[20]

then it takes the value 20, or whatever!

If the WinAPI call does not return length you can do.

SomeWINAPICall(ccc,ccc,StringToPChar(s),high(s));
S[0]:=char(byte(StrLen(Pchar(@s[1]))));

NOTE we don't use StringToPChar in second line!  It alread is a PChar, and
we use StrLen to get the length, and then update the Delphi length.  It is
not safe to use StringToPChar, or wise to use the string until the Delphi
length has been reset in this manner.

Sam

Other Threads