From: AliReza K. M. <kho...@gm...> - 2009-04-02 16:13:21
|
> Quoting AliReza Khoshgoftar Monfared <kho...@gm...>: > > > > > Initially, I thought the solution of Shared Memory should work, as both > > Visual Studio ,and MinGW understand CreateFileMapping(), and > > OpenFileMapping() > > But now I see that whatever I write to named shared memory in visual > > studio, I cannot read in MinGW. > > I even tried to write a simple string like "Hello World" in Visual > Studio, > > and read it from the Shared Memory in MinGW, but the string isn't there > > apparently. > > > So, my question: > > *Is it possible to share a memory space between Visual Studio ,and > > MinGW?* > > I don't know why you shouldn't be able to unless your perhaps you're > using C++ semantics. Can you give an example of what you have in > MinGW vs what you have in VS? This way we can see what may be wrong. > > -- > Earnie Hi, Thanks for the replies, So, to clarify my intention, let's speak about the code, For the time being, I am only trying to make sure I can share something between a VC program, and a MinGW program. Let's say, in the simplest scenario, in one , I write to a shared memory(code is simplified with only the major function calls to avoid making things complicated): hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, > NULL, > PAGE_READWRITE, > 0, > (DWORD)MapSize, > "Global\\MyFileMappingObject"); > > hMapView = (LPSTR) MapViewOfFile(hMapFile, > FILE_MAP_ALL_ACCESS, > 0, > 0, > 0); > > myString = (char *)malloc(sizeof(char)*256); > strcpy(myString, "Hello World!!"); > CopyMemory( (PVOID)hMapView, (const VOID *)myString, (SIZE_T)WriteSize); _getch(); > UnmapViewOfFile(hMapView); > CloseHandle(hMapFile); > free(myString); > In another, I read from the same shared memory: hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, > FALSE, > "Global\\MyFileMappingObject"); > hMapView = (LPSTR) MapViewOfFile(hMapFile, > FILE_MAP_ALL_ACCESS, //FILE_MAP_WRITE | FILE_MAP_READ, > 0, > 0, > 0); > myString = (char *)malloc(sizeof(char)*256); > CopyMemory( (PVOID)myString, (const VOID *)hMapView, (SIZE_T)ReadSize); > UnmapViewOfFile(hMapView); > CloseHandle(hMapFile); > free(myString); The codes do not seem to have an immediate bug, but here is the results I get: ** If I run both codes under MSVC++, I see the exact string "Hello World" in the second process. **If I run both under MinGW, I see the exact string again, but the shared memory is not cleaned for some reason (i.e. let's say after running the codes for the first time, now I want to change the shared string to "Hi World", I will see "Hi Worldld!!" in the second process)it only happens in MinGW, but not in MSVC++ **If I run the first code under MSVC++, and the second under MinGW, I cannot share anything at all (i.e both codes are compiled perfectly, but when I run the MinGW process, I see nothing [MSVC++ process of course should be kept open,and is.]) So, what I am asking is to know if the third test is feasible at all? And if yes, what's wrong with my program (why the same thing works when I run both codes in MSVC++, but doesn't when I run one in MinGW?) Thanks Alireza |