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