首页 > 系统相关 >C++使用共享内存实现进程间通信

C++使用共享内存实现进程间通信

时间:2024-10-18 23:21:56浏览次数:3  
标签:hMapFile MAP mapping C++ 间通信 pView file 共享内存 view

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

标签:hMapFile,MAP,mapping,C++,间通信,pView,file,共享内存,view
From: https://blog.csdn.net/2401_87429224/article/details/143064098

相关文章

  • 算法笔记 C/C++快速入门 | 全章节整理
    目录零.【C语言中的输入和输出函数】sscanf应用场景1:解析用户输入应用场景2:解析文件内容应用场景3:处理网络协议数据应用场景4:字符串解析和数据转换应用场景5:解析复杂的日志数据其他应用场景:scanf 一【编程语言相关】c和cpp二.【数据结构相关】结构体循环定......
  • C++之类和对象2
    文章目录1.类的默认成员函数有哪些2.构造函数3.析构函数4.拷贝构造函数5.赋值运算符重载函数5.1运算符重载5.2赋值运算符重载函数6.取地址运算符重载函数6.1const成员函数6.2普通取地址重载函数与const取地址运算符重载函数1.类的默认成员函数有哪些默认构造函数是......
  • 【C++】string类(1)
    ......
  • 【GESP】C++一级练习BCQM3048,顺序执行、逆推运算
    一道根据结果,一步一步逆推之前结果的题,完全顺序语句执行题目题解详见:https://www.coderli.com/gesp-1-bcqm3048/【GESP】C++一级练习BCQM3048,顺序执行、逆推运算|OneCoder一道根据结果,一步一步逆推之前结果的题,完全顺序语句执行。https://www.coderli.com/gesp-1-bcqm3048/......
  • 【GESP】C++一级练习BCQM3049,细胞分裂
    GESP一级知识点整形int和for循环练习。题目题解详见:【GESP】C++一级练习BCQM3049,细胞分裂|OneCoder【GESP】C++一级练习BCQM3049,细胞分裂|OneCoderGESP一级知识点整形int和for循环练习。https://www.coderli.com/gesp-1-bcqm3049/C++GESP专项交流频道:GESP学习交流频道......
  • 【信奥赛·C++基础语法】CSP-J C++ 指针与引用
    序言指针和引用是非常重要的概念,它们提供了对内存的直接访问和操作方式,使得程序员能够更加灵活地处理数据哈,理解指针和引用的工作原理以及正确使用它们,对于编写高效、安全的C++程序至关重要。一、指针的基本概念指针的定义和作用指针是一个变量,它存储了另一个变量的内......
  • QT/c++相关记录
     QT的大部分容器类(如QString、QVector等)都是使用隐式共享(implicitsharing)技术,这是通过写时复制(copy-on-write,COW)实现的优化模式。理解这一点的关键在于,Qt的容器类需要在对象拷贝时高效处理数据,而隐式共享则允许在栈上操作容器的同时,在需要时共享内部数据的堆上存储。......
  • 必学的简单排序算法——选择排序(c++)
    标题前言一、什么是选择排序二、算法图解三、经典例题1、颜色分类题解思路代码题解2、至少是其他数字两倍的最大数解题思路代码题解3、寻找两个正序数组的中位数解题思路代码题解前言排序算法虽然简单,但是我也要掌握熟练应用,因为学习算法这个复杂的过程,我们应该......
  • C++ -string -常见用法2
    博客主页:【夜泉_ly】本文专栏:【C++】欢迎点赞......
  • C++ 基础-面试题01(C和C++区别、C结构体和C++结构体区别、C和C++ static区别、a和&a区
    1.C和C++的区别特性CC++编程范式面向过程编程面向对象编程+面向过程编程+泛型编程类和对象不支持类和对象支持类和对象,封装、继承、多态等特性标准库标准库有限,如stdio.h、stdlib.h丰富的标准库,如STL(容器、算法)函数和运算符重载不支持支持内存管理手动管理,使用malloc......