Memory sharing between processes

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.