Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#16

Re: Komplettes Projekt in DLL auslagern

  Alt 3. Dez 2006, 14:08
Zitat von GuenterS:
Kannst Du mir ein Beispiel geben wie, so ein Wrapper aussehen könnte?

Die DLL wurde mit D7 Prof. erstellt und sollte dann in TDE verwendet werden...
Kein Problem.
Ich habe mich aber zurückgehalten, da ich keine Lust hatte mir was sinnvolles auszudenken.
Man nehme eine Form in D7, die Ein Edit, einen Button und eine ListBox hat.
Drücke ich den Button wird der Inhalt an die LB gehängt.
Das ganze könnte man so als Interface verpacken um diese Funktionalität anderen zur Verfügung zu stellen ohne dass sie etwas über TObject, AnsiStrings oder die VCL wissen müssten:
Delphi-Quellcode:
unit uDllFormInterfaces;

interface
uses
  Windows;

type
  IDllForm = interface(IUnknown)
  ['{CE9CD182-13A2-4DAE-B7AA-ED8A8100B8A4}']
    function get_Caption : WideString; stdcall;
    procedure set_Caption(const value : WideString); stdcall;

    function get_Item(aIndex : Integer) : WideString; stdcall;
    procedure set_Item(aIndex : Integer; const value : WideString); stdcall;

    function get_Handle : HWnd; stdcall;

    function AddLine(const line : WideString) : Integer; stdcall;

    procedure Show; stdcall;
    function ShowDialog : Integer; stdcall;
    procedure Hide; stdcall;


    property Caption : WideString
      read get_Caption
      write set_Caption;
    property Item[aIndex : Integer] : WideString
      read get_Item
      write set_Item;
    property Handle : HWnd
      read get_Handle;
  end;
Verpacken könnte man es so:
Delphi-Quellcode:
uses
  uDllFormInterfaces,
  uDllForm,
  Windows;

type
  TDllFormWrapper = class(TInterfacedObject, IDllForm)
  private
    fForm: TDllForm;
  protected
    function get_Caption: WideString; stdcall;
    function get_Item(aIndex: Integer): WideString; stdcall;
    procedure set_Caption(const value: WideString); stdcall;
    procedure set_Item(aIndex: Integer; const value: WideString); stdcall;
    property Form : TDllForm read fForm;
    function get_Handle: HWnd; stdcall;
  public

    function AddLine(const line: WideString): Integer; stdcall;

    property Caption : WideString
      read get_Caption
      write set_Caption;
    property Item[aIndex : Integer] : WideString
      read get_Item
      write set_Item;
    property Handle : HWnd
      read get_Handle;

    constructor Create;
    destructor Destroy; override;

    procedure Hide; stdcall;
    procedure Show; stdcall;
    function ShowDialog : Integer; stdcall;

  end;

  function CreateDllForm : IDllForm; stdcall;

implementation

uses
  Dialogs;

function CreateDllForm : IDllForm;
begin
  result := TDllFormWrapper.Create();
end;

{ TDllFormWrapper }

constructor TDllFormWrapper.Create;
begin
  inherited;
  fForm := TDllForm.Create(nil);
end;

destructor TDllFormWrapper.Destroy;
begin
  fForm.Free();
  ShowMessage('freed!'); // weil manche nicht an RefCounting glauben ;-)
  inherited;
end;

function TDllFormWrapper.AddLine(const line: WideString): Integer;
begin
  result := Form.ListBox1.Items.Add(line);
end;

function TDllFormWrapper.get_Caption: WideString;
begin
  result := Form.Caption;
end;

function TDllFormWrapper.get_Item(aIndex: Integer): WideString;
begin
  result := Form.ListBox1.Items[aIndex];
end;

procedure TDllFormWrapper.set_Caption(const value: WideString);
begin
  Form.Caption := value;
end;

procedure TDllFormWrapper.set_Item(aIndex: Integer;
  const value: WideString);
begin
  Form.ListBox1.Items[aIndex] := value;
end;

procedure TDllFormWrapper.Hide;
begin
  Form.Hide();
end;

procedure TDllFormWrapper.Show;
begin
  Form.Show();
end;

function TDllFormWrapper.ShowDialog : Integer;
begin
  result := Form.ShowModal();
end;

function TDllFormWrapper.get_Handle: HWnd;
begin
  result := Form.Handle;
end;
Ich habe eine Screenie angehängt, der zeigt dass es geht und beide Mini-Projekte.
Miniaturansicht angehängter Grafiken
screenie_146.png  
Angehängte Dateien
Dateityp: zip dllforms_137.zip (50,0 KB, 25x aufgerufen)
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat