通过内存映射实现进程间通信
2009-02-25 11:16
295 查看
send.cpp
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc,char **argv){
HANDLE hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
"ShareFile"
);
if(hMapFile == NULL){
cout << "获取内存映射文件失败" << endl;
return 0;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
cout << "内存映射文件申请失败" << endl;
return 0;
}
cout << (char *)lpMapAddress << endl;
UnmapViewOfFile(lpMapAddress);
return 0;
}
recv.cpp
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc,char **argv){
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
4*1024,
"ShareFile"
);
if(hMapFile == NULL){
cout << "分配内存空间出错" << endl;
return 0;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
cout << "申请内存失败" << endl;
return 0;
}
char buf[4096];
cin >> buf;
lstrcpy((char*)lpMapAddress,buf);
int i = 0;
here:
cin >> i;
if(i == 0){
goto here;
}
UnmapViewOfFile(lpMapAddress);
return 0;
}
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc,char **argv){
HANDLE hMapFile = OpenFileMapping(
FILE_MAP_ALL_ACCESS,
false,
"ShareFile"
);
if(hMapFile == NULL){
cout << "获取内存映射文件失败" << endl;
return 0;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
cout << "内存映射文件申请失败" << endl;
return 0;
}
cout << (char *)lpMapAddress << endl;
UnmapViewOfFile(lpMapAddress);
return 0;
}
recv.cpp
#include <stdio.h>
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc,char **argv){
HANDLE hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE,
NULL,
PAGE_READWRITE,
0,
4*1024,
"ShareFile"
);
if(hMapFile == NULL){
cout << "分配内存空间出错" << endl;
return 0;
}
LPVOID lpMapAddress = MapViewOfFile(
hMapFile,
FILE_MAP_ALL_ACCESS,
0,
0,
0
);
if(lpMapAddress == NULL){
cout << "申请内存失败" << endl;
return 0;
}
char buf[4096];
cin >> buf;
lstrcpy((char*)lpMapAddress,buf);
int i = 0;
here:
cin >> i;
if(i == 0){
goto here;
}
UnmapViewOfFile(lpMapAddress);
return 0;
}
相关文章推荐
- 通过内存映射实现进程间通信
- UNIX环境高级编程学习之第十五章进程间通信 - 两个进程通过映射普通文件实现共享内存通信
- 利用内存映射实现进程间通信
- Java NIO 应用 -- 使用内存映射文件实现进程间通信
- 通过内存映射文件实现进程间数据共享
- 内存映射文件与内存读写实现进程间通信的比较
- 总结:内存映射文件与内存读写实现进程间通信的比较
- Java NIO 内存映射文件实现进程间通信
- Linux下通过共享内存进行进程间通信,进程间同步使用信号量来实现
- Java NIO 应用 -- 使用内存映射文件实现进程间通信
- 通过内存映射实现进程间数据交换
- Java NIO 内存映射文件实现进程间通信
- Linux下通过共享内存进行进程间通信,进程间同步使用信号量来实现
- java nio 使用内存映射文件实现进程间通信
- linux基础编程 共享内存 通过消息队列实现同步 shmget
- unity3D MiniMap等比例映射的实现(二) 通过RawImage UV信息控制小地图的移动
- 通过短信实现对服务器的性能(内存、CPU、硬盘空间等)指标的查看
- 总结Unity IOC容器通过配置实现类型映射的几种基本使用方法
- Android 通过软引用实现图片缓存,防止内存溢出
- 通过SSH实现 端口映射