Http://www.vkfz.com/net-CreateFileMapping-t55403.htm
Playroc was published on 23:21:08 super difficult:. Net createfilemapping creates shared memory
In. net, you can use interopservices to call the createfilemapping method of the unmanaged library to create and use shared memory. But how to map an object array to the created memory block? In this way, you don't need to worry about the memory after it is created. You only need to operate on the object array.
Certificate ------------------------------------------------------------------------------------------------------------------------------------
# Region unmanaged function declaration
[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 (0 xffffffff, intptr. Zero, (uint) 100, (uint) (* 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 ++)
{
// My current practice is to copy the array object g_money to the memory one by one
Marshal. structuretoptr (g_money [J], (intptr) (basepos + J * 8), true );
// My current practice is to copy the data in the memory to the array object g_money each time.
// G_money [J] = (money) Marshal. ptrtostructure (intptr) (basepos + J * 8), typeof (money ));
}
}
Catch (system. Exception exception)
{
MessageBox. Show (exception. Message );
}
The effect I want to achieve is that you can directly access the memory by accessing the g_money array. Like in VC, do not call Marshal every time you assign values to g_money. structuretoptr modifies the memory, and each time the memory is modified, Marshal is used. structuretoptr reads memory to g_money
======================================
I read some APIs and found some information. I still don't know much about Memory Sharing. However, I have heard from my colleagues that log management can still be used, especially for multi-process log management. I have encountered this problem for a long time and collected someArticleTo learn and use it later.
The aboveCodeI have not tested it yet. I have read it and found some errors. I will try it myself later and learn more about this memory sharing problem.