Can anyone tell me how I convert a string to PChar?
I need the PChar to call the function CopyFile.
C-ya!
Arjan
StrPCopy or StrCopy (I always mix these two)
Alexander
------------------------------
>Hi,
>Can anyone tell me how I convert a string to PChar?
>I need the PChar to call the function CopyFile.
>C-ya!
> Arjan
Hi Arjan,
Like Alexander says..., StrPCopy. Here's an example:
uses SysUtils;
var
A: array[0..79] of Char;
S: String;
begin
S := 'Honk if you know Blaise.';
StrPCopy(A, S);
Canvas.TextOut(10, 10, StrPas(A));
end;
I hope this helps,
--
Luis Mourao
http://www.intelitech.com
in...@intelitech.com
>Can anyone tell me how I convert a string to PChar?
>I need the PChar to call the function CopyFile.
D2 handles this for you. Just cast the string to PChar. In D1, it's
harder because you have no guarantee that the string has a zero byte
at its end, so you might need to make a copy of the string.
Secrets of Delphi 2 has a StrToPChar function that takes care of these
details for you. In D2, it simply uses a type cast, PChar(str). In D1,
it checks to see if the string already has a zero byte at its end, in
which case, it returns a pointer to the first character in the string.
Otherwise, it dynamically allocates a character array and copies the
Pascal string. It remembers several old strings, and automatically
frees the strings after a certain number of allocations. This achieves
a nice balance between having too many strings lying around, wasting
memory, and being able to use several PChar strings simultaneously in
a Windows API call.
--
Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)
>> Can anyone tell me how I convert a string to PChar?
>> I need the PChar to call the function CopyFile.
>
>StrPCopy or StrCopy (I always mix these two)
As a general rule, I prefer StrPLCopy to StrPCopy. If the source
string is longer than the destination character array, StrPLCopy does
not run off the end of the array. StrPCopy overwrites memory and
causes your program to misbehave.
It's really easy. Use 'Pchar(myString)' instead of using just
'myString' in the function...
--
Jason
E...@DarkElf.reno.nv.us
If you have Dog which is a String;
then Dog := Dog + #0;
Addr(Dog[1]) is a Pchar
CopyFile(Addr(Dog[1]));
It might be crap but I think I somewhen read something about
converting Stings using PChar(s).
As I said - It might be $ง&!X#1&ง.
bye, ralf.
> Can anyone tell me how I convert a string to PChar?
StrPCopy(PCharString, AnsiString);
--
-------------------------------------------------------
"I never gave anybody Hell. I just told them the truth
and they thought it was Hell..." -Harry S. Truman
This won't work for AnsiString because it is implemented like an Object.
This works for me:
@str[ 1 ]
regards,
This works for me :
My_PChar := PChar('name_of_string');
l8r/Jspr
--
DASSIC PRODUCTIONS
------------------
c/o Jesper Frank Nemholt Tel : +45 75 82 52 56
Pilevaenget 23 2. MF. Fax : +45 75 82 52 56
7100 Vejle WWW : http://login.dknet.dk/~dassic
Denmark FTP : login.dknet.dk:/pub/dassic
Planet Earth... E-Mail : das...@login.dknet.dk
- - -
Large cats can be dangerous, but a little pussy never hurt anyone.
@str[1] works if there is a #0 at the end, which you might have to put
there manually if it is a shortstring.
PChar(str) does not work for a shortstring.
The preferable way must be strpcopy or strplcopy since there is
compatibility between ansistring and shortstring.
--
Svante Granqvist Speech, Music and Hearing
Phone +46-8-790 7561 Box 700 14
Fax +46-8-790 7854 S-100 44 Stockholm
mailto:sva...@speech.kth.se http://www.speech.kth.se/~svante
Ray Lischner <li...@tempest-sw.com> wrote in article
<32efa451...@news.proaxis.com>...
> On Thu, 23 Jan 1997 11:24:51 +0100, Alexander Apostolovski
> <apos...@cs.utwente.nl> wrote:
>
> >> Can anyone tell me how I convert a string to PChar?
> >> I need the PChar to call the function CopyFile.
> >
> >StrPCopy or StrCopy (I always mix these two)
>
> As a general rule, I prefer StrPLCopy to StrPCopy. If the source
> string is longer than the destination character array, StrPLCopy does
> not run off the end of the array. StrPCopy overwrites memory and
> causes your program to misbehave.
>
> --
> Ray Lischner, Tempest Software, Inc., Corvallis, Oregon, USA
> Author of Secrets of Delphi 2 (http://www.tempest-sw.com/secrets/)
>
It seems as if Ray Hit the Nail on the Head...
The simplest way to convert a string to a PChar is to typecast it as a
PChar in the following manner (a very simple example but the method works
with everything from simple text assignment to the Windows API):
procedure TForm1.Button1Click(Sender: TObject);
var
MyString: String;
begin
// Typecast the AnsiString as a PChar like this --> PChar(MyString) as
shown // below
MessageBox(Handle, PChar(MyString), nil, mb_OK);
end;
Good Luck,
Andre
Its like incrementing values; just as easy to do I := I + 1 as Inc(I),
but you'd have to assume that the compiler generates better code (ie
uses the inc assembler command) using the Delphi preferred method.
Why the hell else would they have a StrPCopy operation?
Michael.
In fact, it will work, but only with Delphi 2.0. If you modify the PChar
while in the function, you need to perform a redundant cast:
myString := PChar(WhateverThePCharNameIs);
This will reset the length info in the AnsiString.
If you don't believe me, place a Button and three (3) Label components
on a form. Double-click the button and add the following:
procedure TForm1.Button1Click(Sender: TObject);
var
name : AnsiString;
TempName :PChar;
begin
name := 'Dummy Text';
Label3.Caption := IntToSTr(Length(name)) + ' ' + name;
TempName := PChar(name);
GetWindowText(Handle, TempName, Length(name));
Label1.Caption := IntToStr(Length(name)) + ' ' + name;
name := PChar(TempName);
Label2.Caption := IntToStr(Length(name)) + ' ' + name;
end;
Run, click the Button, and see the lengths and the captions.
Hope this helps,
--
Tim Gooch
Editor-in-Chief, Moderator,
Delphi Developer's Journal DDJ-THREAD - Delphi Discussion List
[ http://www.cobb.com/ddj ] [ mailto: list...@cobbtips.com ]
( subscribe ddj-thread <your_name> )
Because strpcopy is needed for the old (delphi 1 & BP7) type of strings
which are known as shortstring in delphi 2. Strpcopy was introduced
to convert (old style) strings to the C-ish windows environment, long
before huge strings were invented.
pchar(ansistring) works fine, though, and I'd guess that it is the
most efficient for ansistrings (but not sure, the compiler IS smart)
It depends on what you're doing. StrPCopy makes a copy of the
string, while simply typecasting the string to a PChar doesn't copy
anything, it just gives a pointer to the first character. If you're
going to be modifying the contents of the string probably it should
be StrPCopy - the new strings are smart enough to get very confused
if you start manipulating them other than via Delphi string functions.
Otoh if, say, you have a string and you just want to pass it to an
API function (a function which will not change the "string") then
I don't see what's wrong with the typecast (unless compatibility
with D1 is an issue).
--
David Ullrich
?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)
>> I need the PChar to call the function CopyFile.
>In Delphi 2.0 you can just typecast:
> P := pChar( S );
In Delphi 1 and 2 you can use:
StrPCopy(PCharPath, Path);
-- Ulrich Kroener