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源码

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.

Dll学习一_Dll 创建并动态引用窗体且释放窗体Demo的更多相关文章

  1. Dll学习二_Dll 窗体中动态创建数据并使用Demo

    沿用上一篇Demo 环境:DelphiXE,XP,SQL2005 贴出改动过的单元代码: dbGrid控件版: unit SubMain_Unit; interface uses Windows, M ...

  2. Dll学习三_Dll 相互间以及主程序间的数据共享——测试未通过,应该用内存映射

    测试环境:XP,DELPHI XE 验证通过结构:主程序+一个Dll窗体 共享方式原理:通过主程序与各Dll定义相同的参数结构体,由主程序实例化该结构体,对于各Dll间的共享,通过传主程序实例化的结构 ...

  3. C#动态引用DLL的方法

    C#编程中,使用dll调用是经常的事,这样做的好处是非常多的,比如把某些功能封装到一个dll中,然后主程序动态调用这个dll. 废话不多说,举例说明如下. 首先,我们需要封装一个dll,vs2008下 ...

  4. vue学习【二】vue结合axios动态引用echarts

    大家好,我是一叶,本篇是vue学习的第二篇,本篇将要讲述vue结合axios动态引用echarts. 在vue中,异步刷新使用的是axios,类似于大家常用的ajax,其实axios已经是vue的第二 ...

  5. ABP 基于DDD的.NET开发框架 学习(六)创建新动态Api

    我们想要这个服务暴露成一个Web API控制器,以方便客户端调用.ASP.NET Boilerplate能够自动且动态地为这个应用服务创建Web API 控制器,只需要一行配置代码即可完成. Dyna ...

  6. C#动态创建和动态使用程序集、类、方法、字段等

    C#动态创建和动态使用程序集.类.方法.字段等 分类:技术交流 (3204)  (3)   首先需要知道动态创建这些类型是使用的一些什么技术呢?其实只要相关动态加载程序集呀,类呀,都是使用反射,那么动 ...

  7. [转载] 动态链接库dll的 静态加载 与 动态加载

    转载自:http://blog.csdn.net/youxin2012/article/details/11538491 dll 两种链接方式  : 动态链接和静态链接(链接亦称加载)   动态链接是 ...

  8. 动态链接库dll的 静态加载 与 动态加载

    dll 两种链接方式  : 动态链接和静态链接(链接亦称加载) 动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统 ...

  9. .Net中把图片等文件放入DLL中,并在程序中引用

    原文:.Net中把图片等文件放入DLL中,并在程序中引用 [摘要] 有时我们需要隐藏程序中的一些资源,比如游戏,过关后才能看到图片,那么图片就必须隐藏起来,否则不用玩这个游戏就可以看到你的图片了,呵呵 ...

随机推荐

  1. IOS 读取本地的Json/plist 文件

    一.一般本地可以存储轻量级数据存储 plist  这个主要是操作字典 方法如下: NSString * sampleFile= [[[NSBundle mainBundle] bundlePath] ...

  2. MVC+easyui 完整实现

    学习mvc半月有余,趁几天假期,没事做就动手做一个完整的网站玩玩,顺便和上家公司的方法做个比较.页面引擎采用mvc自带的功能,建立视图,交给.net自带渲染出页面(对比:上家公司采用的第三方组件渲染的 ...

  3. ios优化复制大文件时,如何使内存运用最少且效率最高

    我也是纠结了好几天,我想自己想个办法,但是数据复制不上去,我现在还不明白,如果有人知道我错在哪了,请留言,如果还有更好的方法,请分享共同进步. ____________________________ ...

  4. 原来腾讯还出过一个开源项目libco

    虽然只能在OpenSUSE上使用,还是应该赞一个的.

  5. VMware系统运维(十一)部署虚拟化桌面 Horizon View 5.2 HTML ACCESS安装

    如果你希望在浏览器上面能够连接到用户桌面,那么HTML ACCESS是必须安装的,下面开始安装. 1.点击红框文件 2.打开安装向导,点击"下一步" 3.接受协议,点击" ...

  6. node.js学习的资源整理

    node中文社区 Node.js专业中文社区:https://cnodejs.org/ node文档 node.js 中文api :http://nodeapi.ucdok.com/ node.js入 ...

  7. HTML 5 History API的”前生今世”

    History是有趣的,不是吗?在之前的HTML版本中,我们对浏览历史记录的操作非常有限.我们可以来回使用可以使用的方法,但这就是一切我们能做的了. 但是,利用HTML 5的History API,我 ...

  8. js实现shell排序

    //shell排序配插入排序function shell_insert_sort(arr){ var gap = arr.length; do{ gap = parseInt(gap/3) + 1; ...

  9. 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。

    去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...

  10. Miniui updateRow更改列字段值

    当UPC等于upccode时,更改列Scanned+1 //先grid.findRow找到UPC等于upccode的行对象 var row = grid.findRow(function (row) ...