简单内存映射
2012-11-01 09:29 龙成 阅读(175) 评论(0) 编辑 收藏 举报//process1 #include <windows.h> #include <iostream> using namespace std; int main() { cout << "server" << endl; HANDLE hEvent; HANDLE hFileMap = NULL; char *p = '\0'; hEvent = CreateEvent(NULL, false, false, "MyEvent"); if(hEvent == NULL) { printf("createEvent failed\n"); return 0; } hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,BUFSIZ,"haha"); if(hFileMap == NULL) { cout << "hFileMap create wrong" << endl; return 0; } p = (char*)MapViewOfFile(hFileMap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0); if(p == NULL) { cout << "p create wrong" << endl; return 0; } cin >> p; UnmapViewOfFile(p); CloseHandle(hFileMap); SetEvent(hEvent); CloseHandle(hEvent); return 0; } //process2 #include <windows.h> #include <iostream> using namespace std; int main() { HANDLE hEvent; HANDLE hFilemap = NULL; char *p; hEvent = CreateEvent(NULL, false, false, "MyEvent"); if(hEvent == NULL) { return 0; } hFilemap = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,BUFSIZ,"haha"); if(hFilemap == NULL) { cout << "hFileMap create failed" << endl; return 0; } p = (char*)MapViewOfFile(hFilemap,FILE_MAP_READ|FILE_MAP_WRITE,0,0,0); WaitForSingleObject(hEvent, INFINITE); cout << p << endl; UnmapViewOfFile(p); CloseHandle(hFilemap); CloseHandle(hEvent); getchar(); return 0; }