Thema: Delphi DLL mit Form

Einzelnen Beitrag anzeigen

Capa

Registriert seit: 26. Okt 2003
102 Beiträge
 
RAD-Studio 2010 Arc
 
#13

AW: DLL mit Form

  Alt 1. Mär 2011, 15:13
mhh anderer Code selbes ergebnis kann die exe immernoch nur per taskmanager killen
bzw über die stop funktion in delphi.

Hier nochma der aktuelle Code nicht das sich doch noch nen anderer Fehler eingeschlichen hat den ich nicht
gesehen hab aber bei dem wenigen Code glaub ich das eher weniger.

DLL-File
Delphi-Quellcode:
library DLL1;

uses
  Sharemem,
  SysUtils,
  Classes,
  Forms,
  windows,
  DLLUnit1 in 'DLLUnit1.pas{Form2};

{$R *.res}

procedure CreateForm(appHandle: THandle); stdcall;
begin
  if appHandle = 0 then apphandle := GetActiveWindow;
  Application.Handle := appHandle;
  try
    if Form2=nil then
      Form2 := TForm2.Create(Application);
    Form2.Show();
  except
    On E: Exception Do Application.HandleException(E);
  end;
  Application.Handle := 0;
end;

procedure HideForm; stdcall;
begin
  if Assigned(Form2) then
  begin
    Form2.Hide();
// FreeAndNil(Form2);
  end;
end;

procedure Test(text1,text2,text3: string); stdcall;
begin
  Form2.Listbox1.Items.Add(text1);
  Form2.Listbox1.Items.Add(text2);
  Form2.Listbox1.Items.Add(text3);
end;

exports CreateForm,
          HideForm,
              Test;

begin
end.
DLL-Unit
Delphi-Quellcode:
unit DLLUnit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm2 = class(TForm)
    ListBox1: TListBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caHide;
end;

end.
Exe-Unit
Delphi-Quellcode:
unit Unit1;

interface

uses
  Sharemem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

procedure CreateForm2(appHandle: THandle); stdcall; external 'DLL1.dllname 'CreateForm';
procedure HideForm2; stdcall; external 'DLL1.dllname 'HideForm';
procedure Test(text1,text2,text3: string); stdcall; external 'DLL1.dllname 'Test';


{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateForm2(Application.Handle);
  Test('Need Help','Capa','DLL Form');
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := False;
  try
    HideForm2;
  finally
    CanClose := True;
  end;
end;

end.
P.S.: Selbst wenn ich die Exe nur starte und dann wieder beende hab ich das problem
also auch wenn ich die Form2 nicht initialisiere.

Geändert von Capa ( 1. Mär 2011 um 15:25 Uhr)
  Mit Zitat antworten Zitat