FileMapping book exmple goes in loop
-
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. //.......
-
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. //.......
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
-
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
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.
-
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.
You are very welcome!
Mircea