Board index » delphi » Q: how to convert a String to PChar?

Q: how to convert a String to PChar?

Hi,

Can anyone tell me how I convert a string to PChar?

I need the PChar to call the function CopyFile.

C-ya!

        Arjan

 

Re:Q: how to convert a String to PChar?


Quote
> 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)

Alexander

------------------------------

Re:Q: how to convert a String to PChar?


Quote
avdl...@worldaccess.nl (Arjan) wrote:
>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
i...@intelitech.com

Re:Q: how to convert a String to PChar?


On Thu, 23 Jan 1997 09:09:03 GMT, avdl...@worldaccess.nl (Arjan)
wrote:

Quote
>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/)

Re:Q: how to convert a String to PChar?


On Thu, 23 Jan 1997 11:24:51 +0100, Alexander Apostolovski

Quote
<apost...@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/)

Re:Q: how to convert a String to PChar?


Quote
Arjan wrote:

> Hi,
> Can anyone tell me how I convert a string to PChar?
> I need the PChar to call the function CopyFile.

  It's really easy.  Use 'Pchar(myString)' instead of using just
'myString' in the function...  

--
Jason
E...@DarkElf.reno.nv.us

Re:Q: how to convert a String to PChar?


Quote
Arjan wrote:

> Hi,

> Can anyone tell me how I convert a string to PChar?

> I need the PChar to call the function CopyFile.

> C-ya!

>         Arjan

If you have Dog which is a String;

then Dog := Dog + #0;

Addr(Dog[1]) is a Pchar

CopyFile(Addr(Dog[1]));

Re:Q: how to convert a String to PChar?


Quote
In article <32efa451.2947...@news.proaxis.com>, li...@tempest-sw.com wrote:
>>> Can anyone tell me how I convert a string to PChar?
>>> I need the PChar to call the function CopyFile.

It might be {*word*99} but I think I somewhen read something about
converting Stings using PChar(s).

As I said - It might be $&!X#1&.

bye, ralf.

Re:Q: how to convert a String to PChar?


Quote
Arjan wrote:
> 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

Re:Q: how to convert a String to PChar?


Quote
Jason Wallace wrote:

> Arjan wrote:

> > Hi,
> > Can anyone tell me how I convert a string to PChar?
> > I need the PChar to call the function CopyFile.

>   It's really easy.  Use 'Pchar(myString)' instead of using just
> 'myString' in the function...

> --
> Jason
> E...@DarkElf.reno.nv.us

This won't work for AnsiString because it is implemented like an Object.

Re:Q: how to convert a String to PChar?


Quote
> >> Can anyone tell me how I convert a string to PChar?

This works for me:

   @str[ 1 ]

regards,

Re:Q: how to convert a String to PChar?


Quote
Antonio Linares wrote:

> > >> Can anyone tell me how I convert a string to PChar?

> 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 {*word*42} never hurt anyone.

Re:Q: how to convert a String to PChar?


Quote
Jesper Frank Nemholt wrote:

> Antonio Linares wrote:

> > > >> Can anyone tell me how I convert a string to PChar?

> > This works for me:

> >    @str[ 1 ]

> > regards,

> This works for me :

>         My_PChar := PChar('name_of_string');

@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

Re:Q: how to convert a String to PChar?


Ray Lischner <li...@tempest-sw.com> wrote in article
<32efa451.2947...@news.proaxis.com>...

Quote
> On Thu, 23 Jan 1997 11:24:51 +0100, Alexander Apostolovski
> <apost...@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

Re:Q: how to convert a String to PChar?


Maybe I'm missing something, but if Borland created a specialised
StrPCopy operation, then doesn't it seem like a good idea to use it.

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.

Go to page: [1] [2]

Other Threads