Einzelnen Beitrag anzeigen

Benutzerbild von arcticwolf
arcticwolf

Registriert seit: 3. Aug 2021
Ort: Erfurt
41 Beiträge
 
Delphi 11 Alexandria
 
#12

AW: Windows Codepages - Kernel32.dll - EnumSystemCodePages

  Alt 27. Jun 2023, 15:19
Wie versprochen, hier nun mein Code wie ich ihn letzendlich verwende:
Delphi-Quellcode:
Type TWindowsCodepages = record
     strict private
       class threadvar FList: TStrings;
       class function EnumCodePagesProc(CodePage:PWideChar) : Boolean; static; stdcall;
       class function GetCodepageName(Codepage:Cardinal) : String; static;
     public
       class function GetInstalled(Var CodePageList:TStrings) : Boolean; static;
       class function GetSupported(Var CodePageList:TStrings) : Boolean; static;
       class function GetName(CodePage: Cardinal) : String; static;
     end;

class function TWindowsCodepages.GetCodepageName(Codepage:Cardinal) : String;
var CpInfoEx : TCPInfoEx;
begin
  Result := '';
  if IsValidCodePage(Codepage) then
  begin
    if (GetCPInfoEx(Codepage, 0, CpInfoEx)) then
    begin
      Result := CpInfoEx.CodePageName;
      Result := Result.Substring(7,Length(Result)-8);
    end;
  end;
end;

class function TWindowsCodepages.EnumCodePagesProc(CodePage:PWideChar) : Boolean;
Var CP : cardinal;
begin
  Result := False;
  if (CodePage<>Nil) then
  begin
    if (Length(Codepage)>0) then
    begin
      CP := StrToUIntDef(CodePage,0);
      if (CP>0) then
      begin
        FList.Add(Format('%.5d: %s', [CP, GetCodepageName(CP)]));
        Result := True;
      end;
    end;
  end;
end;

class function TWindowsCodepages.GetInstalled(Var CodePageList:TStrings) : Boolean;
begin
  Result := False;
  FList := TStringList.Create;
  try
    if (EnumSystemCodePagesW(@EnumCodePagesProc,CP_INSTALLED)) then
    begin
      CodePageList.AddStrings(FList);
      FList.Clear;
      Result := True;
    end;
  finally
    FList.Free;
    FList := Nil;
  end;
end;

class function TWindowsCodepages.GetSupported(Var CodePageList:TStrings) : Boolean;
begin
  Result := False;
  FList := TStringList.Create;
  try
    if (EnumSystemCodePagesW(@EnumCodePagesProc,CP_SUPPORTED)) then
    begin
      CodePageList.AddStrings(FList);
      FList.Clear;
      Result := True;
    end;
  finally
    FList.Free;
    FList := Nil;
  end;
end;

class function TWindowsCodepages.GetName(CodePage: Cardinal) : String;
begin
  Result := GetCodepageName(CodePage);
end;
Zu finden ist der Code auch in der Ply.SysUtils.pas auf GitHub:

https://github.com/playcom-de/Console

Ein Anwendungsbeispiel gibt es dort in der Demo08.
Wolfgang
  Mit Zitat antworten Zitat