Einzelnen Beitrag anzeigen

Benutzerbild von olee
olee

Registriert seit: 16. Feb 2008
Ort: Boppard
540 Beiträge
 
Turbo Delphi für Win32
 
#16

AW: VPHD - Virtual private hard-disc

  Alt 4. Jan 2012, 17:00
Nun ja...
eigentlich war das ganze nicht so schwer wie erwartet ^^

Der Trick war folgender:
Das Projekt von VPHD besteht im Grunde aus einer einzigen Hauptklasse.
Diese habe ich nun so abgeändert, dass ich dazu ein Interface (IVphd) mit allen public Methoden anlegen konnte. Dazu musste ich im Grunde nur die ganze Klasse kopieren, interface drüber schreiben, alle nicht-public felder rauslöschen und für ein paar public-variablen noch getter und setter anlegen.

Damit das ganze nun auch als DLL nutzbar ist habe ich dann noch ein paar kleine Methoden geschrieben, welche exportiert werden. So z.B. zum öffnen / erstellen eines Archives:
Delphi-Quellcode:
function OpenVphdFile(const FileName, Password: PWideChar; CreateNew: Boolean): IVphd; stdcall;
begin
  Result := TVPHD.Create(FileName, Password, CreateNew);
end;
Diese Funktion gibt dann am ende der Anwendung ein IVphd-Interface zurück, welches sich im Grunde fast exakt wie die normale Klasse verwänden lässt.
Zuletzt war es noch nötig ale WideStrings in den Methoden des Interfaces durch PWideChar (mit daraus folgenden Code-Anpassungen) zu ersetzen und natürlich an alle Methoden stdcall dranhängen.
Das wars dann auch schon im Großen und Ganzen.

Hier mal als Beispiel das ganze Interface:
Delphi-Quellcode:
  IVPHD = interface (IInterface)
    ['{3D6C8AAE-0CED-4D71-A1E2-26D2CCD0E8A5}']
  {$REGION 'File management'}
    procedure BeginUpdate; stdcall;
    procedure EndUpdate; stdcall;
    function GetDirectory(APath: PWideChar; DoCreate: Boolean): PDirNode; stdcall;
    function GetDirectoryFile(APath: PWideChar; var FileID: Integer): PDirNode; stdcall;
    function GetFile(AFileName: PWideChar): Integer; overload; stdcall;
    function GetFile(Dir: PDirNode; AName: PWideChar): Integer; overload; stdcall;
    //==============================================================================
    procedure AddFile(const AFileName: PWideChar; Data: TStream;
      FileDate: TDateTime); overload; stdcall;
    procedure AddFile(ARoot, AFileName: PWideChar); overload; stdcall;
    //==============================================================================
    procedure ExtractDir(Dir: PDirNode; DstPath: PWideChar); stdcall;
    procedure ExtractAll(DstPath: PWideChar); stdcall;
    procedure ExtractFile(FileID : Integer; const DstFile: PWideChar); overload; stdcall;
    procedure ExtractFile(FileID : Integer; DstStream: TStream); overload; stdcall;
    //==============================================================================
    procedure ExtractFileStart(FileID: Integer); stdcall;
    function ExtractFilePart(Dst: TStream; Count: Integer): Integer; stdcall;
    function ExtractFilePartBuff(var Data; Count: Integer): Integer; stdcall;
    procedure ExtractFileEnd; stdcall;
    //==============================================================================
    procedure DeleteFile(FileID: Integer); stdcall;
    procedure DeleteDir(Dir: PDirNode); stdcall;
    procedure RenameFile(FileID : Integer; ANewName: PWideChar); stdcall;
    procedure RenameDir(Dir: PDirNode; ANewName: PWideChar); stdcall;
  {$ENDREGION}
    procedure DrawFragmentationBar(DstDC: HDC; Width, Height: Integer;
      DrawStyle : TFragBarDrawStyle); stdcall;

    procedure SaveAs(const AFileName: PWideChar); overload; stdcall;
    procedure SaveAs(AStream: TStream); overload; stdcall;
    procedure Defragment; stdcall;
    procedure Finish; stdcall;

    function GetFileCount: Integer; stdcall;
    function GetFile(Index: Integer): PVphdFileInfo; overload; stdcall;
    property FileList[Index: Integer]: PVphdFileInfo read GetFile;

    function GetUpdating: Boolean; stdcall;
    function GetProgressFile: PWideChar; stdcall;
    procedure SetProgressFile(v: PWideChar); stdcall;
    function GetOwnStream: Boolean; stdcall;
    procedure SetOwnStream(v: Boolean); stdcall;
    function GetEOF: Int64; stdcall;
    function GetDescription: PWideChar; stdcall;
    procedure SetDescription(v: PWideChar); stdcall;
    function GetFileName: PWideChar; stdcall;
    function GetOnProgress: TVphdProgressEvent; stdcall;
    procedure SetOnProgress(v: TVphdProgressEvent); stdcall;

    property Updating: Boolean read GetUpdating;
    property ProgressFile: PWideChar read GetProgressFile write SetProgressFile;
    property OwnStream: Boolean read GetOwnStream write SetOwnStream;
    property Description: PWideChar read GetDescription write SetDescription;
    property FileName: PWideChar read GetFileName;
    property EOF: Int64 read GetEOF;
    property OnProgress: TVphdProgressEvent read GetOnProgress write SetOnProgress;
  end;
Wenn man sich von der Hauptanwendung (VPHD_Tool.exe) mal die Source anschaut sieht man auch, dass der Unterschied zwischen der Nutzung über das Interface und als normale Klasse wirklich minimal ist (lässt sich nun durch einen kleinen Compilerschalter umstellen).

MFG
Björn Zeutzheim
Björn Zeutzheim
Codename: Performancepumpe

Geändert von olee ( 4. Jan 2012 um 17:03 Uhr)
  Mit Zitat antworten Zitat