library TestDll;
uses
  ShareMem,
  SysUtils,
  Classes,
  Forms,
  windows,
  Unit1d in 'Unit1d.pas' {Form1};
var
  DLLApp: TApplication;
{$R *.res}
function ShowDllForm1(AHandle:TApplication):integer; stdcall;
var
  Form1: TForm1;
begin
  Application:=AHandle;
  Form1:=TForm1.Create(AHandle);
  Form1.Show;
  Result:=Form1.SpinEdit1.Value;
end;
procedure DLLUnloadProc(Reason : DWord);
begin
  if Reason = DLL_PROCESS_DETACH then Application := DLLApp;//恢复
end;Exports
  ShowDllForm  index 1,
  ShowDllForm1 index 2;
begin
  DLLApp := Application;  DLLProc := @DLLUnloadProc; //这句怎么起不了作用一样的?  ???(*****)  
end.调用: TestDll.Dll文件
type
  Ta1Form = procedure(AForm:TApplication); stdcall;procedure TFormmain.Button4Click(Sender: TObject);
var AForm:  Ta1Form;
  lHandle: THandle;
begin
  lHandle := LoadLibrary('TestDll.dll');
  try
    if lhandle = 0 then
      raise Exception.Create('不能引导动态链接库...');
    @AForm := GetProcAddress(lHandle, 'ShowDllForm1');
    if @AForm <> nil then
      AForm(Application)
    else
      RaiseLastWin32Error;
  finally
    // FreeLibrary(lHandle); //这句一用上,就会主Form不见了。而用了,被调用的子Form又会出现主程序没有完全退出。(在系统里有进程)???(*****)
  end; 
end;谢谢

解决方案 »

  1.   

    library TestDll;
    uses
      ShareMem,
      SysUtils,
      Classes,
      Forms,
      windows,
      Unit1d in 'Unit1d.pas' {Form1};
    var
      DLLApp: TApplication;
    {$R *.res}
    function ShowDllForm1(AHandle:TApplication):integer; stdcall;
    var
      Form1: TForm1;
    begin
      Application:=AHandle;
      Form1:=TForm1.Create(AHandle);
      Form1.Show;
      Result:=Form1.SpinEdit1.Value;
    end;
    procedure DLLUnloadProc(Reason : DWord);
    begin
      if Reason = DLL_PROCESS_DETACH then Application := DLLApp;//恢复
    end;Exports
      ShowDllForm  index 1,
      ShowDllForm1 index 2;
    begin
      DLLApp := Application;  DLLProc := @DLLUnloadProc; //这句怎么起不了作用一样的?  ???(*****)  
    end.调用: TestDll.Dll文件
    type
      Ta1Form = procedure(AForm:TApplication); stdcall;procedure TFormmain.Button4Click(Sender: TObject);
    var AForm:  Ta1Form;
      lHandle: THandle;
    begin
      lHandle := LoadLibrary('TestDll.dll');
      try
        if lhandle = 0 then
          raise Exception.Create('不能引导动态链接库...');
        @AForm := GetProcAddress(lHandle, 'ShowDllForm1');
        if @AForm <> nil then
          AForm(Application)
        else
          RaiseLastWin32Error;
      finally
        // FreeLibrary(lHandle); //这句一用上,就会主Form不见了。而用了,被调用的子Form又会出现主程序没有完全退出。(在系统里有进程)???(*****)
      end; 
    end;
      

  2.   

    MDI DLL 还没有用过动态LOAD的,
    我的程序都是静态调用的。//以下是MDI 的主窗口,(就是DELPHI MDI 模板建出来的)
    //
    unit Main;interfaceuses Windows, SysUtils, Classes, Graphics, Forms, Controls, Menus,
      StdCtrls, Dialogs, Buttons, Messages, ExtCtrls, ComCtrls, StdActns,
      ActnList, ToolWin, ImgList;type
      TMainForm = class(TForm)
        MainMenu1: TMainMenu;
        File1: TMenuItem;
        FileNewItem: TMenuItem;
        FileOpenItem: TMenuItem;
        FileCloseItem: TMenuItem;
        Window1: TMenuItem;
        Help1: TMenuItem;
        N1: TMenuItem;
        FileExitItem: TMenuItem;
        WindowCascadeItem: TMenuItem;
        WindowTileItem: TMenuItem;
        WindowArrangeItem: TMenuItem;
        HelpAboutItem: TMenuItem;
        OpenDialog: TOpenDialog;
        FileSaveItem: TMenuItem;
        FileSaveAsItem: TMenuItem;
        Edit1: TMenuItem;
        CutItem: TMenuItem;
        CopyItem: TMenuItem;
        PasteItem: TMenuItem;
        WindowMinimizeItem: TMenuItem;
        StatusBar: TStatusBar;
        ActionList1: TActionList;
        EditCut1: TEditCut;
        EditCopy1: TEditCopy;
        EditPaste1: TEditPaste;
        FileNew1: TAction;
        FileSave1: TAction;
        FileExit1: TAction;
        FileOpen1: TAction;
        FileSaveAs1: TAction;
        WindowCascade1: TWindowCascade;
        WindowTileHorizontal1: TWindowTileHorizontal;
        WindowArrangeAll1: TWindowArrange;
        WindowMinimizeAll1: TWindowMinimizeAll;
        HelpAbout1: TAction;
        FileClose1: TWindowClose;
        WindowTileVertical1: TWindowTileVertical;
        WindowTileItem2: TMenuItem;
        ToolBar2: TToolBar;
        ToolButton1: TToolButton;
        ToolButton2: TToolButton;
        ToolButton3: TToolButton;
        ToolButton4: TToolButton;
        ToolButton5: TToolButton;
        ToolButton6: TToolButton;
        ToolButton9: TToolButton;
        ToolButton7: TToolButton;
        ToolButton8: TToolButton;
        ToolButton10: TToolButton;
        ToolButton11: TToolButton;
        ImageList1: TImageList;
        procedure FileNew1Execute(Sender: TObject);
        procedure FileOpen1Execute(Sender: TObject);
        procedure HelpAbout1Execute(Sender: TObject);
        procedure FileExit1Execute(Sender: TObject);
      private
        { Private declarations }
        procedure CreateMDIChild(const Name: string);
      public
        { Public declarations }
      end;var
      MainForm: TMainForm;//这儿加
    procedure CreateChildForm(MainApp:TApplication);stdcall; external 'ChildDll.dll';implementation{$R *.DFM}uses About;procedure TMainForm.CreateMDIChild(const Name: string);
    begin
      { create a new MDI child window }
    //这儿加
      CreateChildForm(Application);
    end;procedure TMainForm.FileNew1Execute(Sender: TObject);
    begin
      CreateMDIChild('NONAME' + IntToStr(MDIChildCount + 1));
    end;procedure TMainForm.FileOpen1Execute(Sender: TObject);
    begin
      if OpenDialog.Execute then
        CreateMDIChild(OpenDialog.FileName);
    end;procedure TMainForm.HelpAbout1Execute(Sender: TObject);
    begin
      AboutBox.ShowModal;
    end;procedure TMainForm.FileExit1Execute(Sender: TObject);
    begin
      Close;
    end;end.以下为CHILDDLL.DLL 的CHILDDLL.DPR文件的内容:
    library ChildDll;{ 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
      Windows,
      SysUtils,
      Classes,
      Forms,
      Child in 'Child.pas' {Form1};var
      DllApp : TApplication;
    {$R *.RES}procedure MyDLLProc(Reason: Integer);
    begin
    //  DLL is unloading. Restore the Application pointer.
      if Reason = DLL_PROCESS_DETACH then
      begin
         if Assigned(DllApp) then
            Application := DllApp;
      end;
    end;procedure CreateChildForm(MainApp:TApplication);stdcall;export;
    var
      Child : TForm1;
    begin
      if not Assigned(DllApp) then
      begin
         DllApp := Application;
         Application := MainApp;
      end;  Child := nil;
      try
        Child := TForm1.Create(Application);
        Child.Show();
      except
        Child.Free();
      end;
    end;exports
      CreateChildForm;begin
      DLLProc := @MyDLLProc;
    end.
    //如果你的建不出来请给Mail,
    //我将给Nizvoo的例子给你。
      

  3.   

    [email protected]但我还想问:
    在testdll.dll文件中:
    procedure DLLUnloadProc(Reason : DWord);
    begin
      if Reason = DLL_PROCESS_DETACH then Application := DLLApp;//恢复
    end;Exports
      ShowDllForm  index 1,
      ShowDllForm1 index 2;
    begin
      DLLApp := Application;  DLLProc := @DLLUnloadProc; //这句怎么起不了作用一样的?  ???(*****)  
    end.
      

  4.   

    因为我的FreeLibrary(lHandle)会出错; //这句一用上,就会主Form不见了。而用了,被调用的子Form又会出现主程序没有完全退出。(在系统里有进程)???(*****)
      
      

  5.   

    将你的程序打包发给我
    [email protected]
      

  6.   

    发了,收到了没有,改完了给我看看:) thank
      

  7.   

    你发的者是什么啊!六个邮件都是这样的!没有一个有附件!Gx4JHFzd6GHQ1XFo7BGteqRe4yed3wTgM6/+RVEqRkWH4SlnJ/XdiWKxjznb74r7mG/6afsYP7I+
    GQn0hrOcMZj1uibAzd54vbaveb6fuq9x798s7EjApOiJMp/cjwGkomv5tZFdUFVhOiotHpjI6byz
    tyYhPelMxov+CaywRewdEthcqjiBuP6Eqox+4FiT1k/OGEYhw6oyhlMILNRe2+akg7vxXiVb3Usc
    DMDIJh3km6FNJv3rt1ApB2OsHTv7rXi/UPG2KJPjUe5aTcX2p51wG+32fZ35JmP2QLwjFHjrbhPO
    T5fJu3zTpF09iduZyhVRAYXfDSMVVM580p47OUZtbvKteFbsCvO4WqHoWvbvS+Iixya7bB1sxzeF
    0DZtIt3vaOPBCVUppThBMslURg37KEJnJndgU0o/YrDZ4LcEe9CJuNF8aCMbDk1kD8f7Vckw2r/n
    9x/PIMdoxfNFux3/ZLuri9mvzwldudqUmyUWdZbkxOIsoUEVUzX0JZp1nEuvcqERvVJIKlXLZqbg
    jiUhB7hA77J4o+iZWdAP6fEobnsZiJzXOpg9BvVki4bl0FjVsF/DER+zZwfub5wmGoh7mga5LKGq
    54y+fDv37nFUDmOFoptba20XJG59v79uX2yzi30N93a14+H8BQNysjxkLeCivair6l0O3mlRr9XL
    +9ytppVRUTtybozaMSEh6rWPk/ctE+Kz1dXO4V7bj1odV0EeOowlXxc7IiDGnoNfINVRSOXrCxBv
      

  8.   

    ah,不会吧,是.zip文件,OutLook分割发送的,怕网速慢,你收不到:)
    后来又重新发过一次,是一个.zip文件,这次没有分块发送,整个.zip文件发。
    sorry.
      

  9.   

    收到了,看了。thank1、我怎么找不到: project->Options...->Packages->下的
    "Build with runtimes packages" ,没有这个选项?Delphi5和Delphi6都没有?2、静态链接不太好,因为我做的是ERP系统,工程太大,用"静态链接"实在不是很好。
    有没有看过[天心ERP系统](fas2000),是用Delphi做的,MDI,动态调用DLL.thank
      

  10.   

    ah,我找到了Build with runtimes packages选项了。真不好意思,我一直没用这个选项。
    请问:这个选项选上有什么作用?(对一般工程而言),thank