DLL.dpr
--------------------------------------------------------------
Procedure LoadChild(ParentApp: TApplication;ParentForm:TForm);export; stdcall;
begin
    Application:=ParentApp;
    Form1:=TForm1.Create(ParentForm);
    Form1.Show;
end;procedure DLLUnloadProc(Reason: Integer); register;
begin
    if Reason = DLL_PROCESS_DETACH then
      if Assigned(DllApplication) then
        Application:=DllApplication;
end;exports
LoadChild;
begin
    DllApplication:=Application;
    DLLProc := @DLLUnloadProc;
end.dll pas文件
-------------------------------------------
var
  Form1: TForm1;
  DllApplication:TApplication;
implementation{$R *.dfm}procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    Action:=CaFree;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
    Form1:=nil;
end;
主程序调用:
--------------------------------------------------
  
  T_ProvaChild=procedure(ParentApp:TApplication;ParentForm:TForm);stdcall;
  //动态调用使用
var
  MainFrm: TMainFrm;
  procedure LoadChild(ParentApp:TApplication;ParentForm:TForm);stdcall;external 'DLL.dll';
 //静态调用方法
implementation{$R *.dfm}uses About_C;procedure TMainFrm.N14Click(Sender: TObject);
begin
   //静态调用方法
    LoadChild(Application,Self);
end;procedure TMainFrm.N15Click(Sender: TObject);
var
DllHandle:THandle;
ProvaChild:T_ProvaChild;
begin
    DllHandle := LoadLibrary('DLL.dll');
    try
    if DLLHandle <> 0 then
    begin
      @ProvaChild:= GetProcAddress(DLLHandle,'LoadChild');
      //获取DLL方法地址
      if Assigned(@ProvaChild) then
      begin
           ProvaChild(Application,Self);
      end;
    end;
  finally
    if DLLHandle<>0 then
    FreeLibrary(DLLHandle);
    //释放DLL
  end;
end;----------------------------------------------1、如果依静态调用的话,完全正常,
2、采用动态调用的话,则感觉Application丢失了,调出来的窗体动不了。

解决方案 »

  1.   

    T_ProvaChild=procedure(ParentApp:TApplication;ParentForm:TForm) of object;stdcall;
      

  2.   

    动态调用要把主APPLICATION传进来
      

  3.   

    procedure DLLUnloadProc(Reason: Integer); register;
    begin
        if Reason = DLL_PROCESS_DETACH then
    begin
      if Assigned(Form1) then
        Form1.Free;
          if Assigned(DllApplication) then
            Application:=DllApplication;
    end;
    end;