WindowsXP FileMapping problem,help!!

This is a discussion on WindowsXP FileMapping problem,help!! within the Windows Programming forums, part of the Platform Specific Boards category; hello, When I want to use the shared memory, I got a problem. 1.I wrote a server to create a ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    5

    WindowsXP FileMapping problem,help!!

    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 = CreateFileMapping(
    INVALID_HANDLE_VALUE,
    NULL,
    PAGE_READWRITE,
    0,
    BUF_SIZE, // 1024
    SHM_NAME);



    pVoid = MapViewOfFile(hMapFile,
    FILE_MAP_ALL_ACCESS,
    0,
    0,
    BUF_SIZE);

    memset(pVoid, '*', BUF_SIZE);
    char *szMsg = "Hello shared memory.";
    CopyMemory(pVoid, 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(hFileMap,
    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

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by wal_sug View Post
    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.
    printf will keep on printing until it either finds a null character, or throws an access violation from trying to reading memory you don't have read access to. Since you are not null-terminating the string or using exception handling you're basically just waiting for a crash to happen
    Code:
    char *szMsg = "Hello shared memory.";
    CopyMemory(pVoid, szMsg, strlen(szMsg));
    strlen returns the length of a string without counting the terminating 0. Use strlen(szMsg)+1 instead.

    Quote Originally Posted by wal_sug View Post
    4.If I only want to output the 8 bytes' info from the shared memory, what should I do?
    You can either copy 8 chars to a new string, or if you don't care about corrupting the original data just insert a NULL at the 9th position.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 30
    Last Post: 09-12-2006, 08:06 PM
  2. windowsxp networking.
    By xddxogm3 in forum Tech Board
    Replies: 10
    Last Post: 10-05-2004, 01:18 PM
  3. WindowsXP themes!
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-31-2002, 06:18 AM
  4. WindowsXP Look
    By Crux++ in forum Windows Programming
    Replies: 10
    Last Post: 04-08-2002, 05:06 PM
  5. WindowsXP Security
    By Dude101 in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 10-03-2001, 02:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21