C++进程共享数据封装成类实例
2019/7/10 23:11:16
本文主要是介绍C++进程共享数据封装成类实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了C++进程共享数据封装成类的方法,分享给大家供大家参考。具体方法如下:
ShareMemory.cpp源文件如下:
复制代码 代码如下:
#include "ShareMemory.h"
CShareMemory::CShareMemory(const char* pszMapName, int nFileSize, BOOL bServer):m_hFileMap(NULL),m_pBuffer(NULL)
{
if (bServer) //是服务端,就创建
{
m_hFileMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, nFileSize, pszMapName);
}
else //否则就打开
{
m_hFileMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, pszMapName);
}
//把它映射到内存
m_pBuffer = ::MapViewOfFile(m_hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
}
CShareMemory::~CShareMemory(void)
{
::UnmapViewOfFile(m_pBuffer);
::CloseHandle(m_hFileMap);
}
CShareMemory::CShareMemory(const char* pszMapName, int nFileSize, BOOL bServer):m_hFileMap(NULL),m_pBuffer(NULL)
{
if (bServer) //是服务端,就创建
{
m_hFileMap = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, 0, nFileSize, pszMapName);
}
else //否则就打开
{
m_hFileMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, pszMapName);
}
//把它映射到内存
m_pBuffer = ::MapViewOfFile(m_hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);
}
CShareMemory::~CShareMemory(void)
{
::UnmapViewOfFile(m_pBuffer);
::CloseHandle(m_hFileMap);
}
ShareMemory.h头文件如下:
复制代码 代码如下:
#pragma once
#include <Windows.h>
class CShareMemory
{
public:
CShareMemory(const char* pszMapName, int nFileSize=0, BOOL bServer=FALSE);
~CShareMemory(void);
//属性
LPVOID GetBuffer() const
{
return m_pBuffer;
}
//实现
private:
HANDLE m_hFileMap;
LPVOID m_pBuffer;
};
#include <Windows.h>
class CShareMemory
{
public:
CShareMemory(const char* pszMapName, int nFileSize=0, BOOL bServer=FALSE);
~CShareMemory(void);
//属性
LPVOID GetBuffer() const
{
return m_pBuffer;
}
//实现
private:
HANDLE m_hFileMap;
LPVOID m_pBuffer;
};
使用时用法如下:
服务端:
复制代码 代码如下:
#include "ShareMemory.h"
int main(int argc, char *argv[])
{
//进程间内存共享
LPCTSTR lpName= "hello";
LPCTSTR lpContent = "123456";
CShareMemory* pShareMemory = new CShareMemory(lpName, 20, FALSE);
LPVOID lpBase = pShareMemory->GetBuffer();
strcpy((char*)lpBase, lpContent);
printf("%s\n",(char*)pShareMemory->GetBuffer());
getchar();
return 0;
}
int main(int argc, char *argv[])
{
//进程间内存共享
LPCTSTR lpName= "hello";
LPCTSTR lpContent = "123456";
CShareMemory* pShareMemory = new CShareMemory(lpName, 20, FALSE);
LPVOID lpBase = pShareMemory->GetBuffer();
strcpy((char*)lpBase, lpContent);
printf("%s\n",(char*)pShareMemory->GetBuffer());
getchar();
return 0;
}
客户端:
复制代码 代码如下:
#include "ShareMemory.h"
int main(int argc, char *argv[])
{
//进程间内存共享
LPCTSTR lpName= "hello";
LPCTSTR lpContent = "123456";
CShareMemory* pShareMemory = new CShareMemory(lpName, 20, FALSE);
printf("%s\n",(char*)pShareMemory->GetBuffer());
getchar();
return 0;
}
int main(int argc, char *argv[])
{
//进程间内存共享
LPCTSTR lpName= "hello";
LPCTSTR lpContent = "123456";
CShareMemory* pShareMemory = new CShareMemory(lpName, 20, FALSE);
printf("%s\n",(char*)pShareMemory->GetBuffer());
getchar();
return 0;
}
希望本文所述对大家的C++程序设计有所帮助。
这篇关于C++进程共享数据封装成类实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享