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. Unable to write into Shared memory

Unable to write into Shared memory

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

    I have a Delphi application which Creates a shared memory uses CreateFileMapping, OpenFileMapping, MapViewOfFile functions. Now I wanted to share the same memory for my MFC application. I used the OpenFileMapping, MapViewOfFile functions. I created a structure exactly same in size as the Delphi application and mapped the structure object. sample code: **HANDLE hMapObject2; hMapObject2 = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, "PP101U3_SHARED"); if( !hMapObject2 ) { AfxMessageBox("Failed to open Simpack DataBase"); return( 0 ); } Simpack = ( struct SIMPACKDB *) MapViewOfFile( hMapObject2, FILE_MAP_ALL_ACCESS, 0, 0, 0 ); if( !Simpack ) { AfxMessageBox("Failed to create Simpack File Map View"); return(0); } Esim->SPV1 = Simpack->SP_Z;**I am able to read the values exactly correct for all the member variables in the structure. But when I try to write value in the shared memory, its not changing. It shows the previous value immediately. The value of Simpack->SP_Z[15] is 0.5010 as read from the shared memory which is set by the Delphi application. When I set or write the value of the same variable to the shared memory using the code: **Simpack->SP_Z[15] = 0.6123;**it shows the previous value 0.5010. When I change the same variables value in the Delphi application it changes and shows the changed value here in the MFC application. Please help me how to write the values in the shared memory. Is there anything wrong in the code?

    T 1 Reply Last reply
    0
    • M manoharbalu

      I have a Delphi application which Creates a shared memory uses CreateFileMapping, OpenFileMapping, MapViewOfFile functions. Now I wanted to share the same memory for my MFC application. I used the OpenFileMapping, MapViewOfFile functions. I created a structure exactly same in size as the Delphi application and mapped the structure object. sample code: **HANDLE hMapObject2; hMapObject2 = OpenFileMapping( FILE_MAP_ALL_ACCESS, FALSE, "PP101U3_SHARED"); if( !hMapObject2 ) { AfxMessageBox("Failed to open Simpack DataBase"); return( 0 ); } Simpack = ( struct SIMPACKDB *) MapViewOfFile( hMapObject2, FILE_MAP_ALL_ACCESS, 0, 0, 0 ); if( !Simpack ) { AfxMessageBox("Failed to create Simpack File Map View"); return(0); } Esim->SPV1 = Simpack->SP_Z;**I am able to read the values exactly correct for all the member variables in the structure. But when I try to write value in the shared memory, its not changing. It shows the previous value immediately. The value of Simpack->SP_Z[15] is 0.5010 as read from the shared memory which is set by the Delphi application. When I set or write the value of the same variable to the shared memory using the code: **Simpack->SP_Z[15] = 0.6123;**it shows the previous value 0.5010. When I change the same variables value in the Delphi application it changes and shows the changed value here in the MFC application. Please help me how to write the values in the shared memory. Is there anything wrong in the code?

      T Offline
      T Offline
      TomasRiker2
      wrote on last edited by
      #2

      It could be an issue of caching ... Try to declare the structure instance with "volatile" and see if that helps.

      Visit my project: Derivative Calculator

      L 1 Reply Last reply
      0
      • T TomasRiker2

        It could be an issue of caching ... Try to declare the structure instance with "volatile" and see if that helps.

        Visit my project: Derivative Calculator

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Hi german "volatile" will not work. The memory must be created without cache. Try to find the "no cache"flat. And there is a better way: you can also use a segment like the following to share data:

        #pragma data_seg("Share")
        BOOL existed = FALSE;
        #pragma data_seg()
        #pragma comment(linker, "/section:Share,rws")

        IMPORTANT: the no cache flag is also needed here. please refer to MSDN, to find the flag.

        L 1 Reply Last reply
        0
        • L Lost User

          Hi german "volatile" will not work. The memory must be created without cache. Try to find the "no cache"flat. And there is a better way: you can also use a segment like the following to share data:

          #pragma data_seg("Share")
          BOOL existed = FALSE;
          #pragma data_seg()
          #pragma comment(linker, "/section:Share,rws")

          IMPORTANT: the no cache flag is also needed here. please refer to MSDN, to find the flag.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          CreateFileMapping

          DWORD flProtect, or SEC_NOCACHE And #pragma comment(linker, "/section:Share,rwsk") both will work.

          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