Unicode Delphi CharToOem
displays a version of Unicode CharToOemW
, which has the following signature:
function CharToOem(Source: PWideChar; Dest: PAnsiChar): BOOL; stdcall;
So you need to provide an ANSI output buffer, but your code provides a Unicode output buffer.
A natural conversion is to switch to the return value of AnsiString
. At the same time, the StringToOem
function has been renamed 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 the ANSI string and explicitly call the ANSI version of the API call.
function AnsiStringToOem(const S: AnsiString): AnsiString; begin Result := S; UniqueString(Result); if S <> '' then begin CharToOemA(PAnsiChar(Result), PAnsiChar(Result)); end; end;
I need to comment that I am surprised to see that the OEM character set is still heavily used today. I thought it went the way of the dinosaurs!
source share