471,851 Members | 999 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,851 software developers and data experts.

MemoryMappedFile Problem

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
Jul 1 '08 #1
3 5333
my error occurs when i try to convert the handle from the mmf to a
GraphicObject

gfxScreenshot = Graphics.FromHdcInternal(picPointer);
Jul 1 '08 #2
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]
- mv*@spam.guard.caspershouse.com

"Martin Priebe" <ma***********@hermos.comwrote in message
news:OY**************@TK2MSFTNGP02.phx.gbl...
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


Jul 1 '08 #3
Thx.
it works.
I write the byteArray to the mapped file.
(Remoting is not fast enough.)

greetings
Martin
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comschrieb
im Newsbeitrag news:%2****************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Martin Priebe" <ma***********@hermos.comwrote in message
news:OY**************@TK2MSFTNGP02.phx.gbl...
>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



Jul 2 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Bruce Davis | last post: by
11 posts views Thread by Kostatus | last post: by
9 posts views Thread by Sudesh Sawant | last post: by
117 posts views Thread by Peter Olcott | last post: by
28 posts views Thread by Jon Davis | last post: by
6 posts views Thread by Ammar | last post: by
2 posts views Thread by Mike Collins | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.