日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo

發(fā)布時(shí)間:2023/12/2 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1、新建Dll工程

2、Dll工程全部代碼

library SubMain;{ Important note about DLL memory management: ShareMem must be thefirst unit in your library's USES clause AND your project's (selectProject-View Source) USES clause if your DLL exports any procedures orfunctions that pass strings as parameters or function results. Thisapplies to all strings passed to and from your DLL--even those thatare nested in records and classes. ShareMem is the interface unit tothe BORLNDMM.DLL shared memory manager, which must be deployed alongwith your DLL. To avoid using BORLNDMM.DLL, pass string informationusing PChar or ShortString parameters. }usesSysUtils, Forms,Classes, Windows, Dialogs,SubMain_Unit in 'SubMain_Unit.pas' {Frm_SubMain};varAppHnd: Thandle;//應(yīng)用程序的句柄變量AppScr: TScreen;//應(yīng)用程序的環(huán)境變量 {$R *.res}procedure DllEntryPoint(dwReason: DWord); begincase dwReason ofDLL_PROCESS_ATTACH:beginAppHnd := Application.Handle;//備份Exe句柄AppScr := Screen;//備份Exe環(huán)境end;DLL_THREAD_ATTACH: ShowMessage('Create Thread'); //Dll文件中最好用Messagebox,ShowMessage部分環(huán)境中容易抽風(fēng)DLL_THREAD_DETACH: ShowMessage('Free Thread'); //Dll文件中最好用Messagebox,ShowMessage部分環(huán)境中容易抽風(fēng) DLL_PROCESS_DETACH:beginif Frm_SubMain <> nil then FreeAndNil(Frm_SubMain);//防止Dll窗體未釋放干凈Application.Handle := AppHnd; //恢復(fù)Exe句柄Screen := AppScr;//恢復(fù)Exe環(huán)境end;end; end;exportsCreateFrm,DropFrm;//輸出函數(shù)接口beginDllProc := @DllEntryPoint;DllEntryPoint(DLL_PROCESS_ATTACH); end.
3、SubMain_Unit.pas源碼

unit SubMain_Unit;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, FyFun_Unit;typeTFrm_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;//接口函數(shù)聲明procedure DropFrm; export;stdcall;//接口函數(shù)聲明 varFrm_SubMain: TFrm_SubMain;implementation{$R *.dfm} procedure CreateFrm(AppHnd: THandle);//窗體創(chuàng)建函數(shù) beginApplication.Handle := AppHnd;if not Assigned(Frm_SubMain) thenFrm_SubMain := TFrm_SubMain.Create(Application);Frm_SubMain.Show; end;procedure DropFrm;//窗體釋放函數(shù) beginif Frm_SubMain <> nil thenFreeAndNil(Frm_SubMain); end; procedure TFrm_SubMain.FormClose(Sender: TObject; var Action: TCloseAction); beginAction := caFree;//dll窗體關(guān)閉自動(dòng)釋放 end;procedure TFrm_SubMain.FormDestroy(Sender: TObject); beginFrm_SubMain := nil;//dll窗體關(guān)閉自動(dòng)釋放 end;end.4、調(diào)用文件Main.pas代碼

unit Main_Unit;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;typeTCreateFrm = 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);privateLibHandle: THandle;FormRef: LongInt;{ Private declarations }public{ Public declarations }end;varFrm_Main: TFrm_Main;implementation{$R *.dfm}procedure TFrm_Main.Btn_1Click(Sender: TObject); //動(dòng)態(tài)加載Dll文件 beginif LibHandle = 0 thenbeginLibHandle := SafeLoadLibrary('SubMain.dll');if LibHandle = 0 thenraise Exception.Create('Not Found Dll File')elseShowMessage('Dll Loaded');end; end;procedure TFrm_Main.Btn_2Click(Sender: TObject); //釋放Dll文件 beginif LibHandle > 0 thenbeginFreeLibrary(LibHandle);LibHandle := 0;ShowMessage('Dll UnLoaded');end; end;procedure TFrm_Main.Btn_3Click(Sender: TObject); //讀取Dll文件窗體并顯示 varCreateFrm: TCreateFrm; beginif LibHandle = 0 thenraise Exception.Create('Place Load Dll File First');@CreateFrm := GetProcAddress(LibHandle,PChar('CreateFrm'));if @CreateFrm = nil thenraise Exception.Create('Function Error');CreateFrm(Application.Handle); end;procedure TFrm_Main.Btn_4Click(Sender: TObject); //釋放Dll窗體 varDropFrm: TDropFrm; begin@DropFrm := GetProcAddress(LibHandle,PChar('DropFrm'));if @DropFrm = nil thenraise Exception.Create('Function Error');DropFrm; end;end.


轉(zhuǎn)載于:https://www.cnblogs.com/jupt/p/3922938.html

總結(jié)

以上是生活随笔為你收集整理的Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。