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. FileMapping book exmple goes in loop

FileMapping book exmple goes in loop

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancetutoriallearning
4 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.
  • C Offline
    C Offline
    coco243
    wrote on last edited by
    #1

    Hi, I am following an example from a book and it doesn't work fine. In a win32 c++ project I am trying to make the following codes to work: At the window creation the mapped file is created

    case WM_CREATE:
    {
    // Create a file mapped object.
    //.............................
    hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL,
    PAGE_READWRITE,
    0,
    1024,
    "MyFileMappingObject" );

    		// Return if the mapping failed.
    		//..............................
    		if ( !hMapFile )
    			return(-1);
    	}
    	break;
    

    There are made modification in mapped file in the following thread function:

    DWORD AnotherProcess( LPDWORD lpdwParam )
    {
    HANDLE hFileMap;
    LPTSTR lpMapAddress;

    // Open the file mapping.
    //.......................
    hFileMap = OpenFileMapping( FILE\_MAP\_ALL\_ACCESS, FALSE,
    	                        "MyFileMappingObject" );
    
    // Map a view to the mapping object.
    //..................................
    lpMapAddress =(LPTSTR)MapViewOfFileEx( hFileMap, FILE\_MAP\_ALL\_ACCESS,
    	                            0, 0, 0, lpdwParam );
    
    // Display the contents of the memory.
    //....................................
    MessageBox( NULL, lpMapAddress, "Mapped Memmory",
    	        MB\_OK | MB\_ICONINFORMATION );
    
    // Set "Received" into the memory.
    //................................
    strcpy( lpMapAddress, "Received" );
    
    UnmapViewOfFile( lpMapAddress );
    
    return( 0 );
    

    }

    There are made modification on mapped file and the thread function is called on the following code:

    case IDM_TEST:
    {
    DWORD dwID;
    // Map a view of the file mapped object.
    //......................................
    lpMapAddress = (LPTSTR)MapViewOfFile( hMapFile,
    FILE_MAP_ALL_ACCESS,
    0, 0, 0 );

    			// Place data int the view.
    			//.........................
    			strcpy(lpMapAddress, "Data passed from main process.");
    
    			// Make sure the data is flushed to the file.
    			//...........................................
    			FlushViewOfFile( lpMapAddress, 
    				             lstrlen( lpMapAddress ) );
    
    			// Create a thread to receive the data.
    			//.....................................
    			hThread = CreateThread( NULL, 0, 
    				      (LPTHREAD\_START\_ROUTINE)AnotherProcess, 
    					  lpMapAddress, 0, &dwID);
    
    			// Set a timer to wait for the data to be processed.
    			//.......
    
    Mircea NeacsuM 1 Reply Last reply
    0
    • C coco243

      Hi, I am following an example from a book and it doesn't work fine. In a win32 c++ project I am trying to make the following codes to work: At the window creation the mapped file is created

      case WM_CREATE:
      {
      // Create a file mapped object.
      //.............................
      hMapFile = CreateFileMapping( (HANDLE)0xFFFFFFFF, NULL,
      PAGE_READWRITE,
      0,
      1024,
      "MyFileMappingObject" );

      		// Return if the mapping failed.
      		//..............................
      		if ( !hMapFile )
      			return(-1);
      	}
      	break;
      

      There are made modification in mapped file in the following thread function:

      DWORD AnotherProcess( LPDWORD lpdwParam )
      {
      HANDLE hFileMap;
      LPTSTR lpMapAddress;

      // Open the file mapping.
      //.......................
      hFileMap = OpenFileMapping( FILE\_MAP\_ALL\_ACCESS, FALSE,
      	                        "MyFileMappingObject" );
      
      // Map a view to the mapping object.
      //..................................
      lpMapAddress =(LPTSTR)MapViewOfFileEx( hFileMap, FILE\_MAP\_ALL\_ACCESS,
      	                            0, 0, 0, lpdwParam );
      
      // Display the contents of the memory.
      //....................................
      MessageBox( NULL, lpMapAddress, "Mapped Memmory",
      	        MB\_OK | MB\_ICONINFORMATION );
      
      // Set "Received" into the memory.
      //................................
      strcpy( lpMapAddress, "Received" );
      
      UnmapViewOfFile( lpMapAddress );
      
      return( 0 );
      

      }

      There are made modification on mapped file and the thread function is called on the following code:

      case IDM_TEST:
      {
      DWORD dwID;
      // Map a view of the file mapped object.
      //......................................
      lpMapAddress = (LPTSTR)MapViewOfFile( hMapFile,
      FILE_MAP_ALL_ACCESS,
      0, 0, 0 );

      			// Place data int the view.
      			//.........................
      			strcpy(lpMapAddress, "Data passed from main process.");
      
      			// Make sure the data is flushed to the file.
      			//...........................................
      			FlushViewOfFile( lpMapAddress, 
      				             lstrlen( lpMapAddress ) );
      
      			// Create a thread to receive the data.
      			//.....................................
      			hThread = CreateThread( NULL, 0, 
      				      (LPTHREAD\_START\_ROUTINE)AnotherProcess, 
      					  lpMapAddress, 0, &dwID);
      
      			// Set a timer to wait for the data to be processed.
      			//.......
      
      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Took me some time to spot the problem (I think):

      lpMapAddress =(LPTSTR)MapViewOfFileEx( hFileMap, FILE_MAP_ALL_ACCESS,
      0, 0, 0, lpdwParam );

      This doesn't make much sense: the last argument of the function is the address where you want it mapped. So you are kind of mapping the memory where it was already mapped. I's suggest to change it to:

      lpMapAddress =(LPTSTR)MapViewOfFile( hFileMap, FILE_MAP_ALL_ACCESS,
      0, 0, 0);

      If you want a shared memory implementation that has been tested, you can find one here. The source file is mlib/src/shmem.cpp

      Mircea

      C 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Took me some time to spot the problem (I think):

        lpMapAddress =(LPTSTR)MapViewOfFileEx( hFileMap, FILE_MAP_ALL_ACCESS,
        0, 0, 0, lpdwParam );

        This doesn't make much sense: the last argument of the function is the address where you want it mapped. So you are kind of mapping the memory where it was already mapped. I's suggest to change it to:

        lpMapAddress =(LPTSTR)MapViewOfFile( hFileMap, FILE_MAP_ALL_ACCESS,
        0, 0, 0);

        If you want a shared memory implementation that has been tested, you can find one here. The source file is mlib/src/shmem.cpp

        Mircea

        C Offline
        C Offline
        coco243
        wrote on last edited by
        #3

        Thank you for the rapid response. Indeed, now, the code works as I think it should. I supposed that this was the central idea, to modify the mapped file from two different places, but appears that something went wrong. I supposed that the LPWORD type of lpdwParam was the problem but I haven't seen a solution. It appears that it does't like the start address but the all file to be mapped. Anyway it seems that it's working, and now I can move on in the learning process beacause I want to get the ideea not to stay to long on each topic. Thank you very much Mircea for your response and for the links. Thank you.

        Mircea NeacsuM 1 Reply Last reply
        0
        • C coco243

          Thank you for the rapid response. Indeed, now, the code works as I think it should. I supposed that this was the central idea, to modify the mapped file from two different places, but appears that something went wrong. I supposed that the LPWORD type of lpdwParam was the problem but I haven't seen a solution. It appears that it does't like the start address but the all file to be mapped. Anyway it seems that it's working, and now I can move on in the learning process beacause I want to get the ideea not to stay to long on each topic. Thank you very much Mircea for your response and for the links. Thank you.

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          You are very welcome!

          Mircea

          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