How to use CreateFileMapping with unnamed file?

Hello experts!

I use two (or more) EXEs that exchange large data (array with colors). I use Memory Mapped file, since it is best speed.

In first EXE I have next code that is OK.
HANDLE hMapedFile = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, biSizeImage, NULL)

In second EXE I have to use CreateDIBSection and create DIB based on hMappedFile. In second EXE I has value (witch is acutely DWORD) of hMapedFile. Simple trying CreateDIBSection and hMappedFile fail.
If I create named Mapped File in first EXE, open this named mapped file in second EXE and then create DIB based at opened named mapped file it works.

But how to create DIB (in second EXE) using handle hMappedFile (from first EXE )? I want to escape naming, because I have to transfer it name between two EXEs and provide unique name.
dvplayltdAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
Hi dvplayltd,
Check out the code in the following link:
http://code.axter.com/MapFileToMemory.h

David Maisonave (Axter)
Cheers!
0
AxterCommented:
Sorry, wrong link.

Look at the following instead:
http://code.axter.com/sharemem.h
0
AxterCommented:
Basically, you want to pass INVALID_HANDLE_VALUE to the first argument to CreateFileMapping
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

AxterCommented:
Here's another example:
http://code.axter.com/SharedBufferPortableClass.cpp

So instead of creating a file, you skip that step and just pass the invalid handle to CreateFileMapping.
That lets you use an unnamed file.
0
dvplayltdAuthor Commented:
ok, Axter, creation of unnamed is easy but question is how to OPEN unnamed file !
0
jkrCommented:
>>In second EXE I have to use CreateDIBSection and create DIB based on hMappedFile. In second EXE I has value
>>(witch is acutely DWORD) of hMapedFile

You'll need to call 'DuplicateHandle()' for the mapping handle if you want to use it in another process.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AxterCommented:
>>ok, Axter, creation of unnamed is easy but question is how to OPEN unnamed file !

The last link I posted has an example.
To open it, you just need the name of the mapview.

Example:
//App creating the map view
m_MapHandle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, m_SizeOfBuffer, m_NameOfMyMapView);

//Second app opening the map view
m_MapHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE, m_NameOfMyMapView);


Even though you're creaing a map via an unnamed file, the mapview itself still has a name, which you can use to open it with.
0
dvplayltdAuthor Commented:
Axter, I try to escape naming, because I have to create many (500-1000) Mapped files per every 5-10 minutes ! And I have to transfer names between some EXEs and this is more fast if I use DWORD (HANDLE) than any unique  STRING.

I think that DuplicateHandle is what I need- it will allow trasfer of handles between EXEs.
0
AxterCommented:
>>I think that DuplicateHandle is what I need- it will allow trasfer of handles between EXEs.

You can just pass an index number instead, and use an index number to open the file.
int MyIndex = 123;

sprintf(NameOfMyMapView, "MyApp%i", MyIndex);
m_MapHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE, NameOfMyMapView);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.