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脚本自动迁移
最新文章
- ExtJS4插件EditArea
- Java虚拟机详解03----常用JVM配置参数
- [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表
- redis配置文件解析
- 使ViewStub 来提高UI的加载的性能
- splay学习笔记
- Unity3d 获取屏幕depth与normal
- 不是技术牛人,如何拿到国内IT巨头的Offer(1)
- 【浏览器那些基础】Android平台有那些CPU类型
- iOS开发常识
- leetcode dfs Validate Binary Search Tree
- iOS获取程序运行平台
- 《shell脚本学习指南》学习笔记之入门
- 用js来实现那些数据结构—目录
- php原生代码实现explode函数功能
- 修改openstack用户配额
- Django之模板
- 在MySQL中快速的插入大量测试数据
- 【LOJ#6374】网格(二项式反演,容斥)
- 号称简明实用的django上手教程
热门文章
- VUE3 之 循环渲染
- 编写Java程序,模拟教练员和运动员出国比赛场景,其中运动员包括乒乓球运动员和篮球运动员。教练员包括乒乓球教练和篮球教练。为了方便出国交流,根乒乓球相关的人员都需要学习英语。
- 《手把手教你》系列技巧篇(五十三)-java+ selenium自动化测试-上传文件-上篇(详细教程)
- 原型模式(python)
- 第10组 Beta冲刺 (4/5)(组长)
- openlayers素材网站
- Spring循环依赖原理
- 若依(ruoyi)代码生成树表结构的那些坑
- 01-JS中字面量与变量
- 《剑指offer》面试题09. 用两个栈实现队列