测试环境:XP,DELPHI XE

验证通过结构:主程序+一个Dll窗体

共享方式原理:通过主程序与各Dll定义相同的参数结构体,由主程序实例化该结构体,对于各Dll间的共享,通过传主程序实例化的结构体指针达到各Dll与主程序相互间的数据共享。且Dll释放不影响主程序实例化结构体时获得的内存空间

主程序代码:

unit Main_Unit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TPara = record
ADOConnStr: String;
end;
TCreateFrm = procedure(AppHnd: THandle; APar: TPara); stdcall; //此处传递主程序实例化后的参数结构体
TCreateSubFrm = procedure(AppHnd: THandle); stdcall;
TDropFrm = procedure; stdcall;
TFrm_Main = class(TForm)
Btn_1: TButton;
Btn_2: TButton;
Btn_3: TButton;
Btn_4: TButton;
Btn_5: TButton;
Btn_6: TButton;
Btn_7: TButton; procedure Btn_1Click(Sender: TObject);
procedure Btn_2Click(Sender: TObject);
procedure Btn_3Click(Sender: TObject);
procedure Btn_4Click(Sender: TObject);
procedure Btn_5Click(Sender: TObject);
procedure Btn_6Click(Sender: TObject);
procedure Btn_7Click(Sender: TObject);
private
LibHandle: THandle;
FormRef: LongInt;
{ Private declarations }
public
{ Public declarations }
end; var
Frm_Main: TFrm_Main;
APara: TPara; implementation {$R *.dfm} procedure TFrm_Main.Btn_1Click(Sender: TObject);
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);
begin
if LibHandle > 0 then
begin
FreeLibrary(LibHandle);
LibHandle := 0;
ShowMessage('Dll UnLoaded');
end;
end; procedure TFrm_Main.Btn_3Click(Sender: TObject);
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'); APara.ADOConnStr := 'Provider=SQLOLEDB.1;Password=*****;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1'; //结构体赋值
CreateFrm(Application.Handle,APara);
end; procedure TFrm_Main.Btn_4Click(Sender: TObject);
var
DropFrm: TDropFrm;
begin
@DropFrm := GetProcAddress(LibHandle,PChar('DropFrm'));
if @DropFrm = nil then
raise Exception.Create('Function Error'); DropFrm;
end; procedure TFrm_Main.Btn_5Click(Sender: TObject);
var
CreateSubFrm: TCreateSubFrm;
begin
if LibHandle = 0 then
raise Exception.Create('Place Load Dll File First'); @CreateSubFrm := GetProcAddress(LibHandle,PChar('CreateSubFrm'));
if @CreateSubFrm = nil then
raise Exception.Create('Function Error'); CreateSubFrm(Application.Handle);
end; procedure TFrm_Main.Btn_7Click(Sender: TObject);
begin
ShowMessage(APara.ADOConnStr); //用来释放Dll后,验证结构体内存块是否同步被释放
end; end.

Dll引用代码:

unit SubMain_Unit;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, FyDataConn_Unit, ActiveX,
ADODB, StdCtrls, cxGraphics, cxControls, cxLookAndFeels,
cxLookAndFeelPainters, cxStyles, dxSkinsCore, dxSkinBlueprint,
dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinHighContrast,
dxSkinSevenClassic, dxSkinSharpPlus, dxSkinStardust, dxSkinTheAsphaltWorld,
dxSkinVS2010, dxSkinWhiteprint, dxSkinscxPCPainter, cxCustomData, cxFilter,
cxData, cxDataStorage, cxEdit, cxNavigator, cxDBData, cxGridLevel, cxClasses,
cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
cxGrid, Grids, DBGrids,dxCore; type
TPara = record //与主程序定义一个一样的结构体
ADOConnStr: String;
end;
TPPara = ^TPara; //定义该结构体的指针结构
TFrm_SubMain = class(TForm)
Btn_1: TButton;
GTV_1: TcxGridDBTableView;
GL_1: TcxGridLevel;
Grd_1: TcxGrid;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormDestroy(Sender: TObject);
procedure Btn_1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
DSet: TADODataSet;
DS: TDataSource;
Conn: TADOConnection;
{ Private declarations }
public
{ Public declarations }
end;
procedure CreateFrm(AppHnd: THandle; APar: TPara);export;stdcall;
procedure DropFrm; export;stdcall; var
Frm_SubMain: TFrm_SubMain;
LocPara: TPara;
PPara: TPPara; implementation {$R *.dfm}
procedure CreateFrm(AppHnd: THandle; APar: TPara);
begin
Application.Handle := AppHnd;
PPara := @APar; //直接
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.Btn_1Click(Sender: TObject);
var
SQL: String;
begin
DSet.Connection := Conn;
DS.DataSet := DSet;
SQL := 'Select * From Cg_CgDanSub';
dbOpen(SQL,DSet); //自定义函数,用于打开数据集
GTV_1.DataController.DataSource := DS;
(GTV_1.DataController as IcxCustomGridDataController).DeleteAllItems; //清除cxGrid列
(GTV_1.DataController as IcxCustomGridDataController).CreateAllItems(False); //添加cxGrid列 end; procedure TFrm_SubMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end; procedure TFrm_SubMain.FormCreate(Sender: TObject);
begin
Conn := TADOConnection.Create(Application);
Conn.LoginPrompt := False;
Conn.ConnectionString := PPara.ADOConnStr;
//Conn.ConnectionString := 'Provider=SQLOLEDB.1;Password=fydesign;Persist Security Info=True;User ID=sa;Initial Catalog=test;Data Source=127.0.0.1';
Conn.Connected := True;
DSet := TADODataSet.Create(Application);
DS := TDataSource.Create(Application);
end; procedure TFrm_SubMain.FormDestroy(Sender: TObject);
begin
DSet.Free;
DS.Free;
FreeAndNil(Conn);
Frm_SubMain := nil;
end;
initialization
dxInitialize;
finalization
dxFinalize;
end.

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

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

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

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

    1.新建Dll工程 2.Dll工程全部代码 library SubMain; { Important note about DLL memory management: ShareMem must b ...

  3. Windows进程间通讯(IPC)----内存映射文件

    内存映射文件原理 内存映射文件是通过在虚拟地址空间中预留一块区域,然后通过从磁盘中已存在的文件为其调度物理存储器,访问此虚拟内存空间就相当于访问此磁盘文件了. 内存映射文件实现过程 HANDLE hF ...

  4. Android JNI学习(三)——Java与Native相互调用

    本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...

  5. docker深入学习三

    docker学习三:network docker支持容器之间的网络通信,docker的网络通信方式有以下五种: bridge docker 默认的网络驱动,如果不指定网络驱动,docker就会创建一个 ...

  6. ElasticSearch7.3学习(三十二)----logstash三大插件(input、filter、output)及其综合示例

    1. Logstash输入插件 1.1 input介绍 logstash支持很多数据源,比如说file,http,jdbc,s3等等 图片上面只是一少部分.详情见网址:https://www.elas ...

  7. Java开发学习(三十六)----SpringBoot三种配置文件解析

    一. 配置文件格式 我们现在启动服务器默认的端口号是 8080,访问路径可以书写为 http://localhost:8080/books/1 在线上环境我们还是希望将端口号改为 80,这样在访问的时 ...

  8. dll学习

    Dll:动态链接库 动态链接库(dll)是包含共享函数库的二进制文件,可以被多个应用程序同时使用.建立应用程序的可执行文件时,不必将DLL连接到应用程序中,而是在运行时动态装载DLL,装载时DLL被映 ...

  9. HTTP学习三:HTTPS

    HTTP学习三:HTTPS 1 HTTP安全问题 HTTP1.0/1.1在网络中是明文传输的,因此会被黑客进行攻击. 1.1 窃取数据 因为HTTP1.0/1.1是明文的,黑客很容易获得用户的重要数据 ...

随机推荐

  1. (easy)LeetCode 205.Reverse Linked List

    Reverse a singly linked list. 解法一:记录单链表每个节点的val,然后重新为单链表赋值.(取巧,仅仅是将val部分改变,原始node节点并没有改变) 代码如下: /** ...

  2. asp.net 自定义文本框

    using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using S ...

  3. Android--使用Notification在通知栏显示消息

    在一个Activity中点击按钮,产生一个通知栏消息通知. package cn.luxh.mynotice; import android.os.Bundle; import android.uti ...

  4. Ubuntu12.04配置静态ip地址

    Ubuntu12.04配置静态ip地址 $sudo gedit /etc/network/interfaces 原有内容只有如下两行: auto lo iface lo inet loopback 向 ...

  5. 多线程为何用while判断条件,而不用if。

    一.代码: package zz.produceandconsumer; import java.util.LinkedList; public class Storage { private fin ...

  6. Shell | grep with n following lines

    'foo' sample.txt ➜ dex-method-counts git:(master) ./dex-method-counts ~/Downloads/n.apk | 'hui' hui: ...

  7. 海外支付:抵御信用卡欺诈的CyberSource

    海外支付:抵御信用卡欺诈的CyberSource 吴剑 2014-06-04 原创文章,转载必需注明出处:http://www.cnblogs.com/wu-jian 吴剑 http://www.cn ...

  8. 那些年,我们开发的接口之:QQ登录(OAuth2.0)

    那些年,我们开发的接口之:QQ登录(OAuth2.0) 吴剑 2013-06-14 原创文章,转载必须注明出处:http://www.cnblogs.com/wu-jian 前言 开发这些年,做过很多 ...

  9. HDU1325

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1325 #include<stdio.h> #include<algorithm&g ...

  10. 【Unity Shaders】学习笔记——SurfaceShader(五)让纹理动起来

    [Unity Shaders]学习笔记——SurfaceShader(五)让纹理动起来 转载请注明出处:http://www.cnblogs.com/-867259206/p/5611222.html ...