Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
Now we have a pointer to the file view, so we copy the text that we want to map from the document’s text CString object.
    void CShareMemView::OnFileWritememoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle;

        MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL,
    PAGE_READWRITE, 0, 2048, “MemoryFile”);

        LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_WRITE,
    0, 0, 0);

        ::lstrcpy((char *) FilePointer, (LPCTSTR) pDoc->text);          ⇐
            .
            .
            .
The last step in this process is to unmap the file view and to indicate to the user that the data has been written to the memory-mapped file. We do that in a message box.
    void CShareMemView::OnFileWritememoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle;

        MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL,
    PAGE_READWRITE, 0, 2048, “MemoryFile”);

        LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_WRITE,
    0, 0, 0);

        ::lstrcpy((char *) FilePointer, (LPCTSTR) pDoc->text);

        UnmapViewOfFile((LPVOID) FilePointer);                          ⇐

        if(FilePointer != NULL){                                        ⇐
            AfxMessageBox(“Memory file written.”);            ⇐
        }                                                               ⇐
    }
We’ve placed our data in memory. All that remains now is to read it back from another process. #### Reading a Memory-Mapped File The other process in which we read the data from the memory mapped file is a copy of our same program, ShareMem. When the user selects the Read Memory File item in the File menu, we open the memory mapped file named “MemoryFile” and read the data from it, displaying it in our view. We start OnFileReadmemoryfile() by getting a pointer to the document.
    void CShareMemView::OnFileReadmemoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
            .
            .
            .
To write the file, we used CreateFileMapping(). As you might imagine, we use a function named OpenFileMapping() to open a memory-mapped file.
    HANDLE OpenFileMapping(DWORD dwDesiredAccess, BOOL bInheritHandle,
    LPCTSTR lpName);
OpenFileMapping() returns a handle to the memory-mapped file. The first parameter indicates the access we want to the file mapping object and can be one of the following: FILE_MAP_WRITE Read-write access FILE_MAP_READ Read-only access FILE_MAP_ALL_ACCESS Same as FILE_MAP_WRITE FILE_MAP_COPY Copy-on-write access. The next parameter in OpenFileMapping() specifies whether this handle will be inherited by any child processes of the current process, and the last parameter is the name of the memory mapped file, which in our case is “MemoryFile”:
    void CShareMemView::OnFileReadmemoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle = NULL;                              ⇐

        MemoryMappingHandle = OpenFileMapping(FILE_MAP_READ, FALSE,
    “MemoryFile”);                                                      ⇐
        .
        .
        .
As before, we need a view of the file to get a pointer to it before we can work with the file. We get that view with MapViewOfFile(), indicating that we want to start at an offset of 0 bytes into the file.
    void CShareMemView::OnFileReadmemoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle = NULL;

        MemoryMappingHandle = OpenFileMapping(FILE_MAP_READ, FALSE,
    “MemoryFile”);

        LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_READ |
    FILE_MAP_WRITE, 0, 0, 0);                                           ⇐
        .
        .
        .
Now that we have a pointer to the view in the file, we can copy the data in that file to the text object in the document.
    void CShareMemView::OnFileReadmemoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle = NULL;

        MemoryMappingHandle = OpenFileMapping(FILE_MAP_READ, FALSE,
    “MemoryFile”);

        LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_READ |
    FILE_MAP_WRITE, 0, 0, 0);

        pDoc->text = (char *) FilePointer;                           ⇐
            .
            .
            .
Finally, we unmap the view of the file, close the memory file’s handle, and invalidate the view to display the data we’ve read.
    void CShareMemView::OnFileReadmemoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        HANDLE MemoryMappingHandle = NULL;

        MemoryMappingHandle = OpenFileMapping(FILE_MAP_READ, FALSE,
    “MemoryFile”);

        LPVOID FilePointer = MapViewOfFile(MemoryMappingHandle, FILE_MAP_READ |
    FILE_MAP_WRITE, 0, 0, 0);

        pDoc->text = (char *) FilePointer;

        UnmapViewOfFile((LPVOID) FilePointer);                          ⇐

        CloseHandle(MemoryMappingHandle);                               ⇐

        Invalidate();                                                   ⇐

    }
Run the program and start a second copy of the same program, as shown in Figure 7.2. Type some text into the first copy of the program now and select the Write Memory File item in the File menu. This places the text you’ve typed into the memory file. Select the Read Memory File item in the second process’s File menu to read the text from the memory file. Our ShareMem program is a success, and we now have two running processes communicate with each other through memory-mapped files. The code for this example, ShareMemView.h and ShareMemView.cpp, appears in Listing 7.2.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.