0x01 核心代码
发送端代码: HANDLE hMapObject; HANDLE hMapView; //创建FileMapping对象 hMapObject = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,TEXT("shared")); if(!hMapObject) { MessageBox(NULL,TEXT("共享内存失败"),TEXT("Error"),MB_OK); return FALSE; } //将FileMapping对象映射到自己的进程 hMapView = MapViewOfFile(hMapObject,FILE_MAP_WRITE,0,0,0); if(!hMapView) { MessageBox(NULL,TEXT("内存映射失败"),TEXT("Error"),MB_OK); return FALSE; } //向共享内存写入数据 strcpy((char*)hMapView,"Test Shared Memery"); 接收端代码: HANDLE hMapObject; HANDLE hMapView; //创建FileMapping对象 hMapObject = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0,0x1000,TEXT("shared")); if(!hMapObject) { MessageBox(NULL,TEXT("共享内存失败"),TEXT("Error"),MB_OK); return FALSE; } //将FileMapping对象映射到自己的进程 hMapView = MapViewOfFile(hMapObject,FILE_MAP_WRITE,0,0,0); if(!hMapView) { MessageBox(NULL,TEXT("内存映射失败"),TEXT("Error"),MB_OK); return FALSE; } //从共享内存读取数据 TCHAR szBuffer[0x1000] = {0}; memcpy(szBuffer,hMapView,10);
0x02 项目测试实例
0x03 总结
掌握内存通信的几个函数就好
CreateFileMappingMapViewOfFile
0则评论给“进程通信 - 共享内存”