By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,892 Members | 1,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,892 IT Pros & Developers. It's quick & easy.

MemoryMappedFile Problem

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
my error occurs when i try to convert the handle from the mmf to a
GraphicObject

gfxScreenshot = Graphics.FromHdcInternal(picPointer);
Jul 1 '08 #2

P: n/a
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

P: n/a
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.