Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Delphi XE3 migration errors?

I am migrating my Delphi 5 application to Delphi XE3. I am getting some erros while compiling it. Can someone please help me to resolve these. Thanks for help in advance.

  1. I am not able to find defination of function OemToChar in XE3. When I Ctrl+Click on that function it shows message Unable to locate 'WinAPI.Windows.pas'. I am not able to open any delphi component file. What is the location of windows.pas located on the system ? or How to resolve it ?

  2. Incompatiable Types: 'PAnsiChar' and 'PWideChar' in below function on line with OemToChar(p1, p2).

function OemToAnsi(const Str: string): string;
var
  p1,
  p2: PChar;
begin
  p1 := PChar(Str);
  p2 := StrNew(p1);
  OemToChar(p1, p2);
  Result := StrPas(p2);
  StrDispose(p2);
end;
  1. Getting error 'Low Bound Exceeds High Bound' in following code.

function StrToRichText(const Str: string): string;
var
  i: integer;
begin
  Result := '';
  for i := 1 to Length(Str) do
  begin
    case Str[i] of
      #128 .. #255 :
        Result := Result + '\''' + LowerCase(IntToHex(Ord(Str[i]), 2));
      '\','{','}':
        Result := Result + '\' + Str[i];
    else
      Result := Result + Str[i];
    end;
  end;
end;
like image 424
Nalu Avatar asked Dec 16 '22 16:12

Nalu


1 Answers

Your OemToAnsi function should look like this:

function OemToAnsi(const Str: AnsiString): AnsiString;
begin
  SetLength(Result, Length(Str));
  OemToCharA(PAnsiChar(Str), PAnsiChar(Result));
end;

But perhaps you'd be better with

function OemToWide(const Str: AnsiString): string;
begin
  SetLength(Result, Length(Str));
  OemToChar(PAnsiChar(Str), PChar(Result));
end;

As for your StrToRichText, that looks more difficult. It clearly only accepts ANSI input. If you want to stick with ANSI then just change the declaration to

function StrToRichText(const Str: AnsiString): AnsiString;

RTF is encoded with 7 bit ASCII. To make that function work with Unicode input you'd need to escape any characters with ordinal >= 128. The escaping is described, for example, on the Wikipedia Rich Text Format page. I'll leave that as an exercise for you!


Before you go much further you need to read Marco Cantù's white paper: Delphi and Unicode.

like image 72
David Heffernan Avatar answered Dec 26 '22 10:12

David Heffernan