Pergunta

I'm trying to port a project from Delphi 4 to Delphi XE2. I have a requirement to use shortstring in the project. According to the Delphi Help, $H- should make the compiler use shortstrings for the string type. I've used this directive but I don't see any difference. I've written a small test program:

program Stringtest;

{$APPTYPE CONSOLE}

{$R *.res}
{$H- }
uses
  System.SysUtils;

var
 str : string;
 short : shortstring;
begin
  try
    str := 'testing';
    short := 'testing';
    Writeln('str ' +Format('%d', [sizeOf(str)]) );
    Writeln('short  ' +Format('%d', [sizeOf(short)])  );
    Readln; 
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

In my opinion the output should be the same for both str and short because the compiler should treat both as a shortstring. But the size is 4 for str and 256 for short. Is there any other way to have the compiler treat string as shortstring or is the only solution to replace all occurences of string with shortstring in the source code?

Foi útil?

Solução

$H- is legacy, provided only for backward compatibility. There is no way to force the Delphi XE2 (or any version of Delphi's compiler from D2009 and higher) to not use Unicode as the default string type.

The compiler directive is still there just to prevent old code from breaking when compiled. There was a bit of an uproar when Delphi 2009 was released with Unicode-only as the default string type, without a switch to revert back to having AnsiString as the default. It was explained that they determined this would be virtually impossible due to the need to have two different versions of the RTL, VCL, and so forth. Allowing ShortString to become the default would require the same thing.

You'll need to explicitly change all of your string references to shortstring (or better yet, fix your code to not require them).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top