Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi unicode porting : Incompatible types: 'Char' and 'AnsiChar' error with Win32 functions like CharToOEM?

I am trying to convert some old Delphi 7 code to Delphi 2010

function AnsiToDOS(S: String): String;
begin
  SetLength(Result, Length(S));
  if S <> '' then begin
     CharToOEM(PChar(S), PChar(Result));
  end;
end;

I get the "Incompatible types: 'Char' and 'AnsiChar' " error at the line:

CharToOEM (external User32 function) found in

Windows.pas unit

Can I rewrite this AnsiToDos function somehow, or do I need to write my own CharToOEM routine?

like image 353
JakeSays Avatar asked Mar 13 '12 18:03

JakeSays


People also ask

Is string incompatible with Char?

String [1] is incompatible with Char 4. DeleteFile (f) "incompatible types: String and PChar 5. Incompatible Types : String and PChar 6. Identical string declarations give incompatible types error 7. "Incompatible type String and Pchar"

What is the signature of chartooem in Delphi?

In Unicode Delphi, CharToOem maps to the Unicode version CharToOemW which has the following signature: function CharToOem(Source: PWideChar; Dest: PAnsiChar): BOOL; stdcall; So you need to supply an ANSI output buffer but your code provides a Unicode output buffer.

Is there any way to support Unicode in Delphi?

And of course there's the whole issue of Unicode. You are using a Unicode Delphi but being confined to ANSI. Do you really want to do that? If you want to support Unicode then you have to stop using Delphi sets, I am afraid. You'll need a data type that can support sets of wide characters.

What are the incompatible types in Delphi?

Delphi - E2010 Incompatible types: 'Integer' and 'Char' - Any ideas Click to share on: facebooktwitterdigggoogledelicioustechnoratistumbleuponmyspacewordpresslinkedingmailigooglewindows livetumblrviadeoyahoo buzzyahoo mailyahoo bookmarksfavoritesemailprint


1 Answers

In Unicode Delphi, CharToOem maps to the Unicode version CharToOemW which has the following signature:

function CharToOem(Source: PWideChar; Dest: PAnsiChar): BOOL; stdcall;

So you need to supply an ANSI output buffer but your code provides a Unicode output buffer.

The natural conversion is to switch to an AnsiString return value. At the same time renamed the function as StringToOem to better reflect what it does.

function StringToOem(const S: String): AnsiString;
begin
  SetLength(Result, Length(S));
  if S <> '' then begin
    CharToOem(PChar(S), PAnsiChar(Result));
  end;
end;

An alternative would be to convert to OEM in place, but for this you need to pass in an ANSI string and call the ANSI version of the API call explicitly.

function AnsiStringToOem(const S: AnsiString): AnsiString;
begin
  Result := S;
  UniqueString(Result);
  if S <> '' then begin
    CharToOemA(PAnsiChar(Result), PAnsiChar(Result));
  end;
end;

I do have to comment that I am surprised to see the OEM character set still being actively used in the modern day. I thought it had gone the way of the dinosaurs!

like image 131
David Heffernan Avatar answered Sep 18 '22 13:09

David Heffernan