调用DLL中的窗体

    技术2022-05-11  66

      (1)DLL源码   library ProjectDll;

      uses     Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,     UnitDll in 'UnitDll.pas' {Form1};//DLL中的窗体

      procedure ProvaChild(ParentApplication: TApplication; ParentForm: TForm); export; stdcall;   var     Form1: TForm1;      DllProc: Pointer;             { Called whenever DLL entry point is called }   begin      Application:=ParentApplication;

         Form1:=TForm1.Create(ParentForm);      Form1.MyParentForm:=ParentForm;      Form1.MyParentApplication:=ParentApplication;   //   windows.SetParent(Form1.Handle,ParentForm.Handle);   //   Form1.FormStyle:=fsMDIChild;      Form1.Show;   end;

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

      exports      ProvaChild;

      begin      DllApplication:=Application;      DLLProc := @DLLUnloadProc;   end.     =============================================     unit UnitDll;

      interface   uses     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

      type     TForm1 = class(TForm)       procedure FormClose(Sender: TObject; var Action: TCloseAction);       procedure FormDestroy(Sender: TObject);     private

        public       MyParentForm: TForm;       MyParentApplication: TApplication;     end;

      var      DllApplication: TApplication;

      implementation

      {$R *.DFM}

      procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);   begin      Action:=caFree;   end;

      procedure TForm1.FormDestroy(Sender: TObject);   begin   //   Application:=DllApplication;   end;

      end.   (2)应用程序源码   T_ProvaChild = procedure (ParentApplication: TApplication; ParentForm: TForm); stdcall;

      var     MainForm: TMainForm;

      implementation

      {$R *.DFM}

      procedure TMainForm.StartClick(Sender: TObject);   var      DllHandle: THandle;      ProcAddr: FarProc;      ProvaChild: T_ProvaChild;   begin      DllHandle := LoadLibrary('ProjectDll');      ProcAddr := GetProcAddress(DllHandle, 'ProvaChild');      if ProcAddr <> nil then      begin         ProvaChild := ProcAddr;         ProvaChild(Application,Self);      end;   end;

      end. 


    最新回复(0)