Forms Delphi

PROGRAM MainProgram;
INTERFACE
uses Windows, etc...;
IMPLEMENTATION
type
SimpleProc = procedure; stdcall;
DLLInitProc = procedure( AppHandle : HWND; CallBackProc : SimpleProc
); stdcall;
var
HandleList : TList;
DLLCallProc : SimpleProc;
procedure LoadDLL( name : string );
var
DLLHandle : THandle;
DLLInit : DLLInitProc;
begin
if FileExists( name ) then
begin
DLLHandle := LoadLibrary( name );
if DLLHandle <> 0 then
begin
// loaded, so get the procedure we call to pass info to it
@DLLInit := GetProcAddress( DLLHandle, 'ExportedDLLProc' );
if Assigned( DLLInit ) then
begin
// the DLL exported procedure is found, so lets call it
DLLInit( Application.Handle, DLLCallProc );
// now we save this DLLs handle to later FREE it
// when the program ends.
HandleList.Add( DLLHandle );
end;
end;
end;
end;
procedure LoadAllDLLs;
begin
// without coding it, somehow you will have to
// figure out how to find ALL the dlls path/filenames
// possibly in a .CFG file, the Registry, etc.
// loop, getting each path/filename string
LoadDLL( DLLName ); // DLLName = path/filename
end;
procedure TForm1.SomeMenuItemClickToOpenDLLWindow;
begin
DLLCallProc; // call the passed back DLL proc
end;
INITIALIZATION
HandleList := TList.Create;
FINALIZATION
HandleList.Free;
END.
--------------------------------------------------------------------------------
DLL CODE:
--------------------------------------------------------------------------------
LIBRARY DLLUnit;
INTERFACE
USES
DLLSource in 'DLLSource.pas';
EXPORTS
DLLInit name 'DLLInit';
IMPLEMENTATION
END.
UNIT DLLSource;
INTERFACE
uses Windows, etc..;
procedure DLLInit( AppHandle : HWND ); stdcall; export;
IMPLEMENTATION
USES
DLLFormSource;
var
OldAppHandle : THandle;
procedure HandleMenuClick;
begin
if Form1 = nil then
DLLForm := TDLLForm.Create( Application.Handle )
else
BringWindowToTop( Form1.Handle );
end;
procedure DLLInit;
begin
// first we assign the passed in Application handle
Application.Handle := AppHandle;
// now we assign the passed in CallBackProc to the address
// of the procedure we use to handle when the user clicks
// on a menu item activating this MDI form.
CallBackProc := @HandleMenuClick;
end;
INITIALIZATION
OldAppHandle := Application.Handle;
FINALIZATION
Application.Handle := OldAppHandle
END.
DLL Form source:
UNIT DLLFormSource;
INTERFACE
USES
Windows, etc..;
TDllForm = class( TForm );
procedure OnClose( TObject, Action, etc);
procedure OnDestroy();
end;
var
DLLForm : TDLLForm;
IMPLEMENTATION
procedure TDLLForm.OnClose;
begin
action := caFree;
end;
procedure TDLLForm.OnDestroy;
begin
DLLForm := nil;
end;
END.