Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1772
  • Last Modified:

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.
0
dvplayltd
Asked:
dvplayltd
  • 6
  • 2
2 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now