[lazarus] Patches to Shanes files

michael at tfdec1.fys.kuleuven.ac.be michael at tfdec1.fys.kuleuven.ac.be
Wed Jul 7 16:54:43 EDT 1999




On Wed, 7 Jul 1999, Payne Thomas E CNIN wrote:

> FYI
> 
> This works with 0.99.12a of win32
> ----------------------
> program test;
> {$mode objpas}
> uses strings;
> var
>   newpchar : Pchar;
>   stringvar : String;
> begin
>   stringvar := '>hello<';
>   newpchar := StrAlloc(Length(StringVar)+1);
>   StrPCopy(newPChar,StringVar);
>   writeln(string(newpchar));
> end.

This is Correct. 
In objpas mode a pchar can be typecast to a shortstring.

> ----------------------
> 
> This does not
> ---------------------
> program test;
> {$mode delphi}
> uses strings;
> var
>   newpchar : Pchar;
>   stringvar : String;
> begin
>   stringvar := '>hello<';
>   newpchar := StrAlloc(Length(StringVar)+1);
>   StrPCopy(newPChar,StringVar);
>   writeln(string(newpchar));
> end.

In delphi mode, you can't typecast a pchar to a SHORTstring.
You can, however, do it if you use the -Sh switch or {$H+} 
so the string is an ansistring. I that case the program 
works correctly.

For both cases: 

1) The typecast isn't needed. You can directly write
   the value of the pchar.
2) NEVER use the 'strings' unit if you use ansistrings;
   the strings unit returns shortstrings only. If you do this,
   you get an additional conversion to ansistring.
   Instead, use the 'sysutils' unit; it has the same routines
   as 'strings', only with ansistrings.

Michael.






More information about the Lazarus mailing list