send.cpp
InBlock.gif#include <stdio.h>
InBlock.gif#include <iostream>
InBlock.gif#include <windows.h>
InBlock.gif
InBlock.gif using namespace std;
InBlock.gif
InBlock.gif int main( int argc, char **argv){
InBlock.gif  HANDLE hMapFile = OpenFileMapping(
InBlock.gif      FILE_MAP_ALL_ACCESS,
InBlock.gif       false,
InBlock.gif       "ShareFile"
InBlock.gif      );
InBlock.gif   if(hMapFile == NULL){
InBlock.gif    cout << "获取内存映射文件失败" << endl;
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif  LPVOID lpMapAddress = MapViewOfFile(
InBlock.gif      hMapFile,
InBlock.gif      FILE_MAP_ALL_ACCESS,
InBlock.gif      0,
InBlock.gif      0,
InBlock.gif      0
InBlock.gif      );
InBlock.gif   if(lpMapAddress == NULL){
InBlock.gif    cout << "内存映射文件申请失败" << endl;
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif
InBlock.gif  cout << ( char *)lpMapAddress << endl;
InBlock.gif  UnmapViewOfFile(lpMapAddress);
InBlock.gif   return 0;
InBlock.gif}
recv.cpp
 
InBlock.gif#include <stdio.h>
InBlock.gif#include <iostream>
InBlock.gif#include <windows.h>
InBlock.gif
InBlock.gif using namespace std;
InBlock.gif int main( int argc, char **argv){
InBlock.gif  HANDLE hMapFile = CreateFileMapping(
InBlock.gif      INVALID_HANDLE_VALUE,
InBlock.gif      NULL,
InBlock.gif      PAGE_READWRITE,
InBlock.gif      0,
InBlock.gif      4*1024,
InBlock.gif       "ShareFile"
InBlock.gif      );
InBlock.gif   if(hMapFile == NULL){
InBlock.gif    cout << "分配内存空间出错" << endl;
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif
InBlock.gif  LPVOID lpMapAddress = MapViewOfFile(
InBlock.gif      hMapFile,
InBlock.gif      FILE_MAP_ALL_ACCESS,
InBlock.gif      0,
InBlock.gif      0,
InBlock.gif      0
InBlock.gif      );
InBlock.gif   if(lpMapAddress ==    NULL){
InBlock.gif    cout << "申请内存失败" << endl;
InBlock.gif     return 0;
InBlock.gif  }
InBlock.gif   char buf[4096];
InBlock.gif  cin >> buf;
InBlock.gif  lstrcpy(( char*)lpMapAddress,buf);
InBlock.gif   int i = 0;
InBlock.gifhere:
InBlock.gif  cin >> i;
InBlock.gif   if(i == 0){
InBlock.gif     goto here;    
InBlock.gif  }
InBlock.gif  UnmapViewOfFile(lpMapAddress);
InBlock.gif   return 0;
InBlock.gif}