使用Win32控制台实现boost共享内存通信

发送端:

#define BOOST_DATE_TIME_NO_LIB 
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
using namespace std;


int main()
{
	//1 先删除之前创建的
	shared_memory_object::remove("MySharedMemory_Name");

	//2 创建共享内存段
	shared_memory_object shm(create_only, "MySharedMemory_Name", read_write);

	//3 设置共享内存大小
	shm.truncate(100);

	//4 映射共享内存片段
	mapped_region region(shm, read_write);

	//5 初始化为0
	std::memset(region.get_address(), 0, region.get_size());

	//6 往内存里写入数据
	string *strTest = static_cast<string*>(region.get_address());
	*strTest = "hello world";

	cout << "发送完成" << endl;
	getchar();
	return 0;
}

接收端:

#define BOOST_DATE_TIME_NO_LIB 
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <iostream>
using namespace boost::interprocess;
using namespace std;


int main()
{
	//1 读取共享内存
	shared_memory_object shm(open_only, "MySharedMemory_Name", read_only);

	//2 映射共享内存
	mapped_region region(shm, read_only);

	//3 获取数据
	string* strRead = static_cast<string*>(region.get_address());

	cout << *strRead << endl;
	getchar();
	return 0;
}

 参考链接:

https://blog.csdn.net/u014385680/article/details/104682269

使用Win32控制台实现boost共享内存通信

上一篇:Photoshop把照片变得高清晰的技法


下一篇:WPF 按钮中有圆形图形的界面设计