進程間記憶體共用問題

來源:互聯網
上載者:User
http://www.vkfz.com/net-CreateFileMapping-t55403.htm

playroc 發表於 2006-2-27 23:21:08 超級難題:.net 中CreateFileMapping 建立共用記憶體問題

.net中可以通過InteropServices調用unmanaged庫的方法CreateFileMapping等來建立和使用共用記憶體。但是如何將一個對象數組對應到建立的記憶體塊呢?這樣一來,記憶體建立後就不用管了,只要對對象數組進行操作就可以了,請高手指點。

------------------------------------------------------------------------------------------------------------------------------------
#region 非託管函式宣告
[DllImport("kernel32.dll",EntryPoint="OpenFileMapping",SetLastError=true, CharSet=CharSet.Auto) ]
private static extern IntPtr OpenFileMapping (int dwDesiredAccess, bool bInheritHandle,String lpName );

[DllImport("Kernel32.dll",EntryPoint="CreateFileMapping",SetLastError=true,CharSet=CharSet.Auto)]
private static extern IntPtr CreateFileMapping(uint hFile, IntPtr lpAttributes, uint flProtect,uint dwMaximumSizeHigh, uint dwMaximumSizeLow, string lpName);

[DllImport("Kernel32.dll")]
private static extern IntPtr MapViewOfFile(IntPtr hFileMappingObject,uint dwDesiredAccess, uint dwFileOffsetHigh,uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

[DllImport("Kernel32.dll",EntryPoint="UnmapViewOfFile",SetLastError=true,CharSet=CharSet.Auto)]
private static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);

[DllImport("kernel32.dll",EntryPoint="CloseHandle",SetLastError=true,CharSet=CharSet.Auto)]
private static extern bool CloseHandle(uint hHandle);

[DllImport("kernel32.dll",EntryPoint="GetLastError",SetLastError=true,CharSet=CharSet.Auto)]
private static extern uint GetLastError();
#endregion

struct Money
{
public int EmployeeNo;
public float Salary;
};

Money[] g_Money = new Money[100];
for(int i = 0; i < 100; i++)
{
g_Money[i] = new Money();
g_Money[i].EmployeeNo = i;
g_Money[i].Salary = i*i;
}

try
{
IntPtr memoryFileHandle = CreateFileMapping(0xFFFFFFFF,IntPtr.Zero,(uint)4,0,(uint)(100*8),"SHARE_MEMORY");
if(memoryFileHandle == IntPtr.Zero)
{
MessageBox.Show("Create Share Memory Failed!");
return;
}

g_hMoney = MapViewOfFile(memoryFileHandle,(uint)983071,0,0,(uint)(100*8));
if(g_hMoney == IntPtr.Zero)
{
MessageBox.Show("Create Share Memory Failed!");
return;
}

int basePos = g_hMoney.ToInt32();
for(int j = 0; j < 100; j++)
{
//我現在的做法,每次將數組對象g_Money一個一個的複製到記憶體中去
Marshal.StructureToPtr(g_Money[j], (IntPtr)(basePos + j * 8), true);
//我現在的做法,每次將記憶體中的資料複製到數組對象g_Money中
// g_Money[j] = (Money)Marshal.PtrToStructure((IntPtr)(basePos + j * 8), typeof(Money));
}
}
catch(System.Exception exception)
{
MessageBox.Show(exception.Message);
}

我想要得到的效果就是:只要訪問g_Money數組就可以直接存取記憶體,象VC中一樣,不要每次對g_Money賦值後再調用Marshal.StructureToPtr修改記憶體,而每次記憶體修改後又用Marshal.StructureToPtr讀取記憶體到g_Money來

============================
看了一些API,找了一些資料,對這個記憶體共用還不是很瞭解。但聽同事說在日誌管理上還是可以用得上的,特別是多進程間日誌管理,這個問題我遇到好久了,就先收集一些文章,到後面再慢慢的學習使用吧。
上面的代碼我還沒有測試過,看了一下,覺得有些錯誤,一會我自己試試,同時對這個記憶體共用問題也多瞭解一些。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.