您的位置:首页 > 其它

通过内存映射实现进程间通信

2009-02-20 10:11 148 查看
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;


}

本文出自 “生活成就技术” 博客,请务必保留此出处http://fireflyliu.blog.51cto.com/669730/131915
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: