C++共享内存类封装

  • 共享内存创建步骤
    ** 1.CreateFileMapping(创建一块内存)
    ** 2.MapViewOfFile(获取内存中的位置)
    ** 3.strcpy 在内存地址中写入数据
    ** 4.使用结束后,关闭销毁。。。
    头文件
#include <stdio.h>
#include <tchar.h>

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
  • 类声明文件
#include "stdafx.h"
//共享内存类的封装
enum USERTYPE{
	USER_CLIENT, //客户端
	USER_SERVER, //服务端
};

class ShareMemory{
public:
	ShareMemory();
	~ShareMemory();

	bool InitShareMemory(std::wstring share_name,USERTYPE user_type,int data_size = 4096);//初始化
	bool WriteData(std::string datas); //写入数据统一使用std::string格式
	bool ReadData(std::string&read_datas);//读取数据
	bool CloseShareMemory();//提前关闭
	static ShareMemory* getInstance(); //获取实例指针 避免
	static ShareMemory* m_instance;
private:
	HANDLE m_hMapFile; //文件句柄
	int m_read_size;//读取大小
	LPVOID m_lpBase;

};
  • 类实现文件
#include "stdafx.h"
#include "ShareMemory.h"
using namespace std;

#define BUF_SIZE 4096

ShareMemory* ShareMemory::m_instance = nullptr;

ShareMemory* ShareMemory::getInstance(){
	if (m_instance==nullptr)
	{
		m_instance = new ShareMemory();
		if (m_instance)
		{
			return m_instance;
		}
	}
	return m_instance;
}

ShareMemory::ShareMemory()
{
	m_hMapFile = nullptr;
	m_read_size = 0;
	m_lpBase = nullptr;
}

ShareMemory::~ShareMemory()
{
	if (m_hMapFile!=nullptr)
	{
		//关闭 防止内存泄漏
		UnmapViewOfFile(m_lpBase);//解除文件映射
		CloseHandle(m_hMapFile); //关闭句柄
	}
}

//初始化共享内存模块
//@params:share_name:内存名:
//@params:user_type:选择客户端或者服务端
//@data_size:默认4096 为创建多大的内存存放数据
bool ShareMemory::InitShareMemory(std::wstring share_name,USERTYPE user_type,int data_size)
{
	m_read_size = data_size;
	//判断用户类型
	if (user_type == USER_SERVER)
	{
		HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,data_size,share_name.c_str());
		if (hMapFile == nullptr)
		{
			cout << "服务端初始化共享内存失败" << endl;
			return false;
		}
		m_hMapFile = hMapFile;
	}
	else
	{
		HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, NULL, share_name.c_str());
		if (hMapFile == nullptr)
		{
			cout << "客户端初始化共享内存失败" << endl;
			return false;
		}
		m_hMapFile = hMapFile;
	}
	return true;
}

//写入数据
//datas:需要写入的数据
bool ShareMemory::WriteData(std::string datas)
{
	LPVOID lpBase = MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0); //获取共享内存的地址
	if (lpBase==nullptr)
	{
		return false;
	}
	m_lpBase = lpBase;
	strcpy((char*)m_lpBase,datas.c_str()); //写入数据
	return true;
}

//读取数据
//read_datas:读出的数据返回到这
bool ShareMemory::ReadData(std::string&read_datas)
{
	LPVOID lpBase = MapViewOfFile(m_hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0); //获取共享内存的地址
	if (lpBase==nullptr)
	{
		return false;
	}
	m_lpBase = lpBase;
	char szBuffer[BUF_SIZE] = {0};
	strcpy(szBuffer,(char*)m_lpBase); //读出数据
	read_datas = szBuffer; //转到read_data;
	return true;
}

bool ShareMemory::CloseShareMemory()
{
	if (m_hMapFile!=nullptr)
	{
		//关闭 防止内存泄漏
		UnmapViewOfFile(m_lpBase);//解除文件映射
		CloseHandle(m_hMapFile); //关闭句柄
	}
	return true;
}
  • 调用方法
int _tmain(int argc, _TCHAR* argv[])
{
	unique_ptr<ShareMemory>sm(new ShareMemory());
	std::wstring demo_name = L"ff";
	sm->InitShareMemory(demo_name,USER_SERVER);
	std::string info_str = "twice";
	sm->WriteData(info_str);
	system("pause");
	return 0;
}

  • 具体说明和优化 回去写
发布了365 篇原创文章 · 获赞 80 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/Giser_D/article/details/103930881