概述
命名共享内存,在windows下主要是使用windows提供的几个API函数,创建、打开、读取、销毁命名共享内存,共享内存比较适合有消息服务者和消息消费者的场景,优点是效率高,适合较多信息的共享,缺点是不太适合双方需要频繁沟通信息的场景。
以下程序分为两部分,一个是负责生产消息的消息服务进程,一个是负责消费消息的消息消费进程;前者创建指定名称的命名共享内存,并将服务消息写入共享内存中,后者打开指定名称的命名共享内存,并将消息读出打印
本文主要内容来自微软官方文档:创建命名共享内存
消息服务端
服务端程序工作过程
服务进程使用CreateFileMapping接口创建命名共享内存。向命名共享内存中写入数据需要调用MapViewOfFile接口, MapViewOfFile函数返回一个指向共享内存的指针pBuf。最后,该进程使用CopyMemory函数将一个字符串数据写入共享内存中。
直接看微软的实例代码即可:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#define BUF_SIZE 256 // 描述共享内存大小
TCHAR szName[]=TEXT("Global\\MyFileMappingObject"); // global标识所有windows用户开启的进程间都可以相互访问到这个内存,需要管理员权限,如果没有这个要求,可以不要global标识
TCHAR szMsg[]=TEXT("Message from first process."); // 数据
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // maximum object size (high-order DWORD)
BUF_SIZE, // maximum object size (low-order DWORD)
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not create file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
_getch();
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}
消息消息端
消费端程序工作过程
消费进程通过调用OpenFileMapping函数,该函数指定由服务进程创建时选定的名称,调用返回一个内存映射的句柄。使用该句柄通过调用MapViewOfFile获取到一个指向共享内存的指针,从该之阵中可以读取共享内存中的数据
具体代码如下:
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#pragma comment(lib, "user32.lib")
#define BUF_SIZE 256
TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
int _tmain()
{
HANDLE hMapFile;
LPCTSTR pBuf;
hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("Could not open file mapping object (%d).\n"),
GetLastError());
return 1;
}
pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
BUF_SIZE);
if (pBuf == NULL)
{
_tprintf(TEXT("Could not map view of file (%d).\n"),
GetLastError());
CloseHandle(hMapFile);
return 1;
}
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
UnmapViewOfFile(pBuf);
CloseHandle(hMapFile);
return 0;
}