Generic solution: RUN a Form inside a DLL

BdLm
BdLm used Ask the Experts™
on
there are many links in the net how to put a FORM side a DLL, I could get that stuff running
Below you will find that sample code I used to Export, the show event, the close, and some function to pass parameters  to that form,  for every form I export a have to add that code stuff ...

Q: Is there a generic solution to to write these functions and call these functions just with the Form as an datatype ?

procedure FormShowModal(parent: Pointer; aForm : TObject); stdcall;
begin
   aFormDLL :=  as aForm.create ;

   if Assigned(parent) then  aFormDLL as TObjectL.SetParent(parent);

   aFormDLL.Caption := aFormDLL.Caption + ' Modal';

   aFormDLL.ShowModal;
end;


procedure FormShowModal(parent: Pointer); stdcall;
begin
   TestFormDLL := TTestFormDLL.Create(nil);
   if Assigned(parent) then  TestFormDLL.SetParent(parent);
 
   TestFormDLL.Caption := TestFormDLL.Caption + ' Modal';
 
   TestFormDLL.ShowModal;
end;
 
 
 
function FormShowNormal(parent: Pointer): Pointer; stdcall;
begin
  TestFormDLL := TTestFormDLL.Create(nil);
  if Assigned(parent) then TestFormDLL.SetParent(parent);
 
  TestFormDLL.Caption := TestFormDLL.Caption + ' Normal';
  TestFormDLL.Show;
 
  result := TestFormDLL;
end;
 
function CloseForm : Boolean ; stdcall;
begin
 
   result := false;
 
    if assigned(TestFormDLL) then
 
         begin
           testFormDLL.CloseDatabaseButtonClick(nil);
           testFormDLL.Button2Click(nil);
           result := true;
         end;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi BdLm,

one way to do this is to create a unit that declare an interface that exposes the form's methods you want to call and use it in the DLL project and the DLL caller program (it's like a Type Library Definition).

It's not the more elegant solution, but works. Maybe if you create the form and implements the IDispatch interface it will be better.


***************************
        Mydll.dpr
***************************
library Mydll;
 
{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }
 
uses
  SysUtils,
  Classes,
  Mydll_Intf in 'Mydll_Intf.pas',
  uDLLForm in 'uDLLForm.pas' {DLLForm};
 
{$R *.res}
 
function CreateDLLForm(AOwner: TComponent): IDLLForm;
begin
  Result := TDLLForm.Create(AOwner);
end;
 
exports CreateDLLForm;
 
begin
end.
 
 
***************************
       uDLLForm.pas
***************************
 
unit uDLLForm;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Mydll_Intf, StdCtrls;
 
type
  TDLLForm = class(TForm, IDLLForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  DLLForm: TDLLForm;
 
implementation
 
{$R *.dfm}
 
procedure TDLLForm.Button1Click(Sender: TObject);
begin
  Caption := 'New Caption';
end;
 
end.
 
***************************
      Mydll_Intf.pas
***************************
unit Mydll_Intf;
 
interface
 
type
   IDLLForm = interface
     procedure Show;
     function ShowModal: Integer; 
   end;
 
implementation
 
end.
 
*****************
  Project1.dpr
*****************
program Project1;
 
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1};
 
{$R *.res}
 
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
 
***********************
       Unit1.pas
***********************
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, MyDLL_Intf;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
function CreateDLLForm(AOwner:TComponent): IDLLForm; external 'Mydll.dll';
 
end.

Open in new window

Geert GOracle dba
Top Expert 2009
Commented:
what you are looking for is a class of form type :
hmmm, i was gonna write a sample myself ... but then i googled
http://leonardorame.blogspot.com/2008/10/delphi-plugin-by-example.html

and a complete case study:
http://www.felix-colibri.com/papers/oop_components/delphi_virtual_constructor/delphi_virtual_constructor.html

Author

Commented:
@ both : thanks for the inputs, pls. give me a medium time (~3 weeks to check it now), it is not forgeotten, but I#m in a overload mode due to many other bugs to fix ...   :-(   in my project
 
Geert GOracle dba
Top Expert 2009

Commented:
looks like your in overload need of a holiday break for 3 weeks while somebody else solves the bugs in the project ...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial