Board index » delphi » PChar<->String

PChar<->String

Hi all helpers

How can I change a variable of the type STRING to a variable with the
type PCHAR.

Example:

VAR pmsg1,pmsg2:PCHAR;
    msg1,msg2:STRING;

begin
....
  pmsg1:=PCHAR(msg1);   {doesn't exist}
  msg2:=STRING(pmsg2);  {doesn't work}
...
end.

I hope, somebody can show me, how I can execute the two commands above.
Thanks a lot

Oli

--
Oliver Wulff
Im Acher 5
CH-8494 Bauma

****************

email: owu...@stud.ee.ethz.ch

 

Re:PChar<->String


Oliver Wulff <owu...@stud.ee.ethz.ch> wrote in article
<329C49FF.7...@stud.ee.ethz.ch>...

Quote
> How can I change a variable of the type STRING to a variable with the
> type PCHAR.

> Example:

> VAR pmsg1,pmsg2:PCHAR;
>     msg1,msg2:STRING;

> begin
> ....
>   pmsg1:=PCHAR(msg1);   {doesn't exist}
>   msg2:=STRING(pmsg2);  {doesn't work}
> ...
> end.

Hi Oli,

To copy a variable of type String to one of type PChar, use the StrPCopy
function as follows:

  StrPCopy(pmsg1, msg1);

To copy a variable of type PChar to one of type String, use the StrPas
function as follows:

  msg2 := StrPas(pmsg2);

(Make sure to include SysUtils in your USES clause)

You can't TYPECAST in Delphi, as you were trying in your example, Object
Pascal is much to well behaved to allow that sort of thing!

James.

Re:PChar<->String


I'm assuming this works the same as in TPW 1.5, in which case the
conversion
routines are:
function StrPas(p:pchar):string;
function StrPCopy(var p:pchar; s:string):pchar;

Rob

Oliver Wulff <owu...@stud.ee.ethz.ch> wrote in article
<329C49FF.7...@stud.ee.ethz.ch>...

Quote
> Hi all helpers

> How can I change a variable of the type STRING to a variable with the
> type PCHAR.

> Example:

> VAR pmsg1,pmsg2:PCHAR;
>     msg1,msg2:STRING;

> begin
> ....
>   pmsg1:=PCHAR(msg1);   {doesn't exist}
>   msg2:=STRING(pmsg2);  {doesn't work}
> ...
> end.

> I hope, somebody can show me, how I can execute the two commands above.
> Thanks a lot

> Oli

> --
> Oliver Wulff
> Im Acher 5
> CH-8494 Bauma

> ****************

> email: owu...@stud.ee.ethz.ch

Re:PChar<->String


Oliver Wulff <owu...@stud.ee.ethz.ch> wrote in article =
<329C49FF.7...@stud.ee.ethz.ch>...

Quote
>=20
> How can I change a variable of the type STRING to a variable with the
> type PCHAR.
>=20

You cannot do this directly.  However, there are three approaches which =
may help.

1. Declare a variable which is an array of chars of sufficient size to =
hold your string, and to copy one to the other:

var
    sString : String ;
    szArray : Array[ 0..30 ] of Char ;

procedure SomeProcedure ;
begin
    StrPCopy( @szArray, sString ) ;
end ;

2. Similar to the above, but allocate the array dynamically.  Don't =
forget the extra char required for the null terminator (and don't forget =
to FREE the memory either):

var
    sString : String ;
    pszChar : PChar ;

procedure SomeProcedure ;
begin
    GetMem( pszChar, Length( sString ) + 1 ) ;

    StrPCopy( pszChar, sString ) ;

    FreeMem( pszChar, Length( sString ) + 1 ) ;
end ;

3. If all you need is to be able to reference your string as a PChar, =
but do not need to take a copy of it, you can manually add a null =
terminator and reference the array of chars maintained by the pascal =
String type:

var
    sString : String ;

procedure SomeProcedure ;
begin
    sString :=3D sString + #0 ;

    SomeFnThatRequiresPChar( @sString[ 1 ] ) ;
end ;

NOTE: This latter approach is not portable between 16 & 32 bits.  i.e. =
This will not be allowed for huge strings in D2.  D2 makes things easier =
by allowing direct casting to PChar type of huge strings, but short =
strings will still require one or other of the above approaches.

Hope this helps,=20

Jolyon Smith.

Other Threads