Click here to Skip to main content
15,886,137 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionRegarding Memory Mapped Pin
manju 321-Jun-11 2:52
manju 321-Jun-11 2:52 
AnswerRe: Regarding Memory Mapped Pin
«_Superman_»21-Jun-11 6:17
professional«_Superman_»21-Jun-11 6:17 
GeneralRe: Regarding Memory Mapped Pin
manju 323-Jun-11 2:39
manju 323-Jun-11 2:39 
GeneralRe: Regarding Memory Mapped Pin
enhzflep23-Jun-11 11:06
enhzflep23-Jun-11 11:06 
GeneralRe: Regarding Memory Mapped Pin
manju 323-Jun-11 20:25
manju 323-Jun-11 20:25 
GeneralRe: Regarding Memory Mapped Pin
enhzflep23-Jun-11 21:12
enhzflep23-Jun-11 21:12 
GeneralRe: Regarding Memory Mapped Pin
manju 323-Jun-11 22:19
manju 323-Jun-11 22:19 
GeneralRe: Regarding Memory Mapped Pin
enhzflep24-Jun-11 0:12
enhzflep24-Jun-11 0:12 
Hullo again.

I've looked at your code and noticed that when referring to memory, there seemed to be mixed references to the buffer that was shared, and the one that was locally defined.

I've gone through and made a number of changes, sorry I can't think of them all right now. Sorry I also made it non-unicode. I've little experience with it and have no impulse to further that just now.

Try this: (yup, tested and working Smile | :) )
Hope your mileage is better than last time..

// process1.c
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 2048

char *szName = "Global\MyFileMappingObject";
char *pSharedBuffer;

int addMsg(char *msg, char *buffer)
{
    long msgCount, curMsgIndex, curMsgLen;
    char *newStrPos;

    // get count of messages stored
    memcpy(&msgCount, buffer, sizeof(long));

    // first message begins after sizeof(long) bytes - since we stored the num of messages as a long
    newStrPos = (char*) (long)buffer + sizeof(long);

    // step through the buffer, until we find the next free space to insert a new message
    for (curMsgIndex=0; curMsgIndex<msgCount; curMsgIndex++)
    {
        curMsgLen = strlen(newStrPos);
        newStrPos += curMsgLen + 1;
    }

    // copy the message
    strcpy(newStrPos, msg);

    // update the message count
    msgCount++;
    memcpy(buffer, &msgCount, sizeof(long));

    return 0;
}

int main()
{
    HANDLE handleMappedFile;
    LPVOID lpMsgBuf;

    handleMappedFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, HIWORD(BUF_SIZE), LOWORD(BUF_SIZE), szName);
    if (handleMappedFile == NULL)
    {
        printf("Could not create file mapping object.\n");
        LocalFree( lpMsgBuf );
        return 1;
    }

    pSharedBuffer = (char*)MapViewOfFile(handleMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pSharedBuffer == NULL)
    {
        printf("Could not map view of file.\n");
        CloseHandle(handleMappedFile);
        return 1;
    }

    // initialize the shared mem
    memset((PVOID)pSharedBuffer, 0, sizeof(char) * BUF_SIZE);

    addMsg("This is msg 1", pSharedBuffer);
    addMsg("This is msg 2", pSharedBuffer);
    addMsg("This is msg 3", pSharedBuffer);

//    // code used to write mapped mem to a disk file for debugging purposes.
//    FILE *fp;
//    char *fName = "buffData.dat";
//    fp = fopen(fName, "wb");
//    fwrite(pSharedBuffer, 1, BUF_SIZE, fp);
//    fclose(fp);

    getch();
    UnmapViewOfFile(pSharedBuffer);
    printf("File unmapped\n");

    CloseHandle(handleMappedFile);
    printf("handle closed.\n");

    return 0;
}


// process2.c
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 2048
char *szName1= "Global\MyFileMappingObject";
char *pSharedBuffer;

int main()
{
    HANDLE hMappedFile;

    hMappedFile = OpenFileMapping(ILE_MAP_ALL_ACCESS, FALSE, szName1); // name of mapping object
    if (hMappedFile == NULL)
    {
        printf("Could not open file mapping object (%d).\n",GetLastError());
        return 1;
    }

    pSharedBuffer = MapViewOfFile(hMappedFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE);
    if (pSharedBuffer == NULL)
    {
        printf("Could not map view of file (%d).\n", GetLastError());
        CloseHandle(hMappedFile);
        return 1;
    }

    long msgCount, curMsgNum;
    char *curMsg;
    memcpy(&msgCount, pSharedBuffer, sizeof(long));
    printf("---- Messages retrieved: %d ----\n", msgCount);
    curMsg = (char*)pSharedBuffer + sizeof(long);
    for (curMsgNum=0; curMsgNum<msgCount; curMsgNum++)
    {
        printf("msg(%d): %s\n", curMsgNum, curMsg);
        curMsg += strlen(curMsg) + 1;
    }

    UnmapViewOfFile(pSharedBuffer);
    CloseHandle(hMappedFile);
    getch();
    return 0;
}

GeneralRe: Regarding Memory Mapped Pin
manju 324-Jun-11 2:07
manju 324-Jun-11 2:07 
GeneralRe: Regarding Memory Mapped Pin
manju 32-Aug-11 19:49
manju 32-Aug-11 19:49 
Questionfp:precise vs. fp:strict performance Pin
Alex_GR21-Jun-11 2:37
Alex_GR21-Jun-11 2:37 
QuestionReceive a client event when file on web server is modified Pin
pscholl21-Jun-11 0:54
pscholl21-Jun-11 0:54 
AnswerRe: Receive a client event when file on web server is modified Pin
Mark Salsbery21-Jun-11 8:18
Mark Salsbery21-Jun-11 8:18 
GeneralRe: Receive a client event when file on web server is modified Pin
pscholl22-Jun-11 1:34
pscholl22-Jun-11 1:34 
GeneralRe: Receive a client event when file on web server is modified Pin
Mark Salsbery22-Jun-11 7:55
Mark Salsbery22-Jun-11 7:55 
QuestionSort the list of files available in a folder. Pin
lucky_122120-Jun-11 23:02
lucky_122120-Jun-11 23:02 
AnswerRe: Sort the list of files available in a folder. Pin
Cedric Moonen21-Jun-11 0:14
Cedric Moonen21-Jun-11 0:14 
AnswerRe: Sort the list of files available in a folder. Pin
Richard MacCutchan21-Jun-11 2:03
mveRichard MacCutchan21-Jun-11 2:03 
GeneralRe: Sort the list of files available in a folder. Pin
lucky_122121-Jun-11 2:10
lucky_122121-Jun-11 2:10 
GeneralRe: Sort the list of files available in a folder. Pin
Richard MacCutchan21-Jun-11 2:59
mveRichard MacCutchan21-Jun-11 2:59 
AnswerRe: Sort the list of files available in a folder. Pin
వేంకటనారాయణ(venkatmakam)21-Jun-11 3:05
వేంకటనారాయణ(venkatmakam)21-Jun-11 3:05 
QuestionQuestion about Getting Main Icon using Findresource() Pin
lek25820-Jun-11 21:51
lek25820-Jun-11 21:51 
AnswerRe: Question about Getting Main Icon using Findresource() Pin
Richard MacCutchan21-Jun-11 2:01
mveRichard MacCutchan21-Jun-11 2:01 
AnswerRe: Question about Getting Main Icon using Findresource() Pin
MicroVirus21-Jun-11 5:50
MicroVirus21-Jun-11 5:50 
Questionproblem while freeing a link list within a vector structure [modified] Pin
Madan Chauhan20-Jun-11 21:27
Madan Chauhan20-Jun-11 21:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.