window下 使用C语言读取共享内存的数据

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

__declspec(dllexport) char* read_shared_memory() {
    char currentPath[MAX_PATH];
    DWORD length = GetCurrentDirectoryA(MAX_PATH, currentPath);
    if (length == 0 || length > MAX_PATH) {
        printf("Error getting current directory.\n");
        return NULL;
    }

    // 构建文件的完整路径
    strcat_s(currentPath, MAX_PATH, "\\UDE_temp_data_file.dat");

    HANDLE hFile = CreateFileA(currentPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hFile == INVALID_HANDLE_VALUE) {
        printf("Error opening file.\n");
        return NULL;
    }

    DWORD fileSize = GetFileSize(hFile, NULL);
    HANDLE hMapFile = CreateFileMappingA(hFile, NULL, PAGE_READWRITE, 0, fileSize, NULL);
    if (hMapFile == NULL) {
        printf("Error creating file mapping.\n");
        CloseHandle(hFile);
        return NULL;
    }

    char* pBuf = (char*)MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, fileSize);
    if (pBuf == NULL) {
        printf("Error mapping view of file.\n");
        CloseHandle(hMapFile);
        CloseHandle(hFile);
        return NULL;
    }

    // 读取数据长度
    unsigned int data_length = *(unsigned int*)pBuf;

    // 根据数据长度读取实际的数据
    char* data = (char*)malloc(data_length + 1); // +1 for null terminator
    if (data == NULL) {
        printf("Memory allocation failed.\n");
        UnmapViewOfFile(pBuf);
        CloseHandle(hMapFile);
        CloseHandle(hFile);
        return NULL;
    }
    memcpy(data, pBuf + sizeof(unsigned int), data_length);
    data[data_length] = '\0'; // null-terminate the string
    //  清空文件内容
    memset(pBuf,0,fileSize);
    // 清理
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
    CloseHandle(hFile);

    return data; // 注意:调用者负责释放这个内存
}
char* read_non_empty_shared_memory() {
    char* data = NULL;
    while (1) {
        data = read_shared_memory();
        if (data != NULL && data[0] != '\0') {
            break;
        }
        Sleep(1000); // 每秒尝试一次
    }
    return data;
}
int main() {
    char* data = read_shared_memory();

    if (data != NULL) {
        printf("Read data: %s\n", data);
        free(data); // 释放由read_shared_memory分配的内存
    } else {
        printf("Failed to read data from shared memory.\n");
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Windows C++中,读取已存在的共享内存中的数据需要经过以下步骤: 1. 使用 `OpenFileMapping` 函数打开共享内存对象,该函数返回一个句柄。 2. 使用 `MapViewOfFile` 函数将共享内存映射到当前进程的地址空间中,该函数返回指向共享内存的指针。 3. 通过指针访问共享内存中的数据。 4. 使用 `UnmapViewOfFile` 函数取消映射。 5. 使用 `CloseHandle` 函数关闭句柄。 下面是一个示例代码,展示如何读取已存在的共享内存中的数据: ```c++ #include <iostream> #include <Windows.h> int main() { // 打开共享内存对象 HANDLE hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, L"mySharedMemory"); if (hMapFile == NULL) { std::cout << "OpenFileMapping failed" << std::endl; return 1; } // 将共享内存映射到当前进程的地址空间中 LPVOID lpMapAddress = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0); if (lpMapAddress == NULL) { std::cout << "MapViewOfFile failed" << std::endl; CloseHandle(hMapFile); return 1; } // 读取共享内存中的数据 int* pData = (int*)lpMapAddress; std::cout << "Data read from shared memory: " << *pData << std::endl; // 取消映射 UnmapViewOfFile(lpMapAddress); // 关闭句柄 CloseHandle(hMapFile); return 0; } ``` 在上面的代码中,假设共享内存对象的名称为 `"mySharedMemory"`,且共享内存中存储的是一个整数。程序通过 `OpenFileMapping` 函数打开共享内存对象,并使用 `MapViewOfFile` 函数将共享内存映射到当前进程的地址空间中。随后,程序通过指针访问共享内存中的数据,最后取消映射并关闭句柄。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值