Dll 학습1Dll 창 만들기 및 동적 참조 및 창 데모 해제

4997 단어 demo
1. 새 Dll 프로젝트
2, Dll 프로젝트 전체 코드
library SubMain;

{ 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, Forms,
  Classes, Windows, Dialogs,
  SubMain_Unit in 'SubMain_Unit.pas' {Frm_SubMain};

var
  AppHnd: Thandle;//         
  AppScr: TScreen;//         
{$R *.res}

procedure DllEntryPoint(dwReason: DWord);
begin

  case dwReason of
    DLL_PROCESS_ATTACH:
    begin
      AppHnd := Application.Handle;//  Exe  
      AppScr := Screen;//  Exe  
    end;
    DLL_THREAD_ATTACH: ShowMessage('Create Thread'); //Dll      Messagebox,ShowMessage         
    DLL_THREAD_DETACH: ShowMessage('Free Thread'); //Dll      Messagebox,ShowMessage          
    DLL_PROCESS_DETACH:
    begin
      if Frm_SubMain <> nil then FreeAndNil(Frm_SubMain);//  Dll       
      Application.Handle := AppHnd;      //  Exe  
      Screen := AppScr;//  Exe  
    end;
  end;
end;

exports
  CreateFrm,DropFrm;//      

begin
  DllProc := @DllEntryPoint;
  DllEntryPoint(DLL_PROCESS_ATTACH);
end.

3、SubMain_Unit.pas 소스
4
unit SubMain_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, FyFun_Unit;

type
  TFrm_SubMain = class(TForm)
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  procedure CreateFrm(AppHnd: THandle);export;stdcall;//      
  procedure DropFrm; export;stdcall;//      
var
  Frm_SubMain: TFrm_SubMain;

implementation

{$R *.dfm}
procedure CreateFrm(AppHnd: THandle);//      
begin
  Application.Handle := AppHnd;

  if not Assigned(Frm_SubMain) then
    Frm_SubMain := TFrm_SubMain.Create(Application);

  Frm_SubMain.Show;
end;

procedure DropFrm;//      
begin
  if Frm_SubMain <> nil then
    FreeAndNil(Frm_SubMain);
end;
procedure TFrm_SubMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;//dll        
end;

procedure TFrm_SubMain.FormDestroy(Sender: TObject);
begin
  Frm_SubMain := nil;//dll        
end;

end.
4, 호출 파일Main.pas 코드
unit Main_Unit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TCreateFrm = procedure(AppHnd: THandle); stdcall;
  TDropFrm = procedure; stdcall;
  TFrm_Main = class(TForm)
    Btn_1: TButton;
    Btn_2: TButton;
    Btn_3: TButton;
    Btn_4: TButton;
    procedure Btn_1Click(Sender: TObject);
    procedure Btn_2Click(Sender: TObject);
    procedure Btn_3Click(Sender: TObject);
    procedure Btn_4Click(Sender: TObject);
  private
    LibHandle: THandle;
    FormRef: LongInt;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Frm_Main: TFrm_Main;

implementation

{$R *.dfm}

procedure TFrm_Main.Btn_1Click(Sender: TObject);		//    Dll  
begin
  if LibHandle = 0 then
  begin
    LibHandle := SafeLoadLibrary('SubMain.dll');
    if LibHandle = 0 then
      raise Exception.Create('Not Found Dll File')
    else
      ShowMessage('Dll Loaded');
  end;
end;

procedure TFrm_Main.Btn_2Click(Sender: TObject);		//  Dll  
begin
  if LibHandle > 0 then
  begin
    FreeLibrary(LibHandle);
    LibHandle := 0;
    ShowMessage('Dll UnLoaded');
  end;
end;

procedure TFrm_Main.Btn_3Click(Sender: TObject);		//  Dll       
var
  CreateFrm: TCreateFrm;
begin
  if LibHandle = 0 then
    raise Exception.Create('Place Load Dll File First');

  @CreateFrm := GetProcAddress(LibHandle,PChar('CreateFrm'));
  if @CreateFrm = nil then
    raise Exception.Create('Function Error');

  CreateFrm(Application.Handle);
end;

procedure TFrm_Main.Btn_4Click(Sender: TObject);		//  Dll  
var
  DropFrm: TDropFrm;
begin
  @DropFrm := GetProcAddress(LibHandle,PChar('DropFrm'));
  if @DropFrm = nil then
    raise Exception.Create('Function Error');

  DropFrm;
end;

end.

좋은 웹페이지 즐겨찾기