none
InterProcess Communication shared memory

    Question

  • I have two process one process contains the code for the server which is constantly being updated. the 2nd process polls the data every second. In the 2nd process i wish to alter the data in the structure once in a while.

    1. I have a server that updates a shared memory which contains a structure that is constantly being updated. 

    2. I also have a client that reads from the shared memory every second.

    3. I wish to have another client that can write to the shared memory. 

    I have managed to implement 1 and 2. could you please let me know what i am doing wrong with my code. 

    /******************Server Code**********************/

            hMapFile = CreateFileMapping(
            INVALID_HANDLE_VALUE, // Use paging file instead of existing file.
            // Pass file handle to share in a file.
            NULL, // Default security 
            PAGE_READWRITE, // Read/write access
            0, // Max. object size 
            sizeof(gc), // Buffer size  
            szMapFileName // Name of mapping object
            );
            
            if (hMapFile == NULL) 
           
            _tprintf(_T("Unable to create file mapping object w/err 0x%08lx\n"), 
            GetLastError());
            return 1;
            }
           
            _tprintf(_T("The file mapping object, %s, is created.\n"), szMapFileName);
            
            pBuf = (struct Gyro_commands*) MapViewOfFile(
            hMapFile, // Handle of the map object
            FILE_MAP_ALL_ACCESS, // Read/write permission
            0, // A high-order DWORD of the file offset 
            // where the view begins.
            0, // A low-order DWORD of the file offset 
            // where the view is to begin. 
            sizeof(gc) // The number of bytes of a file mapping 
            // to map to the view.
            );
            
            if (pBuf == NULL)
           
            _tprintf(_T("Unable to map view of file w/err 0x%08lx\n"), 
            GetLastError()); 
            CloseHandle(hMapFile);
            return 1;
            }
            
            _tprintf(_T("The file view is created.\n"));
            
        while(1)
        {
            stringstream zzz;
            zzz << rand();
            stringstream yyy; 
            yyy << rand();
            stringstream xxx;
            xxx << rand();
            stringstream ccc; 
            ccc << rand();
            
            gc.x_response = 123.25 / rand();
            gc.y_response = 246.80 / rand();
            gc.z_response = 999.99 / rand();
            strncpy(gc.response,zzz.str().c_str(),36);
            strncpy(gc.command_x,yyy.str().c_str(),10);
            strncpy(gc.command_y,xxx.str().c_str(),10);
            strncpy(gc.command_z,ccc.str().c_str(),10);
            
            memcpy(pBuf,&gc,sizeof(gc));
            }
            UnmapViewOfFile(pBuf);
            CloseHandle(hMapFile);

    /***********************Client Code******************************/

        Gyro_commands cc;
        // Open the named file mapping.
        HANDLE hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS, // Read/write access
        FALSE, // Do not inherit the name
        szMapFileName // Name of mapping object 
        );
        
        if (hMapFile == NULL) 
       
        stringstream err("Unable to open file mapping object w/err");
        err << GetLastError();
        ScreenOutput(err.str());
        return 1;
        }
        //stringstream err("The file mapping object, ");
        //err << szMapFileName << " , is opened.\n";
        //ScreenOutput(err.str());
        
        int sizes = sizeof(cc);
       
        /////////////////////////////////////////////////////////////////////////
        // Maps the view of the file mapping into the address space of the 
        // current process.
        // 
        Gyro_commands* ccbuff;
        
        // Create file view from the file mapping object.
        ccbuff = (struct Gyro_commands*)MapViewOfFile(
        hMapFile, // Handle of the map object
        FILE_MAP_ALL_ACCESS, // Read/write permission
        0,
        0,
        sizes); // The number of bytes of a file mapping 
        // to map to the view.
        
        if (ccbuff == NULL) 
       
        stringstream err("Unable to map view of file w/err");
        err << GetLastError();
        ScreenOutput(err.str());
        CloseHandle(hMapFile);
        return 1;
        }
       
        //ScreenOutput(string("The file view is created.\n"));
        
        
        /////////////////////////////////////////////////////////////////////////
        // Show the message in shared memory.
        // 
        stringstream xx;
        xx << "Gyro X: " << ccbuff->x_response << "\n"
          << "Gyro Y: " << ccbuff->y_response << "\n"
          << "Gyro Z: " << ccbuff->z_response << "\n"
          << "Data : " << ccbuff->response << "\n"
          << "Message X: " << ccbuff->command_x << "\n"
          << "Message Y: " << ccbuff->command_y << "\n"
          << "Message Z: " << ccbuff->command_z << "\n";
       
        ScreenOutput(xx.str());
        
        /////////////////////////////////////////////////////////////////////////
        // Unmap the file view and close the file mapping objects.
        // 
        
        UnmapViewOfFile(&ccbuff);
        CloseHandle(hMapFile);

    /******************Client Writer*******************************/

        Gyro_commands cc;
        // Open the named file mapping.
        HANDLE hMapFile = OpenFileMapping(
        FILE_MAP_ALL_ACCESS, // Read/write access
        FALSE, // Do not inherit the name
        szMapFileName // Name of mapping object 
        );
        
        if (hMapFile == NULL) 
       
        stringstream err("Unable to open file mapping object w/err");
        err << GetLastError();
        ScreenOutput(err.str());
        return 1;
        }
        //stringstream err("The file mapping object, ");
        //err << szMapFileName << " , is opened.\n";
        //ScreenOutput(err.str());
        
        int sizes = sizeof(cc);
       
        /////////////////////////////////////////////////////////////////////////
        // Maps the view of the file mapping into the address space of the 
        // current process.
        // 
        Gyro_commands* ccbuff;
        
        // Create file view from the file mapping object.
        ccbuff = (struct Gyro_commands*)MapViewOfFile(
        hMapFile, // Handle of the map object
        FILE_MAP_ALL_ACCESS, // Read/write permission
        0,
        0,
        sizes); // The number of bytes of a file mapping 
        // to map to the view.
        
        if (ccbuff == NULL) 
       
        stringstream err("Unable to map view of file w/err");
        err << GetLastError();
        ScreenOutput(err.str());
        CloseHandle(hMapFile);
        return 1;
        }
       
        ScreenOutput(string("wrote to shared memory"));
        cc.x_response = 100.00;
        cc.y_response = 200.00;
        cc.z_response = 300.00;
       
        memcpy(ccbuff,&cc,sizeof(cc));
        
        /////////////////////////////////////////////////////////////////////////
        // Unmap the file view and close the file mapping objects.
        // 
        
        UnmapViewOfFile(&ccbuff);
        CloseHandle(hMapFile);
    Sunday, June 23, 2013 8:59 PM

All replies

  • rafae11,

    What are you doing wrong? Well that's real simple. You have posted to the wrong forum. This forum deals exclusively with questions/issues about customization and programming of Microsoft Project, a planning and scheduling application.

    Please delete this post and try the following: http://social.technet.microsoft.com/Forums/en-US/whatforum/threads

    John

    Sunday, June 23, 2013 10:59 PM