C # shared memory operations

Source: Internet
Author: User

The following is a class for C # Memory operations. You only need to add the following class to the corresponding project, and this project can directly operate on the memory!

Using system. runtime. interopservices; // to add the following namespace, use dllimport

Public class sharememlib
{

[Dllimport ("user32.dll", charset = charset. Auto)]

Public static extern intptr sendmessage (intptr hwnd, int MSG, int wparam, intptr lparam );

 

[Dllimport ("kernel32.dll", charset = charset. Auto)]

Public static extern intptr createfilemapping (INT hfile, intptr lpattributes, uint flprotect, uint dwmaxsizehi, uint dwmaxsizelow, string lpname );

 

[Dllimport ("kernel32.dll", charset = charset. Auto)]

Public static extern intptr openfilemapping (INT dwdesiredaccess, [financialas (unmanagedtype. bool)] bool binherithandle, string lpname );

 

[Dllimport ("kernel32.dll", charset = charset. Auto)]

Public static extern intptr mapviewoffile (intptr hfilemapping, uint dwdesiredaccess, uint dwfileoffsethigh, uint dwfileoffsetlow, uint dwnumberofbytestomap );

 

[Dllimport ("kernel32.dll", charset = charset. Auto)]

Public static extern bool unmapviewoffile (intptr pvbaseaddress );

 

[Dllimport ("kernel32.dll", charset = charset. Auto)]

Public static extern bool closehandle (intptr handle );

 

[Dllimport ("Kernel32", entrypoint = "getlasterror")]

Public static extern int getlasterror ();

 

Const int error_already_exists = 183;

 

Const int file_map_copy = 0x0001;

Const int file_map_write = 0x0002;

Const int file_map_read = 0x0004;

Const int file_map_all_access = 0x0002 | 0x0004;

 

Const int page_readonly = 0x02;

Const int page_readwrite = 0x04;

Const int page_writecopy = 0x08;

Const int page_execute = 0x10;

Const int page_execute_read = 0x20;

Const int page_execute_readwrite = 0x40;

 

Const int sec_commit = 0x8000000;

Const int sec_image = 0x1000000;

Const int sec_nocache = 0x10000000;

Const int sec_reserve = 0x4000000;

 

Const int invalid_handle_value =-1;

 

Intptr m_hsharedmemoryfile = intptr. zero;

Intptr m_pwdata = intptr. zero;

Bool m_balreadyexist = false;

Bool m_binit = false;

Long m_memsize = 0;

 

Public sharememlib ()

{

}

~ Sharememlib ()

{

Close ();

}

 

// Initialize the memory

Public int Init (string strname, long lngsize)

{

If (lngsize <= 0 | lngsize> 0x00800000) lngsize = 0x00800000;

M_memsize = lngsize;

If (strname. length> 0)

{

// Create a memory shared body (invalid_handle_value)

M_hsharedmemoryfile = createfilemapping (invalid_handle_value, intptr. Zero, (uint) page_readwrite, 0, (uint) lngsize, strname );

If (m_hsharedmemoryfile = intptr. Zero)

{

M_balreadyexist = false;

M_binit = false;

Return 2; // An error occurred while creating the shared body.

}

Else

{

If (getlasterror () = error_already_exists) // already created

{

M_balreadyexist = true;

}

Else

{

M_balreadyexist = false;

}

}

// Create memory ing

M_pwdata = mapviewoffile (m_hsharedmemoryfile, file_map_write, 0, 0, (uint) lngsize );

If (m_pwdata = intptr. Zero)

{

M_binit = false;

Closehandle (m_hsharedmemoryfile );

Return 3; // failed to create memory ing

}

Else

{

M_binit = true;

If (m_balreadyexist = false)

{

}

}

}

Else

{

Return 1;

}

 

Return 0;

}

// Disable shared memory

Public void close ()

{

If (m_binit)

{

Unmapviewoffile (m_pwdata );

Closehandle (m_hsharedmemoryfile );

}

}

/// Read data

Public int read (ref byte [] bytdata, int lngaddr, int lngsize)

{

If (lngaddr + lngsize> m_memsize) return 2; // exceeds the data Zone

If (m_binit)

{

Marshal. Copy (m_pwdata, bytdata, lngaddr, lngsize );

}

Else

{

Return 1;

}

Return 0;

}

 

// Write data to the shared memory

Public int write (byte [] bytdata, int lngaddr, int lngsize)

{

If (lngaddr + lngsize> m_memsize) return 2; // exceeds the data Zone

If (m_binit)

{

Marshal. Copy (bytdata, lngaddr, m_pwdata, lngsize );

}

Else

{

Return 1;

}

Return 0;

}

}

Call method:

Declare sharememery. sharememlib memdb = new sharememlib (); as a public variable.

// Use the following sentence to call the method for initializing shared memory in the above class

If (memdb. INIT ("yfmemtest", 10240 )! = 0)

Convert data that needs to be written in memory into byte []

Memdb. Write (bytdata, 0, BB. Length); // write data to the shared memory.

Byte [] bytdata = new byte [16];
Int intret = memdb. Read (ref bytdata, 0, 16 );

The above method reads 16 bytes of content in the memory.

The above method has been tested in vs2008.

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.