转载:https://blog.csdn.net/tojohnonly/article/details/70246965

共享内存 (也叫内存映射文件) 主要是通过映射机制实现的 , Windows 下进程的地址空间在逻辑上是相互隔离的 , 但在物理上却是重叠的 ; 所谓的重叠是指同一块内存区域可能被多个进程同时使用 , 当调用 CreateFileMapping 创建命名的内存映射文件对象时 , Windows 即在物理内存申请一块指定大小的内存区域 , 返回文件映射对象的句柄 hMap ; 为了能够访问这块内存区域必须调用 MapViewOfFile 函数 , 促使 Windows 将此内存空间映射到进程的地址空间中 ; 当在其他进程访问这块内存区域时 , 则必须使用 OpenFileMapping 函数取得对象句柄 hMap , 并调用 MapViewOfFile 函数得到此内存空间的一个映射 , 这样系统就把同一块内存区域映射到了不同进程的地址空间中 , 从而达到共享内存的目的 , 代码如下 :

进程 A 将数据写入到共享内存 :

#include "stdafx.h"
#include <windows.h>
using namespace std;

#define BUF_SIZE 4096

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// 定义共享数据
char szBuffer[] = "Hello Shared Memory";

// 创建共享文件句柄
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // 物理文件句柄
NULL, // 默认安全级别
PAGE_READWRITE, // 可读可写
0, // 高位文件大小
BUF_SIZE, // 地位文件大小
L"ShareMemory" // 共享内存名称
);

// 映射缓存区视图 , 得到指向共享内存的指针
LPVOID lpBase = MapViewOfFile(
hMapFile, // 共享内存的句柄
FILE_MAP_ALL_ACCESS, // 可读写许可
0,
0,
BUF_SIZE
);

// 将数据拷贝到共享内存
strcpy((char*)lpBase,szBuffer);

// 线程挂起等其他线程读取数据
Sleep(20000);

// 解除文件映射
UnmapViewOfFile(lpBase);
// 关闭内存映射文件对象句柄
CloseHandle(hMapFile);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
进程 B 获取共享内存中的数据 :

#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;

#define BUF_SIZE 4096

int main()
{
// 打开共享的文件对象
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS,NULL,L"ShareMemory");
if (hMapFile)
{
LPVOID lpBase = MapViewOfFile(hMapFile,FILE_MAP_ALL_ACCESS,0,0,0);
// 将共享内存数据拷贝出来
char szBuffer[BUF_SIZE] = {0};
strcpy(szBuffer,(char*)lpBase);
printf("%s",szBuffer);

// 解除文件映射
UnmapViewOfFile(lpBase);
// 关闭内存映射文件对象句柄
CloseHandle(hMapFile);
}
else
{
// 打开共享内存句柄失败
printf("OpenMapping Error");
}
return 0;
}
---------------------
作者:Ensk
来源:CSDN
原文:https://blog.csdn.net/tojohnonly/article/details/70246965
版权声明:本文为博主原创文章,转载请附上博文链接!

C++ windows下共享内存的更多相关文章

  1. windows 下共享内存使用方法示例

    windows下共享内存使用方法较 linux 而言微微复杂 示例实现的功能 有一个视频文件,一块内存区域 : 程序 A,将该视频写入该内存区域 : 程序 B,从该内存区域读取该视频 : 代码模块实现 ...

  2. 利用windows api共享内存通讯

    主要涉及CreateFile,CreateFileMapping,GetLastError,MapViewOfFile,sprintf,OpenFileMapping,CreateProcess Cr ...

  3. Windows进程间通信--共享内存映射文件(FileMapping)--VS2012下发送和接收

    之前以为两个互不相关的程序a.exe b.exe通信就只能通过网络,人家说可以通过发消息,我还深以为不然,对此,我表示万分惭愧. 之前课本上说的进程间通信,有共享内存.管道等之类的,但没有自己操刀写过 ...

  4. linux下共享内存mmap和DMA(直接访问内存)的使用 【转】

    转自:http://blog.chinaunix.net/uid-7374279-id-4413316.html 介绍Linux内存管理和内存映射的奥秘.同时讲述设备驱动程序是如何使用“直接内存访问” ...

  5. Windows 下的内存泄漏检测方法

    在 Windows 下,可使用 Visual C++ 的 C Runtime Library(CRT) 检测内存泄漏. 首先,我们在.c或.cpp 文件首行插入这一段代码: #define _CRTD ...

  6. linux 下共享内存

    一.共享内存相关知识 所谓共享内存,就是多个进程间共同地使用同一段物理内存空间,它是通过将同一段物理内存映射到不同进程的 虚拟空间来实现的.由于映射到不同进程的虚拟空间中,不同进程可以直接使用,不需要 ...

  7. 学习笔记:Linux下共享内存的方式实现进程间的相互通信

    一.常用函数 函数系列头文件 #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> ft ...

  8. Windows 下整理内存工具推荐——cleanmem

    ---恢复内容开始--- cleanmem 是个不错的内存整理工具,www.xdown.com 下载有便携版提供下载. 软件有pro版和free版,一般情况下,free版够用了,没必要用pro版. p ...

  9. windows下共享式服务开发

    参考: https://blog.csdn.net/dongyewolong/article/details/8164873 https://blog.csdn.net/qwertyupoiuytr/ ...

随机推荐

  1. ModuleNotFoundError: No module named '_sqlite3'

    ModuleNotFoundError: No module named '_sqlite3' 解决: 1,首先安装 sqlite-devel yum install sqlite-devel 2,重 ...

  2. leetcode103

    class Solution { public: vector<vector<int>> zigzagLevelOrder(TreeNode* root) { vector&l ...

  3. Spring Boot 16 条最佳实践

    Spring Boot是最流行的用于开发微服务的Java框架.在本文中,我将与你分享自2016年以来我在专业开发中使用Spring Boot所采用的最佳实践.这些内容是基于我的个人经验和一些熟知的Sp ...

  4. ACCESS 组合字段 order by 出错

    ACCESS 组合字段 order by 出错 SELECT boardID,boardType,parentID,child,depth,rootID,(parentStr + ',' + boar ...

  5. spring mvc 映射器和适配器

    映射器和适配器 1.非注解的映射器和适配器 a. 入门程序中的单个映射 BeanNameUrlHandlerMapping SimpleControllerHandlerAdapter b.另一种ma ...

  6. 解决CentOS内网机通过Windows下架设代理来访问网络

    新分配的CentOS运行在内网环境下,无法连接Internet,为了能够使用yum部署OpenVas工具,需要在内网下一台Windows主机架设代理,作代理服务器来令虚拟机上网. 代理服务器选择了CC ...

  7. centos7 配置dns服务器

    yum install bind ----------------------------------------------------------------------------------- ...

  8. clr相关名词

    程序集:一个或多个类型定义文件和资源文件的集合 Native Code(本机代码): 已被编译为特定于处理器的机器码的代码. 本地代码(native code)是计算机编程(代码),编译用来运行一个特 ...

  9. The Process of Google Hiring

    [The Process of Google Hiring] 1.keynote 1: The Google hiring process is designed to hire the most t ...

  10. 14.Longest Common Prefix (String)

    Write a function to find the longest common prefix string amongst an array of strings. class Solutio ...