讀數據信息:
public int ReadLengthAndCount()
{
Byte[] bytData = new Byte[infoSize];
if (infoSize > m_MemSize) return 2; //超出數據區
if (m_bInit)
{
Marshal.Copy(m_pwData, bytData, 0, infoSize);
}
else
{
return 1; //共享內存未初始化
}
String str = System.Text.Encoding.Unicode.GetString(bytData).Trim('');
String [] strs = System.Text.RegularExpressions.Regex.Split(str,"");
m_length = System.Convert.ToInt32(strs[0]);
m_count = System.Convert.ToInt32(strs[1]);
return 0; //讀成功
}
寫數據信息:
public int WriteLengthAndCount(int length, int count)
{
semWriteLength.WaitOne();
if (infoSize > m_MemSize) return 2; //超出數據區
String strLengthAndCount = System.Convert.ToString(length) + "" + System.Convert.ToString(count);
Byte[] bytData = System.Text.Encoding.Unicode.GetBytes(strLengthAndCount);
if (m_bInit)
{
Marshal.Copy(bytData, 0, m_pwData, bytData.Length);
}
else
{
semWriteLength.Release();
return 1; //共享內存未初始化
}
semWriteLength.Release();
return 0;
}
源碼(工程文件)下載地址:
http://download.csdn.Net/source/798731
本工程在以下環境下編譯通過:
Windows XP SP3
Microsoft VisualC# 2005
Microsoft VisualStudio 2005 V8.0.50727.42
Microsoft .Net Framework V2.0.50727