Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A Problem in Writing Shared Memory to windows from a C++ application

A Problem in Writing Shared Memory to windows from a C++ application

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++game-devdebuggingperformance
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    D_code_writer
    wrote on last edited by
    #1

    Hey Guys, I'm writing an application in C++ that needs to write shared memory to windows and I've hit a stumbling block. Even though I'm not getting any error messages (I've stepped through with the debugger) it would appear nothing is writing to shared memory. Here is the code that writes the shared memory,

    void Write_Physics(PhysicsStruct *s_physics)
    {
    // Create the shared memory
    TCHAR szName[] = TEXT("Local\\physics");
    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
    if (!hMapFile)
    {
    return;
    }

    //	Open the buffer for file read and write
    mapFileBuffer = (unsigned char\*)MapViewOfFile(m\_physics.hMapFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, sizeof(PhysicsStruct));
    if (!mapFileBuffer)
    {
    	return;
    }
    
    //	Copy the physics pointer over
    CopyMemory(mapFileBuffer,s\_physics,sizeof(PhysicsStruct));
    //\_getch();
    
    
    //	Write the shared memory
    UnmapViewOfFile(m\_physics.mapFileBuffer);
    CloseHandle(m\_physics.hMapFile);
    

    }

    My difficulty is that when I try another process to read the shared memory nothing is happening. For completeness here is the code that reads the structure from the shared memory,

    void Read_Physics(PhysicsStruct *s_physics)
    {
    // Create the shared memory
    TCHAR szName[] = TEXT("Local\\physics");
    hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
    if (!m_physics.hMapFile)
    {
    return;
    }

    //	Open the buffer for file read and write
    mapFileBuffer = (unsigned char\*)MapViewOfFile(hMapFile, FILE\_MAP\_READ, 0, 0, sizeof(PhysicsStruct));
    if (!mapFileBuffer)
    {
    	return;
    }
    
    s\_physics = (PhysicsStruct\*)mapFileBuffer;
    
    //	Write the shared memory
    UnmapViewOfFile(mapFileBuffer);
    CloseHandle(hMapFile);	
    

    }
    //-----------------------------------------------------------------------

    If anyone has any suggestions it would be greatly appreciated. What the problem resembles is like trying to write something to file and forgetting the fprintf or file write commands. Thanks in advance guys.

    Richard Andrew x64R 2 Replies Last reply
    0
    • D D_code_writer

      Hey Guys, I'm writing an application in C++ that needs to write shared memory to windows and I've hit a stumbling block. Even though I'm not getting any error messages (I've stepped through with the debugger) it would appear nothing is writing to shared memory. Here is the code that writes the shared memory,

      void Write_Physics(PhysicsStruct *s_physics)
      {
      // Create the shared memory
      TCHAR szName[] = TEXT("Local\\physics");
      hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
      if (!hMapFile)
      {
      return;
      }

      //	Open the buffer for file read and write
      mapFileBuffer = (unsigned char\*)MapViewOfFile(m\_physics.hMapFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, sizeof(PhysicsStruct));
      if (!mapFileBuffer)
      {
      	return;
      }
      
      //	Copy the physics pointer over
      CopyMemory(mapFileBuffer,s\_physics,sizeof(PhysicsStruct));
      //\_getch();
      
      
      //	Write the shared memory
      UnmapViewOfFile(m\_physics.mapFileBuffer);
      CloseHandle(m\_physics.hMapFile);
      

      }

      My difficulty is that when I try another process to read the shared memory nothing is happening. For completeness here is the code that reads the structure from the shared memory,

      void Read_Physics(PhysicsStruct *s_physics)
      {
      // Create the shared memory
      TCHAR szName[] = TEXT("Local\\physics");
      hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
      if (!m_physics.hMapFile)
      {
      return;
      }

      //	Open the buffer for file read and write
      mapFileBuffer = (unsigned char\*)MapViewOfFile(hMapFile, FILE\_MAP\_READ, 0, 0, sizeof(PhysicsStruct));
      if (!mapFileBuffer)
      {
      	return;
      }
      
      s\_physics = (PhysicsStruct\*)mapFileBuffer;
      
      //	Write the shared memory
      UnmapViewOfFile(mapFileBuffer);
      CloseHandle(hMapFile);	
      

      }
      //-----------------------------------------------------------------------

      If anyone has any suggestions it would be greatly appreciated. What the problem resembles is like trying to write something to file and forgetting the fprintf or file write commands. Thanks in advance guys.

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      Where is hMapFile declared?

      void Read_Physics(PhysicsStruct *s_physics)
      {
      // Create the shared memory
      TCHAR szName[] = TEXT("Local\\physics");
      here >>>> hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
      if (!m_physics.hMapFile)
      {
      return;
      }

      //	Open the buffer for file read and write
      mapFileBuffer = (unsigned char\*)MapViewOfFile(hMapFile, FILE\_MAP\_READ, 0, 0, sizeof(PhysicsStruct));
      if (!mapFileBuffer)
      {
      	return;
      }
      
      s\_physics = (PhysicsStruct\*)mapFileBuffer;
      
      //	Write the shared memory
      UnmapViewOfFile(mapFileBuffer);
      CloseHandle(hMapFile);	
      

      }

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • D D_code_writer

        Hey Guys, I'm writing an application in C++ that needs to write shared memory to windows and I've hit a stumbling block. Even though I'm not getting any error messages (I've stepped through with the debugger) it would appear nothing is writing to shared memory. Here is the code that writes the shared memory,

        void Write_Physics(PhysicsStruct *s_physics)
        {
        // Create the shared memory
        TCHAR szName[] = TEXT("Local\\physics");
        hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
        if (!hMapFile)
        {
        return;
        }

        //	Open the buffer for file read and write
        mapFileBuffer = (unsigned char\*)MapViewOfFile(m\_physics.hMapFile, FILE\_MAP\_ALL\_ACCESS, 0, 0, sizeof(PhysicsStruct));
        if (!mapFileBuffer)
        {
        	return;
        }
        
        //	Copy the physics pointer over
        CopyMemory(mapFileBuffer,s\_physics,sizeof(PhysicsStruct));
        //\_getch();
        
        
        //	Write the shared memory
        UnmapViewOfFile(m\_physics.mapFileBuffer);
        CloseHandle(m\_physics.hMapFile);
        

        }

        My difficulty is that when I try another process to read the shared memory nothing is happening. For completeness here is the code that reads the structure from the shared memory,

        void Read_Physics(PhysicsStruct *s_physics)
        {
        // Create the shared memory
        TCHAR szName[] = TEXT("Local\\physics");
        hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PhysicsStruct), szName);
        if (!m_physics.hMapFile)
        {
        return;
        }

        //	Open the buffer for file read and write
        mapFileBuffer = (unsigned char\*)MapViewOfFile(hMapFile, FILE\_MAP\_READ, 0, 0, sizeof(PhysicsStruct));
        if (!mapFileBuffer)
        {
        	return;
        }
        
        s\_physics = (PhysicsStruct\*)mapFileBuffer;
        
        //	Write the shared memory
        UnmapViewOfFile(mapFileBuffer);
        CloseHandle(hMapFile);	
        

        }
        //-----------------------------------------------------------------------

        If anyone has any suggestions it would be greatly appreciated. What the problem resembles is like trying to write something to file and forgetting the fprintf or file write commands. Thanks in advance guys.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        There is another problem with the code for the Read function: You're passing the pointer s_physics by value. This is not going to do what you want. You need to pass the pointer's address. You would do that like so:

        void Read_Physics(PhysicsStruct **s_physics)

        Then inside the function:

        *s_physics = (PhysicsStruct*)mapFileBuffer;

        And the read function would be called thusly:

        Read_Physics(&s_physics);

        The difficult we do right away... ...the impossible takes slightly longer.

        D 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          There is another problem with the code for the Read function: You're passing the pointer s_physics by value. This is not going to do what you want. You need to pass the pointer's address. You would do that like so:

          void Read_Physics(PhysicsStruct **s_physics)

          Then inside the function:

          *s_physics = (PhysicsStruct*)mapFileBuffer;

          And the read function would be called thusly:

          Read_Physics(&s_physics);

          The difficult we do right away... ...the impossible takes slightly longer.

          D Offline
          D Offline
          D_code_writer
          wrote on last edited by
          #4

          Richard, Many thanks for answering. My apologies I wrote this as pseudo code to try and save time. The declarations for those are, HANDLE hmapFile; unsigned char *mapFileBuffer; They are at the beginning of each function. The joys of doing this at 1:00am! Copy you on the declaration of the read function - leave that with me to sort out. Apart from this is there any other reason you can see for this and particular the write function not working? Many thanks in advance.

          Richard Andrew x64R 1 Reply Last reply
          0
          • D D_code_writer

            Richard, Many thanks for answering. My apologies I wrote this as pseudo code to try and save time. The declarations for those are, HANDLE hmapFile; unsigned char *mapFileBuffer; They are at the beginning of each function. The joys of doing this at 1:00am! Copy you on the declaration of the read function - leave that with me to sort out. Apart from this is there any other reason you can see for this and particular the write function not working? Many thanks in advance.

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            D_code_writer wrote:

            I wrote this as pseudo code to try and save time.

            It's not helpful if you post code that is not the actual code that is causing the problem. I can see one other BIG problem: You're setting s_physics to point to the memory of the file map, but then you're closing the file mapping! EDIT: The problem is the same with the write function. You're writing to the memory, but then you're closing the file mapping! The read function cannot read from a file mapping that doesn't exist. I think this is the main reason it's not working. You need to copy the memory pointed to by mapFileBuffer, and return that as your result. Then you'll need to free this memory somewhere else. So, in pseudo code: Read Function: Create File Map; Allocate new memory = sizeof(PhysicsStruct); Copy memory from File Map to new memory; Set s_physics = new memory; return s_physics;

            The difficult we do right away... ...the impossible takes slightly longer.

            D 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              D_code_writer wrote:

              I wrote this as pseudo code to try and save time.

              It's not helpful if you post code that is not the actual code that is causing the problem. I can see one other BIG problem: You're setting s_physics to point to the memory of the file map, but then you're closing the file mapping! EDIT: The problem is the same with the write function. You're writing to the memory, but then you're closing the file mapping! The read function cannot read from a file mapping that doesn't exist. I think this is the main reason it's not working. You need to copy the memory pointed to by mapFileBuffer, and return that as your result. Then you'll need to free this memory somewhere else. So, in pseudo code: Read Function: Create File Map; Allocate new memory = sizeof(PhysicsStruct); Copy memory from File Map to new memory; Set s_physics = new memory; return s_physics;

              The difficult we do right away... ...the impossible takes slightly longer.

              D Offline
              D Offline
              D_code_writer
              wrote on last edited by
              #6

              Richard, Copy that - I actually figured it out. I found an old VC++ 6 project called MMFAPP1. That actually worked. Here is the crux of the fix. Declare the following variables,

              HANDLE m_hFileMMF; // memory mapped file
              LPVOID m_pViewMMFFile; // view of file, contains text in edit box

              Then create the file handle,

              m_hFileMMF = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4*1024,"MyMMF");
              DWORD dwError = GetLastError();
              if ( ! m_hFileMMF )
              {
              MessageBox(_T("Creation of file mapping failed"));
              }
              else
              {
              m_pViewMMFFile = MapViewOfFile(m_hFileMMF,FILE_MAP_ALL_ACCESS,0,0,0); // map all file

                  if(! m\_pViewMMFFile )
                  {
                      MessageBox(\_T("MapViewOfFile function failed"));
                  }
              }
              

              Once that is done to write the data to the shared memory we have PhysicsStruct s_physics;

              if(m_pViewMMFFile )
              CopyMemory(m_pViewMMFFile,&s_physics,sizeof(PhysicsStruct));

              To read from the shared memory we have,

              if(m_pViewMMFFile )
              CopyMemory((PVOID)&s_physics,m_pViewMMFFile,sizeof(SPageFilePhysics));

              I tested that and it worked like a charm. Also many thanks to the guy who wrote MMFAPP1 in the first place. It saved my neck. Also Richard thanks for your help as well.

              Richard Andrew x64R 1 Reply Last reply
              0
              • D D_code_writer

                Richard, Copy that - I actually figured it out. I found an old VC++ 6 project called MMFAPP1. That actually worked. Here is the crux of the fix. Declare the following variables,

                HANDLE m_hFileMMF; // memory mapped file
                LPVOID m_pViewMMFFile; // view of file, contains text in edit box

                Then create the file handle,

                m_hFileMMF = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,4*1024,"MyMMF");
                DWORD dwError = GetLastError();
                if ( ! m_hFileMMF )
                {
                MessageBox(_T("Creation of file mapping failed"));
                }
                else
                {
                m_pViewMMFFile = MapViewOfFile(m_hFileMMF,FILE_MAP_ALL_ACCESS,0,0,0); // map all file

                    if(! m\_pViewMMFFile )
                    {
                        MessageBox(\_T("MapViewOfFile function failed"));
                    }
                }
                

                Once that is done to write the data to the shared memory we have PhysicsStruct s_physics;

                if(m_pViewMMFFile )
                CopyMemory(m_pViewMMFFile,&s_physics,sizeof(PhysicsStruct));

                To read from the shared memory we have,

                if(m_pViewMMFFile )
                CopyMemory((PVOID)&s_physics,m_pViewMMFFile,sizeof(SPageFilePhysics));

                I tested that and it worked like a charm. Also many thanks to the guy who wrote MMFAPP1 in the first place. It saved my neck. Also Richard thanks for your help as well.

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                Glad you got it sorted. :)

                The difficult we do right away... ...the impossible takes slightly longer.

                D 1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  Glad you got it sorted. :)

                  The difficult we do right away... ...the impossible takes slightly longer.

                  D Offline
                  D Offline
                  D_code_writer
                  wrote on last edited by
                  #8

                  So am I!! Thanks again for your help

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups