Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#8

AW: Bezeichnungen von SpecialFoldern?

  Alt 23. Nov 2013, 15:57
Und die Abstraktion selbst müsste niemals mehr geändert werden, auch nicht, wenn Microsoft in Windows 10/11/12 alle bisherigen Funktionen als deprecated kennzeichnet und man stattdessen plötzlich SHGetVerySpecialFoldersWhichDidPossiblyNotExistInO lderWindows verwenden soll?
Du hast den Sinn der Abstraktion beschrieben

Hier mal ein Beispiel
Delphi-Quellcode:
unit AppPath;

interface

  type
    IAppPath = interface
      ['{00EF3178-EB41-49A3-9385-B3CB61CD0012}']
      function DocumentRoot : string;
    end;

    TAppPath = class
    private
      class var FAppPath : IAppPath;
    public
      class procedure SetAppPathInstance( AAppPath : IAppPath );
      class function DocumentRoot : string;
    end;

implementation

  uses
{$IFDEF WIN32}WinapiAppPath{$ENDIF}
{$IFDEF WIN64}WinapiAppPath{$ENDIF}
{$IFDEF OSX32}MacapiAppPath{$ENDIF}
;
  { TAppPath }

  class function TAppPath.DocumentRoot : string;
    begin
      Result := FAppPath.DocumentRoot;
    end;

  class procedure TAppPath.SetAppPathInstance( AAppPath : IAppPath );
    begin
      FAppPath := AAppPath;
    end;

end.
Delphi-Quellcode:
unit WinapiAppPath;

interface

  uses
    AppPath;

implementation

  uses
    SysUtils,
    WinapiAppPathSpecialFolder,
    WinapiAppPathKnownFolder;

  function WinVersionMinVista : Boolean;
    begin
      Result := CheckWin32Version( 6, 0 );
    end;

initialization

  if WinVersionMinVista
  then
    TAppPath.SetAppPathInstance( TAppPathKnownFolder.Create )
  else
    TAppPath.SetAppPathInstance( TAppPathSpecialFolder.Create );

end.
Delphi-Quellcode:
unit WinapiAppPathSpecialFolder;

interface

  uses
    AppPath;

  type
    TAppPathSpecialFolder = class( TInterfacedObject, IAppPath )
    public
      function DocumentRoot : string;
    end;

implementation

  uses
    Windows,
    ShellApi,
    ShlObj;

  function GetSpecialFolder( FolderID : longint ) : string;
    var
      Path : pchar;
      idList : PItemIDList;
    begin
      GetMem( Path, MAX_PATH );
      SHGetSpecialFolderLocation( 0, FolderID, idList );
      SHGetPathFromIDList( idList, Path );
      Result := string( Path );
      FreeMem( Path );
    end;

  { TAppPathSpecialFolder }

  function TAppPathSpecialFolder.DocumentRoot : string;
    begin
      Result := GetSpecialFolder( CSIDL_MYDOCUMENTS );
    end;

end.
Delphi-Quellcode:
unit WinapiAppPathKnownFolder;

interface

  uses
    AppPath;

  type
    TAppPathKnownFolder = class( TInterfacedObject, IAppPath )
    public
      function DocumentRoot : string;
    end;

implementation

  uses
    SysUtils,
    Windows,
    ShlObj;

  function GetKnownFolder( const FolderID : TGUID ) : string;
    var
      LPath : PChar;
    begin
      if SHGetKnownFolderPath( FolderID, 0, 0, LPath ) >= 0
      then
        Result := LPath
      else
        Result := #0;
    end;

  { TAppPathKnownFolder }

  function TAppPathKnownFolder.DocumentRoot : string;
    begin
      Result := GetKnownFolder( StringToGUID( '{FDD39AD0-238F-46AF-ADB4-6C85480369C7}' ) );
    end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)

Geändert von Sir Rufo (23. Nov 2013 um 16:03 Uhr)
  Mit Zitat antworten Zitat