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
Then we start that program again as a new process.
               ----------------------------------------------
              |                                                     |
              |----------------------------------------------       |
              |  ---------------------------             |
              | |Here's the data...        |             |
              | |              ------------------------------------------
              | |            |                                      |                                       |
              | |            |------------------------------------------- |
              | |            |  ---------------------------         |
              | |            | |                        |           |
              | |            | |                        |           |
              |  ------------- |                        |                 |
                       |
              |              | |                        |           |
              ---------------- |                        |                 |
                       |
                             | |                        |           |
                             | |                        |           |
                               |  ---------------------------       |
                               |                                    |
                                  ---------------------------------------
Next, we read the data in from the memory file in the second process.
               ----------------------------------------------
              |                                                       |
              |----------------------------------------------         |
              |  ---------------------------              |
              | |Here's the data...        |              |
              | |               ----------------------------------------------
              | |            |                                        |
              | |            |---------------------------------------------- |
              | |            |   ---------------------------          |
              | |            | |Here's the data...       |            |
              | |            | |                         |            |
              | -------------- |                         |                   |
                             |
              |              | |                         |            |
               --------------- |                         |                   |
                             |
                             | |                         |            |
                             | |                         |            |
                             |  ---------------------------           |
                               |                                             |
                                --------------------------------------------
Determining whether Your Program Is Already Running One way to determine whether your program is already running as another process is to set up a flag in a memory-mapped file that you check each time the program starts. In this way, we are able to communicate between running processes. Create this new project, ShareMem, now. Create a new MDI program named ShareMem with the AppWizard. The first step in writing ShareMem will be to let the user type in the data they want to transfer from process to process, and we support that now. #### Getting the Data to Transfer To read keys from the keyboard and store them in a CString in the document named, for example, text, add an OnChar() function and place the following code in it:
    void CShareMemView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        pDoc->text += nChar;

        Invalidate();
        CView::OnChar(nChar, nRepCnt, nFlags);
    }
To display that text, we add this code to OnDraw():
    void CShareMemView::OnDraw(CDC* pDC)
    {
        CShareMemDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);

        pDC->TextOut(0, 0, pDoc->text);                               ⇐
    }
Now we have the data we want to place in the memory-mapped file and have stored it in a CString object named text. The next step is to write that data to a memory-mapped file and then read it in. To let the user do that, add two more items to the File menu: Write Memory File and Read Memory File. All we have to do now is to make these new menu items work. #### Writing a Memory-Mapped File First we write our data to a memory-mapped file in OnFileWritememoryfile(). We begin by getting a pointer to the document.
    void CShareMemView::OnFileWritememoryfile()
    {

        CShareMemDoc* pDoc = GetDocument();                             ⇐
        ASSERT_VALID(pDoc);                                             ⇐
            .
            .
            .
To create a memory-mapped file, we use CreateFileMapping().
    HANDLE CreateFileMapping(HANDLE  hFile, LPSECURITY_ATTRIBUTES
    lpFileMappingAttributes, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow, LPCTSTR lpName);
Here, we pass it a file handle (we use 0xFFFFFFFF to indicate that we want to use the system’s own swap file for our file); a pointer to a SECURITY_ATTRIBUTES structure, which we leave NULL; the file attributes, which can include: PAGE_READONLY Read-only access to the committed region PAGE_READWRITE Read-write access to the committed region PAGE_WRITECOPY Copy on write access to the committed region followed by the high double word of the file size we’re requesting, the low double word of the file size, and the name of the file we’re creating, which we call “MemoryFile.”
    void CShareMemView::OnFileWritememoryfile()
    {

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

        HANDLE MemoryMappingHandle;                                     ⇐

        MemoryMappingHandle = CreateFileMapping((HANDLE) 0xFFFFFFFF, NULL,
    PAGE_READWRITE, 0, 2048, “MemoryFile”);                             ⇐
        .
        .
        .
Now we’ve created our memory-mapped file. To use it, we need a view of that file—much like the views we’re already familiar with. #### Creating Memory File Views We use MapViewOfFile() to get an actual pointer to our new file in memory.
    LPVOID MapViewOfFile(HANDLE hFileMappingObject, DWORD
    dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, DWORD dwNumberOfBytesToMap);
MapViewOfFile() returns a pointer to the start of our file in memory. The first parameter is a handle of a open file-mapping object. The next parameter specifies the type of access we want and can take the following values: 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 two parameters hold the offset where mapping is to begin in the file and the last parameter holds the number of bytes to map. If the last parameter is zero, we map the entire file.
    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);                                                           ⇐
        .
        .
        .
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.