解决方案 »

  1.   


    以下是Dll代码
    library PDllTest;uses
    // FastMM4,
       SysUtils,
       Forms,
       Messages,
       Windows,
       Classes,
       Graphics,
       UGlobdata in 'Sample_Delphi_DLL\UGlobdata.pas',
       uUnload in 'Sample_Delphi_DLL\uUnload.pas' {FrmUnload},
       Prodave60 in 'Sample_Delphi_DLL\Prodave60.pas',
       PubFuns in 'Sample_Delphi_DLL\PubFuns.pas',
       uAbout in 'Sample_Delphi_DLL\uAbout.pas' {FrmAbout},
       uAGinfo in 'Sample_Delphi_DLL\uAGinfo.pas' {FrmAGinfo},
       uBitRead in 'Sample_Delphi_DLL\uBitRead.pas' {FrmBitRead},
       uBitTest in 'Sample_Delphi_DLL\uBitTest.pas' {FrmBitTest},
       Ucontection in 'Sample_Delphi_DLL\Ucontection.pas' {FrmContection},
       uDBnumber in 'Sample_Delphi_DLL\uDBnumber.pas' {FrmDBCount},
       UDbRead in 'Sample_Delphi_DLL\UDbRead.pas' {FrmDBRead},
       uDBWrite in 'Sample_Delphi_DLL\uDBWrite.pas' {FrmDBWrite},
       uDiagBuf in 'Sample_Delphi_DLL\uDiagBuf.pas' {FrmDiagBuf},
       uFieldRead in 'Sample_Delphi_DLL\uFieldRead.pas' {FrmFieldRead},
       uFieldWrite in 'Sample_Delphi_DLL\uFieldWrite.pas' {FrmFieldWrite};{$R *.res}  
       
    var
       DLLApp: TApplication;
       DLLScreen: TScreen;
       TheForm: TForm;
       TheClass: TPersistentClass;
       DllHandle:Thandle;
    Procedure GetLoadConInfo(DllHWD:Thandle;var ConInfo:TTLoadCon);stdcall;
    begin
         ConInfo:=LoadCon;
         DllHandle:=DLLHWD;
    end;procedure RunTestDLL(DllHWD:Thandle;ADLLName, FormName, FormCaption: string;
                                  APP: TApplication; AScreen: TScreen) stdcall;
    begin
        Application := App;
        Screen := AScreen;
        RegisterClasses([TFrmUnload, TFrmAbout,TFrmAGinfo,TFrmBitRead,TFrmBitTest,
                            TFrmContection,TFrmDBCount,TFrmDBRead,TFrmDBWrite,
                            TFrmDiagBuf,TFrmFieldRead,TFrmFieldWrite]);
        TheClass := GetClass('T' + FormName); 
    if (TheClass <> nil) and TheClass.InheritsFrom(TForm) then begin
       TheForm := TForm(TheClass.Create).Create(nil);
       AppHWD :=TheForm;
       DllHandle:=DLLHWD;
       TheForm.Caption:=FormCaption;
       try
          TheForm.ShowModal;
       finally
          FreeAndNil(TheForm)
       end;
    end;
    end;procedure DLLUnloadProc(dwReason: Dword);
    begin
    if dwReason = DLL_PROCESS_DETACH then begin   //当进程退出时
        Application := DLLApp; //恢复
        Screen := DLLScreen;
        if FrmUnload<>nil then   FreeAndNil(FrmUnload);    // 卸载时释放资源
        if FrmAbout<>nil then   FreeAndNil(FrmAbout);
        if FrmAGinfo<>nil then   FreeAndNil(FrmAGinfo);
        if FrmBitRead<>nil then   FreeAndNil(FrmBitRead);
        if FrmBitTest<>nil then   FreeAndNil(FrmBitTest);
        if FrmContection<>nil then   FreeAndNil(FrmContection);
        if FrmDBCount<>nil then   FreeAndNil(FrmDBCount);
        if FrmDBRead<>nil then   FreeAndNil(FrmDBRead);
        if FrmDBWrite<>nil then   FreeAndNil(FrmDBWrite);
        if FrmDiagBuf<>nil then   FreeAndNil(FrmDiagBuf);
        if FrmFieldRead<>nil then   FreeAndNil(FrmFieldRead);
        if FrmFieldWrite<>nil then   FreeAndNil(FrmFieldWrite);
             SendMessage(Application.Handle, WM_CLOSE, 0, 0);// 关闭dll
        if DllHandle<>0 then
                        FreeLibrary(DllHandle);//释放dll 资源
    end;
    end;exports
       RunTestDLL,
       GetLoadConInfo;
    begin
       DLLApp := Application; //保存 DLL 中初始的 Application 对象
       DLLScreen := Screen;
       DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
    end.  
      

  2.   

    http://blog.csdn.net/simonhehe/article/details/8671375