VS C++ 共享内存 CreateFileMapping

xiaoxiao2021-03-01  14

创建共享内存 HANDLE CreateFileMapping(   HANDLE hFile,              // handle to file to map   LPSECURITY_ATTRIBUTES lpFileMappingAttributes, // optional security attributes, 是否被继承   DWORD flProtect,           // protection for mapping object   DWORD dwMaximumSizeHigh,   // high-order 32 bits of object size   DWORD dwMaximumSizeLow,    // low-order 32 bits of object size   LPCTSTR lpName             // name of file-mapping object ); hFile // (HANDLE)0xFFFFFFFF 不指定共享内存句柄 lpFileMappingAttributes // 是否被继承 flProtect // 读写权限 dwMaximumSizeHigh //  dwMaximumSizeLow // 共享内存大小 lpName // 名字 返回值: 成功:共享内存句柄

失败:GetLastError function returns ERROR_ALREADY_EXISTS;或 NULL。

映射共享内存地址空间LPVOID MapViewOfFile(  HANDLE hFileMappingObject,  // file-mapping object to map into address space  DWORD dwDesiredAccess,      // access mode  DWORD dwFileOffsetHigh,     // high-order 32 bits of file offset  DWORD dwFileOffsetLow,      // low-order 32 bits of file offset  DWORD dwNumberOfBytesToMap  // number of bytes to map);hFileMappingObject : CreateFileMapping 和 OpenFileMapping 返回的句柄dwDesiredAccess : 存取模式返回值:成功:返回映射的地址

失败:GetLastError function returns ERROR_ALREADY_EXISTS;或 NULL。

解除映射BOOL UnmapViewOfFile(  LPCVOID lpBaseAddress   // address where mapped view begins ,MapViewOfFile 的句柄 );返回值:成功:返回非0

失败:返回0

关闭打开的句柄BOOL CloseHandle(  HANDLE hObject   // handle to object to close

);

例程  创建共享内存

#include <Windows.h> #include <cstdio> static HANDLE hShareMem = INVALID_HANDLE_VALUE; static void * pRawShareAddr = NULL; int main(int argc, char **argv) { int nMemSize = 1024; hShareMem = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL); if( (hShareMem != INVALID_HANDLE_VALUE) && (hShareMem != NULL) ) { pRawShareAddr = (void *)MapViewOfFile(hShareMem, FILE_MAP_ALL_ACCESS, 0, 0, 0); memset(pRawShareAddr, 0, nMemSize); // 清空数据 } UnmapViewOfFile(pRawShareAddr); // 解除映射 pRawShareAddr=NULL; CloseHandle(hShareMem); // 关闭句柄 hShareMem=INVALID_HANDLE_VALUE; getchar(); return 0; } 打开一个有名的共享内存 HANDLE OpenFileMapping(   DWORD dwDesiredAccess,  // access mode BOOL bInheritHandle,    // inherit flag LPCTSTR lpName          // pointer to name of file-mapping object );  返回值: 成功:返回打开 lpName 名字的共享内存地址

失败:GetLastError function returns ERROR_ALREADY_EXISTS;或 NULL。

例程  获取共享内存

#include <Windows.h> #include <cstdio> static HANDLE hShareMem = INVALID_HANDLE_VALUE; static void * pRawShareAddr = NULL; int main(int argc, char **argv) { int nMemSize = 1024; char sShareMemName[] = "shareMemName"; hShareMem = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, (LPCTSTR)sShareMemName); if( (hShareMem != INVALID_HANDLE_VALUE) && (hShareMem != NULL) ) { pRawShareAddr = (void *)MapViewOfFile(hShareMem, FILE_MAP_ALL_ACCESS, 0, 0, 0); if (pRawShareAddr != NULL) { unsigned long long tempaddr=(unsigned long long)pRawShareAddr; printf("get_share_mem: windows shm addr=0xx\r\n",tempaddr); } } UnmapViewOfFile(pRawShareAddr); // 解除映射 pRawShareAddr=NULL; CloseHandle(hShareMem); // 关闭句柄 hShareMem=INVALID_HANDLE_VALUE; getchar(); return 0; }
转载请注明原文地址: https://www.6miu.com/read-3100062.html

最新回复(0)