博客
关于我
进程间通信(1) dll 实现进程的内存共享
阅读量:63 次
发布时间:2019-02-26

本文共 1575 字,大约阅读时间需要 5 分钟。

1. 两个进程访问同一个dll

在这里插入图片描述
2.写入时复制(CopyOnWrite,简称COW)思想

如果有多个调用者(Callers)同时访问相同的资源(如内存或者是磁盘上的数据存储),他们会共同获取相同的指针指向相同的资源,直到某个调用者修改资源内容时,系统才会真正复制一份专用副本(private copy)给该调用者, 只有更新操作,才会去复制一份新的数据并更新替换,否则都是访问同一个资源。

3. 注意点

3.1 不能传指针作共享,每进程有自己的内存空间。

Each process gets its own address space. It is very important that pointers are never stored in a variable contained in a shared data segment. A pointer might be perfectly valid in one application but not in another.

3.2. 共享变量得初始化Any variables in a shared data segment must be statically initialized.

4.1 dll:

#pragma data_seg("MySec")int nDate = 0;#pragma data_seg()#pragma comment(linker,"/section:MySec,rws")  _declspec(dllexport) void setIntData(int nDate1){       nDate = nDate1;}_declspec(dllexport) int getIntData(){       return nDate;}

4.2 exe中的代码:

void testSetFunc(){       HINSTANCE hInst;    hInst = LoadLibrary("Dll1.dll");    typedef void (*setIntData)(int nDate1);    setIntData setIntDataFunc = (setIntData)GetProcAddress(hInst, MAKEINTRESOURCE(5));    if (!setIntDataFunc)    {           return;    }    setIntDataFunc(66);}void testGetFunc(){       HINSTANCE hInst;    hInst = LoadLibrary("Dll1.dll");    typedef int (*getIntData)();    getIntData getIntDataFunc = (getIntData)GetProcAddress(hInst, MAKEINTRESOURCE(2));    if (!getIntDataFunc)    {           return;    }    int n = getIntDataFunc();    char ch[256] = "\0";    sprintf_s(ch, "%d", n);    MessageBox(g_hWnd, ch, "f", MB_OK);}

4.3在两个进程exe中分别调用testSetFunc,testGetFunc将得到同一个结果。

【引用】

[1]: 代码地址
[2]: How do I share data in my DLL with an application or with other DLLs

转载地址:http://tmr.baihongyu.com/

你可能感兴趣的文章
mysql启动报错The server quit without updating PID file几种解决办法
查看>>
MySQL命令行登陆,远程登陆MySQL
查看>>
mysql命令:set sql_log_bin=on/off
查看>>
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MYSQL和ORACLE的一些操作区别
查看>>
mysql和redis之间互相备份
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基本知识点梳理和查询优化
查看>>
mysql基础
查看>>