简体   繁体   中英

How to save structure in shared memory C - Windows

I have a structure:

typedef struct configuration {
    char *server_type;
    char *server_ip;
    unsigned int server_port;
} configuration;

Now since i have more processes i need this structure to be shared between all of them ( after it's initialized by the Main process). So i have this named shared memory:

TCHAR szMsg[]=TEXT("JUST A TEST");
LPCTSTR pBuf;
HANDLE hMapFile;
hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, BUF_SIZE, "Global\\Config");

if (hMapFile == NULL){
    perror("Errore nel creare memory object");
    exit(1);
}

pBuf = (LPTSTR) MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
if (pBuf == NULL){
    perror("Errore nel mappare la view del file");
    CloseHandle(hMapFile);
    exit(1);
}

CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
UnmapViewOfFile(pBuf);

Now how can i pass the pointer of the structure insted of szMsg and from another process read it from shared memory ?

Thanks

创建一个数据类型为“configuration* structPoint”的变量,然后传递 structPoint 而不是 'TCHAR szMsg'。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM