Dll 학습1Dll 창 만들기 및 동적 참조 및 창 데모 해제
4997 단어 demo
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.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
msdn에서 인증서 설정 데이터베이스 이미지 데모[페이지 맨 위로 돌아가기] 인바운드 연결 구성 인바운드 연결을 위한 Host_ 구성A HOST에서_A에서 HOST_B로그인 이름을 만듭니다.USE master; CREATE LOGIN HOST_B_login WIT...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.