Window 共享内存
转载请注明来源:https://www.cnblogs.com/hookjc/
C++使用共享内存实现进程间通信
文件映射是一种实现进程间单向或双向通信的机制。它允许两个或多个本地进程间相互通信。为了共享文件或内存,所有的进程必须使用相同的文件映射的名字或是句柄。
为了实现共享文件,第一个进程先调用CreateFile方法。接下来调用CreateFileMapping方法来创建一个文件映射对象。并为文件映射指明一个句柄和名称。由于事件,信号,互斥对象和文件映射等这些内核对象都共享同一个名字空间,所以如果这个名字和其他一个对象的名称重名的话那么将创建失败。
为了实现共享内存,进程应首先调用CreateFileMapping函数然后在hFile参数中传入INVALID_HANDLE_VALUE宏来替代句柄。相应的文件映射对象会从系统的分页文件中获得一段内存。如果hFile参数的值是INVALID_HANDLE_VALUE,那么你在调用CreateFileMapping时必须给共享内存指定一个大小值。
使用共享内存或文件的进程必须使用MapViewOfFile函数或MapViewOfFileEx函数来创建一个文件视图。
下面我们创建一个名称为"Local\SampleMap"的文件映射对象,并将一个字符串写入到文件映射中。
我们将创建两个程序,一个是服务程序,一个是客户程序。服务程序负责创建文件映射。
服务程序命名为CppFileMappingServer,它的执行过程是
1.创建一个特定大小的文件映射对象,名称为“Local\SampleMap”
2.将这个对象的文件视图映射到进程的地址空间,然后向视图中写入字符串。
接下来执行客户程序CppFileMappingClient,它首先打开这个名称为“Local\SampleMap”的文件映射对象。然后把相同的文件映射视图映射到自己的地址空间中。然后从视图中读取服务进程所写入的数据。
Server完整源码:
- #pragma region Includes
- #include <stdio.h>
- #include <windows.h>
- #pragma endregion
- #define MAP_PREFIX L"Local\\"
- #define MAP_NAME L"SampleMap"
- #define FULL_MAP_NAME MAP_PREFIX MAP_NAME
- // Max size of the file mapping object.
- #define MAP_SIZE 65536
- // File offset where the view is to begin.
- #define VIEW_OFFSET 0
- // The number of bytes of a file mapping to map to the view. All bytes of the
- // view must be within the maximum size of the file mapping object (MAP_SIZE).
- // If VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to
- // the end of the file mapping.
- #define VIEW_SIZE 1024
- // Unicode string message to be written to the mapped view. Its size in byte
- // must be less than the view size (VIEW_SIZE).
- #define MESSAGE L"Message from the first process."
- int wmain(int argc, wchar_t* argv[])
- {
- HANDLE hMapFile = NULL;
- PVOID pView = NULL;
- // Create the file mapping object.
- hMapFile = CreateFileMapping(
- INVALID_HANDLE_VALUE, // Use paging file - shared memory
- NULL, // Default security attributes
- PAGE_READWRITE, // Allow read and write access
- 0, // High-order DWORD of file mapping max size
- MAP_SIZE, // Low-order DWORD of file mapping max size
- FULL_MAP_NAME // Name of the file mapping object
- );
- if (hMapFile == NULL)
- {
- wprintf(L"CreateFileMapping failed w/err 0x%08lx\n", GetLastError());
- goto Cleanup;
- }
- wprintf(L"The file mapping (%s) is created\n", FULL_MAP_NAME);
- // Map a view of the file mapping into the address space of the current
- // process.
- pView = MapViewOfFile(
- hMapFile, // Handle of the map object
- FILE_MAP_ALL_ACCESS, // Read and write access
- 0, // High-order DWORD of the file offset
- VIEW_OFFSET, // Low-order DWORD of the file offset
- VIEW_SIZE // The number of bytes to map to view
- );
- if (pView == NULL)
- {
- wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());
- goto Cleanup;
- }
- wprintf(L"The file view is mapped\n");
- // Prepare a message to be written to the view.
- PWSTR pszMessage = MESSAGE;
- DWORD cbMessage = (wcslen(pszMessage) + 1) * sizeof(*pszMessage);
- // Write the message to the view.
- memcpy_s(pView, VIEW_SIZE, pszMessage, cbMessage);
- wprintf(L"This message is written to the view:\n\"%s\"\n",
- pszMessage);
- // Wait to clean up resources and stop the process.
- wprintf(L"Press ENTER to clean up resources and quit");
- getchar();
- Cleanup:
- if (hMapFile)
- {
- if (pView)
- {
- // Unmap the file view.
- UnmapViewOfFile(pView);
- pView = NULL;
- }
- // Close the file mapping object.
- CloseHandle(hMapFile);
- hMapFile = NULL;
- }
- return 0;
- }
Client完整源码
- #pragma region Includes
- #include <stdio.h>
- #include <windows.h>
- #pragma endregion
- #define MAP_PREFIX L"Local\\"
- #define MAP_NAME L"SampleMap"
- #define FULL_MAP_NAME MAP_PREFIX MAP_NAME
- // File offset where the view is to begin.
- #define VIEW_OFFSET 0
- // The number of bytes of a file mapping to map to the view. All bytes of the
- // view must be within the maximum size of the file mapping object. If
- // VIEW_SIZE is 0, the mapping extends from the offset (VIEW_OFFSET) to the
- // end of the file mapping.
- #define VIEW_SIZE 1024
- int wmain(int argc, wchar_t* argv[])
- {
- HANDLE hMapFile = NULL;
- PVOID pView = NULL;
- // Try to open the named file mapping identified by the map name.
- hMapFile = OpenFileMapping(
- FILE_MAP_READ, // Read access
- FALSE, // Do not inherit the name
- FULL_MAP_NAME // File mapping name
- );
- if (hMapFile == NULL)
- {
- wprintf(L"OpenFileMapping failed w/err 0x%08lx\n", GetLastError());
- goto Cleanup;
- }
- wprintf(L"The file mapping (%s) is opened\n", FULL_MAP_NAME);
- // Map a view of the file mapping into the address space of the current
- // process.
- pView = MapViewOfFile(
- hMapFile, // Handle of the map object
- FILE_MAP_READ, // Read access
- 0, // High-order DWORD of the file offset
- VIEW_OFFSET, // Low-order DWORD of the file offset
- VIEW_SIZE // The number of bytes to map to view
- );
- if (pView == NULL)
- {
- wprintf(L"MapViewOfFile failed w/err 0x%08lx\n", GetLastError());
- goto Cleanup;
- }
- wprintf(L"The file view is mapped\n");
- // Read and display the content in view.
- wprintf(L"Read from the file mapping:\n\"%s\"\n", (PWSTR)pView);
- // Wait to clean up resources and stop the process.
- wprintf(L"Press ENTER to clean up resources and quit");
- getchar();
- Cleanup:
- if (hMapFile)
- {
- if (pView)
- {
- // Unmap the file view.
- UnmapViewOfFile(pView);
- pView = NULL;
- }
- // Close the file mapping object.
- CloseHandle(hMapFile);
- hMapFile = NULL;
- }
- return 0;
- }
运行效果:
Server
Client
来源:python脚本自动迁移
Window 共享内存的更多相关文章
- [转]WINDOW进程间数据通讯以及共享内存
1.引言 在Windows程序中,各个进程之间常常需要交换数据,进行数据通讯.WIN32 API提供了许多函数使我们能够方便高效地进行进程间的通讯,通过这些函数我们可以控制不同进程间的数据交换,就如同 ...
- Fresco内存机制(Ashmem匿名共享内存)
Fresco的内存机制 Fresco是Facebook出品的高性能图片加载库,采用了Ashmem匿名共享内存机制, 来解决图片加载中的OOM问题.这里不对Fresco做深入分析,只关注Fresco在A ...
- C扩展 从共享内存shm到memcache外部内存
引言 - ipc - shm 共享内存 本文会通过案例了解ipc 的共享内存机制使用, 后面会讲解C 如何使用外部内存服务memcached. 好先开始了解 linux 共享内存机制. 推荐先参看下面 ...
- Codesys 使用共享内存 打通通讯
Codesys V3.5 平台 提供了库SysShm,其中包含了共享内存操作的接口函数: SysSharedMemoryClose; SysSharedMemoryCreate; SysShare ...
- Linux 共享内存详解一
共享内存段被多个进程附加的时候,如果不是所有进程都已经调用shmdt,那么删除该共享内存段时,会出现一个临时的不完整的共享内存段(key值是0),无法彻底删除.只有当所有进程都调用shmdt,这个临时 ...
- PHP进程通信基础——信号量+共享内存通信
PHP进程通信基础--信号量+共享内存通信 由于进程之间谁先执行并不确定,这取决于内核的进程调度算法,其中比较复杂.由此有可能多进程在相同的时间内同时访问共享内存,从而造成不可预料的错误.信号量这个名 ...
- C++ 共享内存 函数封装
#pragma once #include <string> #include <wtypes.h> #include <map> using namespace ...
- Linux学习笔记(14)-进程通信|共享内存
在Linux中,共享内存是允许两个不相关的进程访问同一个逻辑内存的进程间通信方法,是在两个正在运行的进程之间共享和传递数据的一种非常有效的方式. 不同进程之间共享的内存通常安排为同一段物理内存.进程可 ...
- linux 共享内存 shmat,shmget,shmdt,shmctl
shmget int shmget(key_t key, size_t size, int flag);//开辟一段共享内存 key_t key :标识符的规则() size_t size :共享内存 ...
随机推荐
- c++—通讯录管理系统
一.运用所学的结构体.地址指针等基础知识,完成通讯录管理系统 二.系统主要有以下6个功能: 1.添加联系人2.显示联系人 3.删除联系人 4.查找联系人5.修改联系人 6.清空联系人 1 #inclu ...
- MySQL高级查询与编程笔记 • 【第2章 数据定义和操作】
全部章节 >>>> 本章目录 2.1 数据定义语言和数据操作语言 2.1.1 设计"优乐网"数据库 2.1.2 数据定义语言 2.1.3 数据操作语言 ...
- Java初学者作业——定义英雄类(Hero),英雄类中的属性包括:姓名、攻击力、防御力、生命值和魔法值;方法包括:攻击、介绍。
返回本章节 返回作业目录 需求说明: 定义英雄类(Hero),英雄类中的属性包括:姓名.攻击力.防御力.生命值和魔法值:方法包括:攻击.介绍. 实现思路: 分析类的属性及其变量类型. 分析类的方法及其 ...
- POI导入导出Excel(HSSF格式,User Model方式)
1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...
- nalu,在java中使用lambda查询数据库
不忘初心 最开始接触写代码的时候,用的是C井,查数据库直接硬编码sql,挺难受的. 后来学习到EntityFramework,用起来是真香,都是强类型,各种智能提示,代码写起来极度舒适,效率起飞. 最 ...
- Star Way To Heaven
题目描述 小 x伤心的走上了 Star way to heaven. 到天堂的道路是一个笛卡尔坐标系上一个 n*m的长方形通道 顶点在0,0 和 . 小 n,m 从最左边任意一点进入,从右边任意一点走 ...
- spring boot 使用 mybatis 开启事务回滚 的总结
1.前言 以前没有使用mybatis,可以关闭自动提交,然后做sql操作,对操作进行catch捕获异常, 如果没有异常则commit 提交 ,有异常则 rollback 回滚,新增的数据则删除 ,修改 ...
- sql优化--尽可能少用like
1.前言 like非常消耗性能,当搜索 like '%%' 的时候,仍然会对比全表信息后查找相关的数据, 2.如何优化? 使用动态标签 <if test="nickName != '% ...
- Vue-cli代理解决跨域问题
使用vue-cli调接口的时候,总是会出现垮与问题,因为vue的localhost与访问域名不一致导致.而这一点,开发者显然也想到了,故而在vuejs-templates,也就是vue-cli的使用的 ...
- vue2.0多页面开发
我们平常用vue开发的时候总觉得vue好像就是专门为了单页面应用而诞生的,其实不是.因为vue在工程化开发的时候很依赖webpack,而webpack是将所有的资源整合到一块,弄成一个单页面.但是vu ...