MemoryMappedFile Problem

  • Thread starter Thread starter Martin Priebe
  • Start date Start date
M

Martin Priebe

Hi,

i tried to use MMF to communicate between 2 processes.
i want to transfer a bitmap between the 2 processes.

i created a mmf in my first process

public IntPtr CreateMMF(FileAccess access, int size)

{

if (size < 0)

throw new ArgumentException("The size parameter should be a number greater
than Zero.");

IntPtr memoryFileHandle = CreateFileMapping(0xFFFFFFFF, IntPtr.Zero,
(uint)access, 0, (uint)size, "MMF");

if (memoryFileHandle == IntPtr.Zero)

throw new Exception("Creating Shared Memory failed.");

return memoryFileHandle;

}



i mapped it and write the graphic to it

IntPtr mappedViewHandle = MapViewOfFile(mmfHandle, (uint)FILE_MAP_WRITE, 0,
0, 0);

if (mappedViewHandle == IntPtr.Zero)

throw new Exception("Creating a view of Shared Memory failed.");

Marshal.WriteIntPtr(mappedViewHandle, gfxScreenshot.GetHdc());

UnmapViewOfFile(mappedViewHandle);

CloseHandle((uint)mappedViewHandle);



my other process mapped the file but throw an exception "not enough memory"

IntPtr mappedFileHandle = OpenFileMapping((int)FileAccess.ReadWrite, false,
"MMF");

if (mappedFileHandle == IntPtr.Zero)

throw new Exception("Opening the Shared Memory for Read failed.");

//here the Exception is thrown

IntPtr mappedViewHandle = MapViewOfFile(mappedFileHandle,
(uint)FILE_MAP_READ, 0, 0, 0);

if (mappedViewHandle == IntPtr.Zero)

throw new Exception("Creating a view of Shared Memory failed.");

IntPtr windowHandle = Marshal.ReadIntPtr(mappedViewHandle);

if (windowHandle == IntPtr.Zero)

throw new ArgumentException("Reading from the specified address in Shared
Memory failed.");

UnmapViewOfFile(mappedViewHandle);

CloseHandle((uint)mappedFileHandle);

return windowHandle;



CreateMappedFile i use a size of 200000. (nearly 200 KB)





Pleayse help me.

greetings

Martin
 
my error occurs when i try to convert the handle from the mmf to a
GraphicObject

gfxScreenshot = Graphics.FromHdcInternal(picPointer);
 
Martin,

The problem here is with this line:

Marshal.WriteIntPtr(mappedViewHandle, gfxScreenshot.GetHdc());

You are writing only the handle to the pointer. That DC handle is not
going to be valid outside of the process that created it.

What you need to do is write all the bitmap data to the memory mapped
file, and then read it back out on the other side.

Curious, why aren't you using Remoting or WCF to do this for you? You
could have transferred your Bitmap using much simpler code. My suggestion,
use Named Pipes as the channel if you go this route.
 
Thx.
it works.
I write the byteArray to the mapped file.
(Remoting is not fast enough.)

greetings
Martin


Nicholas Paldino said:
Martin,

The problem here is with this line:

Marshal.WriteIntPtr(mappedViewHandle, gfxScreenshot.GetHdc());

You are writing only the handle to the pointer. That DC handle is not
going to be valid outside of the process that created it.

What you need to do is write all the bitmap data to the memory mapped
file, and then read it back out on the other side.

Curious, why aren't you using Remoting or WCF to do this for you? You
could have transferred your Bitmap using much simpler code. My
suggestion, use Named Pipes as the channel if you go this route.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Martin Priebe said:
Hi,

i tried to use MMF to communicate between 2 processes.
i want to transfer a bitmap between the 2 processes.

i created a mmf in my first process

public IntPtr CreateMMF(FileAccess access, int size)

{

if (size < 0)

throw new ArgumentException("The size parameter should be a number
greater than Zero.");

IntPtr memoryFileHandle = CreateFileMapping(0xFFFFFFFF, IntPtr.Zero,
(uint)access, 0, (uint)size, "MMF");

if (memoryFileHandle == IntPtr.Zero)

throw new Exception("Creating Shared Memory failed.");

return memoryFileHandle;

}



i mapped it and write the graphic to it

IntPtr mappedViewHandle = MapViewOfFile(mmfHandle, (uint)FILE_MAP_WRITE,
0, 0, 0);

if (mappedViewHandle == IntPtr.Zero)

throw new Exception("Creating a view of Shared Memory failed.");

Marshal.WriteIntPtr(mappedViewHandle, gfxScreenshot.GetHdc());

UnmapViewOfFile(mappedViewHandle);

CloseHandle((uint)mappedViewHandle);



my other process mapped the file but throw an exception "not enough
memory"

IntPtr mappedFileHandle = OpenFileMapping((int)FileAccess.ReadWrite,
false, "MMF");

if (mappedFileHandle == IntPtr.Zero)

throw new Exception("Opening the Shared Memory for Read failed.");

//here the Exception is thrown

IntPtr mappedViewHandle = MapViewOfFile(mappedFileHandle,
(uint)FILE_MAP_READ, 0, 0, 0);

if (mappedViewHandle == IntPtr.Zero)

throw new Exception("Creating a view of Shared Memory failed.");

IntPtr windowHandle = Marshal.ReadIntPtr(mappedViewHandle);

if (windowHandle == IntPtr.Zero)

throw new ArgumentException("Reading from the specified address in Shared
Memory failed.");

UnmapViewOfFile(mappedViewHandle);

CloseHandle((uint)mappedFileHandle);

return windowHandle;



CreateMappedFile i use a size of 200000. (nearly 200 KB)





Pleayse help me.

greetings

Martin
 
Back
Top