C++进程间共享数据实例
本文实例讲述了C++进程间共享数据的实现方法,分享给大家供大家参考。具体实现方法如下:
intmain(intargc,char*argv[]) { //RecursiveDelete("C:\\20_128\\"); //SelfRun("runModel"); //进程间内存共享 LPCTSTRlpName="hello"; LPCTSTRlpContent="123456"; HANDLEhMap=::OpenFileMapping(FILE_MAP_ALL_ACCESS,FALSE,lpName); if(NULL==hMap)//打开失败 { //创建 hMap=::CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_EXECUTE_READWRITE,0,strlen(lpContent),lpName); if(hMap==NULL) { return-1; } //写入 LPVOIDlpBase=::MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0); strcpy((char*)lpBase,lpContent); printf("write%s\n",lpContent); } else { //打开成功就只读取 LPVOIDlpBase=::MapViewOfFile(hMap,FILE_MAP_ALL_ACCESS,0,0,0); if(lpBase==NULL) { return-1; } printf("read%s\n",(char*)lpBase); } getchar(); return0; }
希望本文所述对大家的C++程序设计有所帮助。