我对此进行了测试..它有效..首先运行C 代码。它将创建一个内存映射。然后运行python代码。它将写入地图。C
代码将读取地图并打印写入其中的内容。
我知道这段代码是错误的,因为我没有正确地序列化数据(也就是先将文件大小写到文件中,然后再将数据写到文件中,等等。)但是……这
只是一个BASIC的工作示例..仅此而已。
蟒蛇:
import mmapshm = mmap.mmap(0, 512, "Local\Test") #You should "open" the memory map file instead of attempting to create it..if shm: shm.write(bytes("5", 'UTF-8')); shm.write(bytes("Hello", 'UTF-8')) print("GOOD")
C ++:
#include <windows.h>#include <cstring>#include <cstdbool>#include <iostream>typedef struct{ void* hFileMap; void* pData; char MapName[256]; size_t Size;} SharedMemory;bool CreateMemoryMap(SharedMemory* shm){ if ((shm->hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, shm->Size, shm->MapName)) == NULL) { return false; } if ((shm->pData = MapViewOfFile(shm->hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, shm->Size)) == NULL) { CloseHandle(shm->hFileMap); return false; } return true;}bool FreeMemoryMap(SharedMemory* shm){ if (shm && shm->hFileMap) { if (shm->pData) { UnmapViewOfFile(shm->pData); } if (shm->hFileMap) { CloseHandle(shm->hFileMap); } return true; } return false;}int main(){ SharedMemory shm = {0}; shm.Size = 512; sprintf(shm.MapName, "Local\Test"); if (CreateMemoryMap(&shm)) { char* ptr = (char*)shm.pData; memset(ptr, 0, shm.Size); while (ptr && (*ptr == 0)) { Sleep(100); } int size = (int)*ptr; ptr += sizeof(char); int i = 0; for (; i < size; ++i) { std::cout<<ptr[i]; } FreeMemoryMap(&shm); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)