C++ 共享内存 函数封装

时间:2021-10-14 04:01:04
 1 #pragma once
 2 #include <string>
 3 #include <wtypes.h>
 4 #include <map>
 5 
 6 using namespace std;
 7 
 8 #define  MEMORYSIZE 102400000 //每块内存的大小
 9 
10 struct MemoryAddr{
11     string StrName;
12     LPVOID pBuffer;    
13     HANDLE m_Handle;
14 };
15 
16 class SharedMemory
17 {
18 public:
19     SharedMemory(void);
20     ~SharedMemory(void);
21 public:
22     string strMapName;                // 内存映射对象名称
23     string strComData;        // 共享内存中的数据
24     int MemoeryNum; //开辟内存数
25 
26     map<int,MemoryAddr>MemoryInfo;
27 
28     LPVOID Init(string MeName);
29     void   IntPutData(string MeName,string str);
30     string OutPutData(string MeName);
31     string GetAllData();
32 };
 1 #include "StdAfx.h"
 2 #include "SharedMemory.h"
 3 
 4 
 5 SharedMemory::SharedMemory(void)
 6 {
 7     MemoeryNum=0;
 8 }
 9 
10 SharedMemory::~SharedMemory(void)
11 {
12     map<int,MemoryAddr>::iterator it;
13 
14     for (it=MemoryInfo.begin();it!=MemoryInfo.end();it++)
15     {        
16         ::UnmapViewOfFile(it->second.pBuffer);
17         ::CloseHandle(it->second.m_Handle);
18     }
19 
20     MemoryInfo.clear();
21 }
22 
23 LPVOID SharedMemory::Init( string MeName)
24 {
25     MemoryAddr mMemoryAddr;
26     LPVOID pBuffer;
27     // 首先试图打开一个命名的内存映射文件对象  
28     HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, 0, MeName.c_str());
29 
30     if (NULL == hMap)
31     {    // 打开失败,创建之
32         hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,MEMORYSIZE,MeName.c_str());
33         // 映射对象的一个视图,得到指向共享内存的指针,设置里面的数据
34         pBuffer = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
35     }
36     else
37     {    // 打开成功,映射对象的一个视图,得到指向共享内存的指针,显示出里面的数据
38         pBuffer = ::MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
39     }
40     
41     mMemoryAddr.StrName=MeName.c_str();
42     mMemoryAddr.pBuffer=pBuffer;
43     mMemoryAddr.m_Handle=hMap;
44 
45     MemoryInfo.insert(pair<int,MemoryAddr>(MemoeryNum,mMemoryAddr));
46 
47     MemoeryNum++;
48 
49     return pBuffer;
50 }
51 
52 void SharedMemory::IntPutData(string MeName, string str )
53 {
54     map<int,MemoryAddr>::iterator itr;
55     for (itr=MemoryInfo.begin();itr!=MemoryInfo.end();itr++)
56     {
57         if (MeName==itr->second.StrName)
58         {
59             strcpy((char*)itr->second.pBuffer, str.c_str());
60         }
61     }
62 }
63 
64 std::string SharedMemory::OutPutData(string MeName)
65 {
66     map<int,MemoryAddr>::iterator iter;
67 
68     for (iter=MemoryInfo.begin();iter!=MemoryInfo.end();iter++)
69     {
70         if (iter->second.StrName==MeName)
71         {
72             return (char*)iter->second.pBuffer;
73         }
74     }
75 }
76 
77 std::string SharedMemory::GetAllData()
78 {
79     string str="";
80     map<int,MemoryAddr>::iterator ir;
81     for (ir=MemoryInfo.begin();ir!=MemoryInfo.end();ir++)
82     {
83         string strTemp="";
84         strTemp.append(ir->second.StrName);
85         strTemp.append(":");
86         strTemp.append((char*)ir->second.pBuffer);
87         str.append(strTemp.c_str());
88         str.append(",");
89     }
90     return str;
91 }