内存共享
?ø?Ì?ä??ÏíÄÚ?æ, ÍøÉϵÄ?úÂë??Äã?éÒ?ÏÂcreatefilemapping,openfilemapping, mapviewoffile,unmapviewoffileÕâ???öº?Êý????
c# code
public class sharemem{[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, [marshalas(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();//[dllimport("kernel32.dll", setlasterror = true)]//public static extern intptr getstdhandle(enumstd std);protected enum
enumstd{std_input_handle = -10,std_output_handle = -11,std_error_handle = -12}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 sharemem(){}~sharemem(){close();}///
/// ?õÊ?????ÏíÄÚ?æ/// ///
??ÏíÄÚ?æÃû?Æ///
??ÏíÄÚ?æ?óÐ?///
public int init(string strname, long lngsize){if (lngsize <= 0 || lngsize > 0x00800000) lngsize = 0x00800000;m_memsize = lngsize;if (strname.length >
0){//????ÄÚ?æ??ÏíÌå(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; //??????ÏíÌåÊ??Ü}else{if (getlasterror() == error_already_exists)//ÒÑ?,????{m_balreadyexist = true;}else
//ÐÂ????{m_balreadyexist = false;}}//--------------------//????ÄÚ?æÓ?Éä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; //????ÄÚ?æÓ?ÉäÊ??Ü}else{m_binit = true;if (m_balreadyexist == false){//?õÊ???}}//--------------------}else{return 1; //?ÎÊý?íÎó }return 0; //?????É??}///
/// ?Ø?Õ??ÏíÄÚ?æ/// public void close(){if (m_binit){unmapviewoffile(m_pwdata);closehandle(m_hsharedmemoryfile);}}///
/// ?ÁÊý?Ý/// ///
Êý?Ý///
ÆðÊ?µØÖ?///
?öÊý///
public int read(ref byte[] bytdata, int lngaddr, int lngsize){if (lngaddr + lngsize > m_memsize) return 2; //???öÊý?ÝÇøif
(m_binit){marshal.copy(m_pwdata, bytdata, lngaddr, lngsize);}else{return 1; //??ÏíÄÚ?æÎ??õÊ???}return 0; //?Á?É??}///
/// Ð?Êý?Ý/// ///
Êý?Ý///
ÆðÊ?µØÖ?///
?öÊý///
public int write(byte[] bytdata, int lngaddr, int lngsize){if (lngaddr + lngsize > m_memsize) return 2; //???öÊý?ÝÇøif (m_binit){marshal.copy(bytdata, lngaddr, m_pwdata, lngsize);}else{return 1; //??ÏíÄÚ?æÎ??õÊ???}return 0; //Ð??É??}}
?úÂëÎÒÉ?È?ÁË,???ýÎÒ?ÉÒÔ??Ò?ÏÂÎÒµÄË?Â?,??ÖªµÀ?ÔÄãÓÐÓÃÂð,ÄãµÄÊÇ?æÍ??É,ÎÒÒÔÇ?×öµÄÄÇ?öÒ?ÊÇ?æÍ?.Ê×ÏÈÁ??ö??Ìå?Ö?ðÊÇform1ºÍform2,ÔÚfrom1ÖÐ??Òåprivate form2 form2=new form2();.ÔÚform2ÖÐ??ÒåÒ??ö????ºÍÒ??ölist??ºÏ,??ºÏ???æÒª?æÍ?µÄ×ø?ê,????ÊÇ?ÓÊÕform1???ýÀ?µÄ?ÎÊý,È?ºóÔÚform1ÖÐ?æÍ?,ÔÚ?æÍ?µÄ?ý?ÌÖÐ?Ñ?æÍ?µÄ×ø?ê??µ??Õ?Åform2??ÒåµÄÄÇ?ö????,È?ºó?ÑÕâ?ö×ø?êµã???æÔÚÒ??ölist??ºÏÖÐ,È?ºóÂíÉÏÖØ?æthis.invalidate();,ÖØ?æµÄ????ÀïÑ,??listÕâ?ö??ºÏ?øÐÐ?æÍ?.