
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
1056
|
Chapter 18: Threading and Synchronization
// Make an array to hold the bytes.
byte[] bytes = new byte[count];
// Read out the bytes for the object.
Marshal.Copy(_ptrToMemory, bytes, 0, count);
// Set up the memory stream with the object bytes.
using (MemoryStream ms = new MemoryStream(bytes))
{
// Set up a binary formatter.
BinaryFormatter formatter = new BinaryFormatter( );
// Get the object to return.
TransferItemType item;
try
{
item = (TransferItemType)formatter.Deserialize(ms);
}
finally
{
// Signal that we received the object using the mutex.
_mtxSharedMem.ReleaseMutex( );
}
// Give them the object.
return item;
}
}
#endregion
}
public class PInvoke
{
#region PInvoke defines
[Flags]
public enum PageProtection : uint
{
NoAccess = 0x01,
Readonly = 0x02,
ReadWrite = 0x04,
WriteCopy = 0x08,
Execute = 0x10,
ExecuteRead = 0x20,
ExecuteReadWrite = 0x40,
ExecuteWriteCopy = 0x80,
Guard = 0x100,
NoCache = 0x200,
WriteCombine = 0x400,
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr CreateFileMapping(IntPtr hFile,
IntPtr lpFileMappingAttributes, PageProtection flProtect,
Example 18-12. SharedMemoryManager and PInvoke classes (continued)