//DLL
library Func;uses
  ShareMem,
  SysUtils,
  Classes,
  Forms,
  Windows,
  MConnect,
  Child2 in 'Child2.pas' {FormChild2};{$R *.RES}procedure CallModule();stdcall;export;
begin
    if not Assigned(FormChild2) then
        FormChild2 := TFormChild2.Create(Application);
    FormChild2.Show;
end;exports
   CallModule;begin
end;//MDIChild单元:procedure TFormChild2.N9Click(Sender: TObject);
begin
    Close;
end;procedure TFormChild2.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
    Action := caFree;
end;procedure TFormChild2.FormDestroy(Sender: TObject);
begin
    FormChild2 := nil;
end; 
 
//主程序:
type
  TCallModule =  procedure();stdcall;var
    FormMain: TFormMain;
    LibHandle: HModule;
    procedure LoadModule(AModuleName: String);implementation{$R *.DFM}
procedure LoadModule(AModuleName: String);
var
    CallModule: TCallModule;
begin
    LibHandle := LoadLibrary(PChar(AModuleName));
    if LibHandle = 0 then Exit;
        @CallModule := GetProcAddress(LibHandle,'CallModule');
    if @CallModule <> nil then
        CallModule();
end;procedure TFormMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var
    iCount: Integer;
begin
    if MessageDLG('是否退出?',mtConfirmation,[mbYes,mbNO],0) = mrYes then
    begin
        for iCount := 0 to MDIChildCount - 1 do
            MDIChildren[iCount].Close;       
        CanClose := True;
        if not (LibHandle = 0) then
            FreeLibrary(LibHandle);
    end else
        CanClose := False;
end;关键是无论DLL还是EXE都要带运行时间包VCL50.bpl编译 
 
这是一个调用DLL中MDICHILDFORM的例子,改成其他窗体类型,一样能用

解决方案 »

  1.   

    好象与普通的DLL没有什么区别啊。
    你可以在DLL中加入一个窗体,然后写一个过程用来创建这个窗体并显示它,
    然后在就可以调用DLL中的这个过程了,它会显示DLL中的窗体了。
      

  2.   

    其实在delphi 4 的例子中就有的,不过到了5以后就去掉了,如果你需要相应的例子,可以给我发mail:[email protected]
      

  3.   


    《delphi5开发人员指南》中的例子:// Dll library CalendarLib;uses
      ShareMem,
      SysUtils,
      Classes,
      DLLFrm in 'DLLFrm.pas' {DLLForm};exports
      ShowCalendar;
      
    begin
    end.// Dll 中的窗体单元,有一TCalendar控件unit DLLFrm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, Grids, Calendar;type  TDLLForm = class(TForm)
        calDllCalendar: TCalendar;
        procedure calDllCalendarDblClick(Sender: TObject);
      end;{ Declare the export function }
    function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;implementation
    {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
    var
      DLLForm: TDllForm;
    begin
      // Copy application handle to DLL's TApplication object
      Application.Handle := AHandle;
      DLLForm := TDLLForm.Create(Application); 
      try
        DLLForm.Caption := ACaption;
        DLLForm.ShowModal;
        Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
      finally
        DLLForm.Free;
      end;
    end;procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
    begin
      Close;
    end;end.
    // Projectprogram CalendarTest;uses
      Forms,
      MainFfm in 'MainFfm.pas' {MainForm};{$R *.RES}begin
      Application.CreateForm(TMainForm, MainForm);
      Application.Run;
    end.
    // Project 中的单元unit MainFfm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, StdCtrls;type
      { First, define a procedural data type, this should reflect the
        procedure that is exported from the DLL. }
      TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;  { Create a new exception class to reflect a failed DLL load }
      EDLLLoadError = class(Exception);  TMainForm = class(TForm)
        lblDate: TLabel;
        btnGetCalendar: TButton;
        procedure btnGetCalendarClick(Sender: TObject);
      end;var
      MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.btnGetCalendarClick(Sender: TObject);
    var
      LibHandle   : THandle;
      ShowCalendar: TShowCalendar;
    begin  { Attempt to load the DLL }
      LibHandle := LoadLibrary('CALENDARLIB.DLL');
      try
        { If the load failed, LibHandle will be zero.
          If this occurs, raise an exception. }
        if LibHandle = 0 then
          raise EDLLLoadError.Create('Unable to Load DLL');
        { If the code makes it here, the DLL loaded successfully, now obtain
          the link to the DLL's exported function so that it can be called. }
        @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
        { If the function is imported successfully, then set lblDate.Caption to reflect
          the returned date from the function. Otherwise, show the return raise
          an exception. }
        if not (@ShowCalendar = nil) then
          lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
        else
          RaiseLastWin32Error;                                                               
      finally
        FreeLibrary(LibHandle); // Unload the DLL.
      end;
    end;end.
      

  4.   

    DDG的例子
    //dll
    library CalendarMLLib;uses
      ShareMem,
      SysUtils,
      Classes,
      DLLFrm in 'DLLFrm.pas' {DLLForm};exports
      ShowCalendar,
      CloseCalendar;
      
    begin
    end.unit DLLFrm;interfaceuses
      SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
      Forms, Dialogs, Grids, Calendar;type  TDLLForm = class(TForm)
        calDllCalendar: TCalendar;
      end;{ Declare the export function }
    function ShowCalendar(AHandle: THandle; ACaption: String): Longint; stdCall;
    procedure CloseCalendar(AFormRef: Longint); stdcall;
    implementation
    {$R *.DFM}function ShowCalendar(AHandle: THandle; ACaption: String): Longint;
    var
      DLLForm: TDllForm;
    begin
      // Copy application handle to DLL's TApplication object
      Application.Handle := AHandle;
      DLLForm := TDLLForm.Create(Application);
      Result := Longint(DLLForm);
      DLLForm.Caption := ACaption;
      DLLForm.Show;  
    end;procedure CloseCalendar(AFormRef: Longint);
    begin
      if AFormRef > 0 then
        TDLLForm(AFormRef).Release;
    end;end.
    //调用function ShowCalendar(AHandle: THandle; ACaption: String): Longint; StdCall;
      external 'CALENDARMLLIB.DLL';procedure CloseCalendar(AFormRef: Longint); stdcall;
      external 'CALENDARMLLIB.DLL';implementation{$R *.DFM}procedure TMainForm.btnShowCalendarClick(Sender: TObject);
    begin
      if not Assigned(FFormRef) then
        FFormRef := TForm(ShowCalendar(Application.Handle, Caption));
    end;procedure TMainForm.btnCloseCalendarClick(Sender: TObject);
    begin
      if Assigned(FFormRef) then
      begin
        CloseCalendar(Longint(FFormRef));
        FFormRef := nil;
      end;
    end;procedure TMainForm.FormCreate(Sender: TObject);
    begin
      FFormRef := nil; // Initialize the FFormRef field to nil. 
    end;
    end.
      

  5.   

    来个最简单的//dlllibrary about;uses
      aboutunit in 'aboutunit.pas' {aboutform};
      
    exports
      showform;beginend.
    //formunit aboutunit;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
      Taboutform = class(TForm)
      Label1: TLabel;
      Label2: TLabel;
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      aboutform: Taboutform;function showform:boolean;stdcall;implementation{$R *.DFM}function showform(title:string;version:string):boolean;
    var
      aboutform:Taboutform;
    begin
      result:=false;
      aboutform:=taboutform.Create(application);
      try
        with aboutform do
        begin
          aboutform.Label1.caption:=title;
          aboutform.llabel2.caption:=version;
          aboutform.showmodal;
          result:=true;
        end;
      finally
        aboutform.free;
      end;
    end;end.然后在别的程序中调用showform就可以了