今日在項目中碰到一個問題
Update: 其實開始想到的方案還包括Remoting
那麼什麼解決方案好呢?眾所周知
關鍵API類
view sourceprint?public static class API
{
[DllImport(
public static extern IntPtr SendMessage(IntPtr hWnd
[DllImport(
public static extern IntPtr CreateFileMapping(int hFile
[DllImport(
public static extern IntPtr OpenFileMapping(int dwDesiredAccess
[DllImport(
public static extern IntPtr MapViewOfFile(IntPtr hFileMapping
[DllImport(
public static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);
[DllImport(
public static extern bool CloseHandle(IntPtr handle);
[DllImport(
public static extern int GetLastError();
[DllImport(
public static extern int memcmp(IntPtr ptr
[DllImport(
public static extern unsafe int memcmp(void* ptr
[DllImport(
public static extern int RtlCompareMemory(IntPtr Destination
public const int ERROR_ALREADY_EXISTS =
public const int FILE_MAP_COPY =
public const int FILE_MAP_WRITE =
public const int FILE_MAP_READ =
public const int FILE_MAP_ALL_ACCESS =
public const int PAGE_READONLY =
public const int PAGE_READWRITE =
public const int PAGE_WRITECOPY =
public const int PAGE_EXECUTE =
public const int PAGE_EXECUTE_READ =
public const int PAGE_EXECUTE_READWRITE =
public const int SEC_COMMIT =
public const int SEC_IMAGE =
public const int SEC_NOCACHE =
public const int SEC_RESERVE =
public const int INVALID_HANDLE_VALUE =
}
代碼實現
view sourceprint?public enum MemoryResult
{
Success
NotInitialized
NoChange
Failed
}
public class ShareMemory<T> : IDisposable
where T : class
{
IntPtr m_hSharedMemoryFile = IntPtr
IntPtr m_pwData = IntPtr
bool m_bAlreadyExist = false;
bool m_bInit = false;
long m_MemSize =
int m_size;
byte[] m_lastData;
public ShareMemory()
{
}
~ShareMemory()
{
Close();
}
/// <summary>
/// 初始化共享內存
/// </summary>
public MemoryResult Init(string strName)
{
m_size =
var lngSize = m_size;
if (lngSize <=
m_MemSize = lngSize;
if (strName
{
//創建內存共享體 (INVALID_HANDLE_VALUE)
m_hSharedMemoryFile = API
if (m_hSharedMemoryFile == IntPtr
{
m_bAlreadyExist = false;
m_bInit = false;
return MemoryResult
}
else
{
if (API
{
m_bAlreadyExist = true;
}
else //新創建
{
m_bAlreadyExist = false;
}
}
//
//創建內存映射
m_pwData = API
if (m_pwData == IntPtr
{
m_bInit = false;
API
return MemoryResult
}
else
{
m_bInit = true;
if (m_bAlreadyExist == false)
{
//初始化
}
}
//
}
else
{
return MemoryResult
}
return MemoryResult
}
/// <summary>
/// 關閉共享內存
/// </summary>
public void Close()
{
if (m_bInit)
{
API
API
}
}
/// <summary>
/// 讀數據
/// </summary>
public unsafe MemoryResult Read(out T obj)
{
obj = default(T);
byte[] bytData = new byte[m_size];
if (m_bInit)
{
Marshal
if (m_lastData != null)
{
fixed (byte* p
{
fixed (byte* p
{
if (mcmp(p
return MemoryResult
}
}
}
m_lastData = bytData;
var fmt = new BinaryFormatter();
using (var ms = new MemoryStream(bytData))
{
try
{
obj = (T)fmt
}
catch (SerializationException)
{
return MemoryResult
}
}
}
else
{
return MemoryResult
}
return MemoryResult
}
/// <summary>
/// 寫數據
/// </summary>
public MemoryResult Write(T obj)
{
var fmt = new BinaryFormatter();
byte[] bytData;
if (obj is byte[])
bytData = obj as byte[];
else
{
using (var ms = new MemoryStream())
{
fmt
bytData = ms
}
}
if (m_bInit)
Marshal
else
return MemoryResult
return MemoryResult
}
#region IDisposable Members
public void Dispose()
{
Close();
}
#endregion
}
使用也很簡單
view sourceprint?寫數據
if (mem == null)
{
mem = new ShareMemory<ChannelStatus[]>();
mem
}
var xml = PhoneCallHelper
foreach (XmlElement ele in xml
{
var status = GetChannelStatusFromXElement(ele);
_status[status
}
mem
讀數據
using (var mem = new ShareMemory<ChannelStatus[]>())
{
if (mem
throw new Exception(
ChannelStatus[] status;
if (mem
return status;
return null;
}
From:http://tw.wingwit.com/Article/program/net/201311/13598.html