MapViewOfFile向子进程发送一个整数

时间:2012-11-22 13:38:19

标签: c windows winapi shared-memory

所以我试图将一个a = 3的整数从服务器端传递给Client。问题是,当我在客户端执行消息的printf时,显示的值是一个随机数(而不是3)比如19923)。我试图在服务器端传递a值(& a),但显示的值是心形。请查看该通信有什么问题。提前谢谢。

   //Server
    #include <windows.h>
    #define BUF_SIZE 256
    LPSTR szMapName = "MyFileMappingObject";

    int main(void)
    {
        int a = 3;
        HANDLE hMapFile;
        LPVOID lpMapAddress;
        BOOL bRet;

        hMapFile = CreateFileMapping(
            INVALID_HANDLE_VALUE,         /* use swap, not a particular file */
            NULL,                     /* default security */
            PAGE_READWRITE,               /* read/write access */
            0,                        /* maximum object size (high-order DWORD) */
            1024,                     /* maximum object size (low-order DWORD) */
            szMapName);               /* name of mapping object */
        if (hMapFile == INVALID_HANDLE_VALUE){
                 printf("CreateFileMapping error %lu",GetLastError());
            }
        lpMapAddress = MapViewOfFile(
                hMapFile,         /* handle to map object */
                FILE_MAP_ALL_ACCESS,  /* read/write permission */
                0,            /* offset (high-order) */
                0,            /* offset (low-order) */
                0);
        if (lpMapAddress == NULL) 
                 printf("MapViewOfFile error");

        //ZeroMemory(lpMapAddress, strlen(szMsg) + 1);
        CopyMemory(lpMapAddress, a, sizeof(a));

        Sleep(10000);

        bRet = UnmapViewOfFile(lpMapAddress);
        if (bRet == FALSE)
                printf("UnampViewOfFile error");

        bRet = CloseHandle(hMapFile);
        if (bRet == FALSE)
                printf("CloseHandle error");

        return 0;
    }
//Client
#include <windows.h>
#include <stdio.h>
#define BUF_SIZE 256
LPSTR szMapName = "MyFileMappingObject";

int main(void)
{
    HANDLE hMapFile;
    LPVOID lpMapAddress;
    BOOL bRet;
    hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS,                 /* read/write access */
        FALSE,                               /* do not inherit the name */
        szMapName);                          /* name of mapping object */ 

    if (hMapFile == INVALID_HANDLE_VALUE){
        printf("CreateFileMapping error %lu",GetLastError());
        return 1;
    }

    lpMapAddress = MapViewOfFile(
        hMapFile,       /* handle to map object */
        FILE_MAP_ALL_ACCESS,    /* read/write permission */
        0,          /* offset (high-order) */
        0,          /* offset (low-order) */
        0);

    if (lpMapAddress == NULL){
        printf("MapViewOfFile error %d",GetLastError());
        return 1;
    }

    printf("Message from server is: %d\n", lpMapAddress);

    bRet = UnmapViewOfFile(lpMapAddress);
    if (bRet == FALSE){
        printf("UnampViewOfFile");
        return 1;
    }
    bRet = CloseHandle(hMapFile);
    if (bRet == FALSE){ 
        printf("CloseHandle");
        return 1;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

这是不正确的:

printf("Message from server is: %d\n", lpMapAddress);

因为lpMapAddress的类型为LPVOID(一个void*),所以这将打印lpMapAddress点的内存地址,而不是整数值。 (请注意,必须使用%p来打印指针值。)

基于用于将int写入共享内存的技术:

CopyMemory(lpMapAddress, a, sizeof(a));

提取int将是相反的:

int read_a;
CopyMemory(&read_a, lpMapAddress, sizeof(read_a));
printf("Message from server is: %d\n", read_a);

请注意,以下行中缺少格式说明符:

printf("CreateFileMapping error",GetLastError());

应该是:

printf("CreateFileMapping error: %lu", GetLastError());