博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
不同进程通过共享内存实现数据共享
阅读量:4299 次
发布时间:2019-05-27

本文共 1268 字,大约阅读时间需要 4 分钟。

第1步: 新建控制台工程,主进程代码如下

#include "stdafx.h"#include "windows.h"int _tmain(int argc, _TCHAR* argv[]){    wchar_t MemShareName[] = L"MemShareForTest";    LPVOID pMemShare;    int data = 10;    //HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,0,MemShareName);    HANDLE hMap = ::CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,128,MemShareName);  // hu 空间大小为Bytes    pMemShare = ::MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0);    memcpy((int*)pMemShare,&data,sizeof(data));    printf("%x:%d\n",(int*)pMemShare,*(int*)pMemShare);    data++;    memcpy((int*)pMemShare+1,&data,sizeof(data));    printf("%x:%d\n",(int*)pMemShare,*(int*)pMemShare);    printf("%x:%d\n",(int*)pMemShare+1,*((int*)pMemShare+1));    getchar();    ::UnmapViewOfFile(pMemShare);    ::CloseHandle(hMap);    return 0;}

第2步: 新建控制台工程,从进程代码如下

#include "stdafx.h"#include "windows.h"int _tmain(int argc, _TCHAR* argv[]){    wchar_t MemShareName[] = L"MemShareForTest";    LPVOID pMemShare;    HANDLE hMap = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,0,MemShareName);    pMemShare = ::MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0);    printf("%x:%d\n",(int*)pMemShare,*(int*)pMemShare);    printf("%x:%d\n",(int*)pMemShare+1,*((int*)pMemShare+1));    getchar();    return 0;}

第3步: 依次运行主从进程,结果如下

这里写图片描述

你可能感兴趣的文章
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Mac 下docker路径 /var/lib/docker不存在问题
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(二) 基础命令
查看>>
Docker(三) 构建镜像
查看>>
Spring 全家桶注解一览
查看>>
JDK1.8-Stream API使用
查看>>
cant connect to local MySQL server through socket /tmp/mysql.sock (2)
查看>>
vue中的状态管理 vuex store
查看>>
Maven之阿里云镜像仓库配置
查看>>
Maven:mirror和repository 区别
查看>>
微服务网关 Spring Cloud Gateway
查看>>
SpringCloud Feign的使用方式(一)
查看>>
SpringCloud Feign的使用方式(二)
查看>>