WindowsXP FileMapping problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • walsug
    New Member
    • Oct 2008
    • 14

    WindowsXP FileMapping problem

    hello,
    When I want to use the shared memory, I got a problem.
    1.I wrote a server to create a piece of shared memory. Source is below:
    // shmsv.cpp
    HANDLE hMapFile = NULL;
    LPVOID pVoid = NULL;

    hMapFile = CreateFileMappi ng(
    INVALID_HANDLE_ VALUE,
    NULL,
    PAGE_READWRITE,
    0,
    BUF_SIZE, // 1024
    SHM_NAME);



    pVoid = MapViewOfFile(h MapFile,
    FILE_MAP_ALL_AC CESS,
    0,
    0,
    BUF_SIZE);

    memset(pVoid, '*', BUF_SIZE);
    char *szMsg = "Hello shared memory.";
    CopyMemory(pVoi d, szMsg, strlen(szMsg));

    2.I wrote another program to read message from the shared memory. Source is below:
    // readFrom.cpp
    HANDLE hFileMap = NULL;
    LPVOID pVoid = NULL;

    hFileMap = OpenFileMapping (FILE_MAP_READ,
    FALSE,
    SHM_NAME);

    pVoid = MapViewOfFile(h FileMap,
    FILE_MAP_READ,
    0,
    0,
    8);
    //BUF_SIZE);
    printf("message from shared memory is: %s.\n", (char *)pVoid);

    3.Here comes the probelm: I only want to output the 8 bytes' info from the shared memory.
    But the "readFrom" program outputed all the message from the shared memory.

    4.If I only want to output the 8 bytes' info from the shared memory, what should I do?

    regards,
    yogo
Working...