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. UIWebView获得内容的高 高度自适应 宽度自适应

    UIWebView获得内容的高-作出自适应高的UIWebView- (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *height ...

  2. 移动的rem自适应

    (function (doc, win) { var docEl = doc.documentElement, //html resizeEvt = 'orientationchange' in wi ...

  3. 《MFC游戏开发》笔记七 游戏特效的实现(一):背景滚动

    本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9344721 作者:七十一雾央 新浪微博:http:// ...

  4. asp.net oracle 存储过程

    ORACLE代码 CREATE OR REPLACE PROCEDURE gd_CURSOR(MYCS1 OUT SYS_REFCURSOR,MYCS2 OUT SYS_REFCURSOR,a out ...

  5. CF Sea and Islands

    Sea and Islands time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. hdu-5683 zxa and xor (位运算)

    题目链接: zxa and xor Time Limit: 16000/8000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  7. 代码创建FlexibleSpaceBarButton(弹性空白)

    //1.创建toolbar左边的按钮,调用initWithTitle方法进行初始化 // UIBarButtonItem *extend = [[UIBarButtonItemalloc]initWi ...

  8. Javascript中二级联动

    主要使用到到了地址JSON格式,来做,没有涉及数据库的读取. <!DOCTYPE html><html><head> <meta charset=" ...

  9. 关于Css选择器优先级

    今天练习css的时候,重叠后的style发现不起作用,原来css选择器优先级大有文章. 声明: yi下内容选自 51cto.com  --加以自己的理解 以备日后参照使用,毕竟自己理解的才是自己的. ...

  10. 学习Slim Framework for PHP v3 ( 二)

    昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...