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?
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"
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.
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.
Delphi - E2010 Incompatible types: 'Integer' and 'Char' - Any ideas Click to share on: facebooktwitterdigggoogledelicioustechnoratistumbleuponmyspacewordpresslinkedingmailigooglewindows livetumblrviadeoyahoo buzzyahoo mailyahoo bookmarksfavoritesemailprint
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With