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. cocosbuilder中使用字体描边时,字符重叠,间距过小问题

    cocosbuilder中使用字体描边时,字符重叠,间距过小问题 cocos2d-x 3.7 v3.7解析cocosbuilder中描边字体的代码如下: void LabelTTFLoader::pa ...

  2. Java基础知识强化之IO流笔记73:NIO之 Channel

    1. Java NIO的Channel(通道)类似 Stream(流),但又有些不同: 既可以从通道中读取数据,又可以写数据到通道.但流的读写通常是单向的. 通道可以异步地读写. 通道中的数据总是要先 ...

  3. 【Mood-7】tell 2 my gf-miss u not sudden but always

    #sorry not coming 2 see u the national day holiday! I'm BULL in the land,fighting 4 u and babies!  # ...

  4. java正则表达式常用实例——借鉴思路

    转载自:http://mp.weixin.qq.com/s?__biz=MjM5OTM4NDMyMg==&mid=2650044497&idx=1&sn=dc80fa35f7e ...

  5. jquery插件开发规范

    一.请给你的代码加上注释 这个世界不存在百分百的完美的jquery插件,注释不止是给别人看.更重要的是给自己看. 你应该把注释当做你代码的一部分,养成随手加注释的习惯,尤其是编写javascript的 ...

  6. 怒刷DP之 HDU 1029

    Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d &a ...

  7. 轻松绕过极域电子教室、和教师控制 Say GoodBye

    注意:以下博文(包括但不限于汉字.英文.阿拉伯数字 .图片.影像,以及前述之各种任意组合等等)均为随意敲击键盘所出,用于检验本人电脑键盘录入.屏幕显示的机械.光电性能,并不代表本人观点.如需要详查请直 ...

  8. 移动开发Html 5前端性能优化指南

    详细内容请点击 PC优化手段在Mobile侧同样适用在Mobile侧我们提出三秒种渲染完成首屏指标基于第二点,首屏加载3秒完成或使用Loading基于联通3G网络平均338KB/s(2.71Mb/s) ...

  9. Part 56 Generics in C#

     

  10. [转] 利用任务计划重启sqlserver服务

    1.建立一个批处理文件restartsqlserver.bat     内容如下:     net   stop   mssqlserver   /y    net   start  mssqlser ...