首頁 > 編程 > Delphi > 正文

            delphi中一個值得大家來考慮的DLL問題

            2020-01-31 20:52:35
            字體:
            來源:轉載
            供稿:網友

            以下是我的原代碼:
            ==========================================================================================
            DPR  單元代碼
            program Mdiform;
            uses
              Forms,
              UMdiform in ´UMdiform.pas´ {Mainform},
              UDM in ´UDM.pas´ {GlobalDM: TDataModule},
              UFun in ´UFun.pas´;
            {$R *.res}
            begin
              Application.Initialize;
              Application.CreateForm(TMainform, Mainform);
              Application.Run;
            end.
            ===============================================================================================
            主窗體代碼:
            unit UMdiform;
            interface
            uses
              Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
              Dialogs, Udm,StdCtrls;
            type

              TMainform = class(TForm)
                Button1: TButton;
                procedure Button1Click(Sender: TObject);
                procedure FormCreate(Sender: TObject);
              private
                { Private declarations }
              public
                 MyHandle:HWND;
                { Public declarations }
              end;
            var
              Mainform: TMainform;
            type
              T_ShowTestMng=function (var adm:TMainform) : Boolean; StdCall;
            implementation
            {$R *.dfm}
            procedure TMainform.Button1Click(Sender: TObject);
            var 
              Lib_         :THandle;
              _ShowTestMng :T_ShowTestMng;
            begin
              Lib_:=LoadLibrary(pchar(´MdiChild.dll´));
              try
                @_ShowTestMng:=GetProcAddress(Lib_,´_ShowTestMng´);
                if not(@_ShowTestMng=nil) then
                  _ShowTestMng(Mainform);
              finally
                FreeLibrary(Lib_);
              end;
            end;
            procedure TMainform.FormCreate(Sender: TObject);
            begin
             MyHandle:=Application.Handle;
            end;
            end.
            ==============================================================================================
            子窗體DLL代碼:
            library MdiChild;
            uses
              ShareMem,
              UMdiform,  //此單元為父窗體單元,在頂目設置中我已經設置了搜索此單元在路徑。
              Forms,
              SysUtils,
              Classes,
              UChild in ´UChild.pas´ {FrmChild};//FrmChild子窗體的FormStyle屬性為FsMDIChild
            {$R *.res}
            function _ShowTestMng(var adm:TMainform) : Boolean; StdCall;
            begin
              result:=true;
              Application.Handle:=adm.MyHandle;
              Application.CreateForm(TFrmChild,FrmChild);   //程序就出錯在此:出錯原因是:Cannot create form. No MDI Forms are currently active.
              FrmChild.Show;
            end;
            exports
                _ShowTestMng;
            end.
            發表評論 共有條評論
            用戶名: 密碼:
            驗證碼: 匿名發表

            圖片精選