[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Re: Trouble About File Mapping (I need some help please!

Willy Denoyette

8/24/2003 10:33:00 AM

Leonardo Lanni wrote:
|| I've create a filemapping succesfully into a process (using c
|| language with windows library) with its name included into the
|| parameters.
|| Then I wrote another process in c language in which I need to attach
|| this filemapping to the memory of the process. First I've opened the
|| filemapping with OpenFileMapping (passing the name of filemapping
|| given in the first process) and getting the Handle to it, and then I
|| used MapViewOfFile in order to attach the FileMapping to the process
|| (using the handle).
|| But I get this result :
|| If I execute the first program, then I let it opened and I execute
|| the second, I succeded in attaching the filemapping to the second
|| process,
|| but If I execute the first, close it and then execute the second, I
|| get an error (Can't open the filemapping)
|| Why this? Once created the filemapping by the first process, the
|| second one should be able to open and attach it for he has in the
|| parameters the name of it!
|| Maybe (I thought) if I close the first process, there is no Handle
|| pointing to the file mapping so that it's cancelled (garbage
|| collection), but if it is the cause I think is not so intelligent,
|| isn't it?
|| I'd like an answer, it is very important (university thesis), I also
|| add part of this code,
|| Thank you a lot!
|| PROCESS #1:
|| HANDLE shmem1;
|| shmem1 =
|| CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,500000,"my_mapp
|| ing");
|| ...
|| PROCESS #2:
|| HANDLE shmem;
|| char *attacco;
|| shmem = OpenFileMapping(FILE_MAP_ALL_ACCESS,TRUE,"my_mapping");
|| attacco =(char*)MapViewOfFile(shmem,FILE_MAP_WRITE,0,0,0);
|| ..
|| I've done a check to the Handle returned by the OpenFileMapping, and
|| it is NULL so it can't open the file mapping!
|| ( if (shmem == NULL) printf("Error : Cannot Open File Mapping!"); )

The shared memory object will be destroyed as soon as process#1 terminates (unless another process has an open handle to the
object).
In your case Process#2 can only open a FileMapping if process#1 is running (and has not called CloseHandle for the object).
So what you see is quite normal, what did you expect? If no process has an open handle to a shared memory object, what would there
to be shared????
Willy.