同上

解决方案 »

  1.   

    这是源码unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls;
    type
      TFrameClass = class of TFrame;
      //注意   必须是class of TFrame而不是 class(TFrame)
      //class of TFrame  声明的是类类型   f:TFrameClass是一个类
      //class(TFrame) 是声明一个类, 这个类继承了TFrame类 f: TFrame; 是一个对象
     TGetDllFrameClass = function (App:THandle;frm:THandle):TFrameClass;stdcall;
     TGetDllFormClass = function (App:THandle;frm:THandle):TFormClass;stdcall;
    type
      TForm1 = class(TForm)
        btn1: TButton;
        pnl1: TPanel;
        btn2: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
      private
           DLLHandle: THandle;
          GetDllFrameClass: TGetDllFrameClass;
          GetDllFormClass: TGetDllFormClass;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation
     var
     TMP:TFrameClass;
     tmp2:TFrame;
      TMP3:TFormClass;
     tmp4:TForm;
    {$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
    begin
    DLLHandle := LoadLibrary('DLL.dll');
         try
          @GetDllFrameClass := GetProcAddress(DLLHandle, 'GetDllFrameClass');
          if @GetDllFrameClass = nil then
            Abort;
            tmp:=GetDllFrameClass(application.Handle,Self.Handle);
           tmp2:=TMP.Create(self);
           tmp2.Parent:=pnl1;
          tmp2.Show;
       except
             raise;
        end;
    end;   //这是调用dll里的frame类procedure TForm1.btn2Click(Sender: TObject);
    begin
    DLLHandle := LoadLibrary('DLL.dll');
         try
          @GetDllFormClass := GetProcAddress(DLLHandle, 'GetDllFormClass');
          if @GetDllFormClass = nil then
            Abort;
            tmp3:=GetDllFormClass(application.Handle,Self.Handle);
           Application.CreateForm(tmp3,tmp4);
            tmp4.Show;
       except
             raise;
        end;
    end;end.
      

  2.   

    我这是在网上下载别人的源码,可以编译,什么对象会译放掉了呢?
    下面是DLL里的源码:library Dll;{ 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
      SysUtils,
      Classes,
      Unit2 in 'Unit2.pas' {Frame1: TFrame},
      Unit1 in 'Unit1.pas' {Form1};{$R *.res}
    exports  GetDllFrameClass,GetDllFormClass;begin
    end.下面是dll引用的窗体源码:unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
      Dialogs, StdCtrls;type
      TFrame1 = class(TFrame)
        btn1: TButton;
        mmo1: TMemo;
        lbl1: TLabel;
        scrlbr1: TScrollBar;
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    TFrameClass = class of TFrame   ;
    function GetDllFrameClass(App:THandle;frm:THandle):TFrameClass;stdcall;
    implementation{$R *.dfm}
    function GetDllFrameClass(App:THandle;frm:THandle):TFrameClass;stdcall;
    begin
    //传进来的参数原来是为了和主程序沟通,不是必要的,这里就不管它了
    Result:=TFrame1;
    end;
    end.