为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

内存共享

2018-02-04 4页 doc 18KB 5阅读

用户头像

is_358746

暂无简介

举报
内存共享内存共享 ?ø?Ì?ä??ÏíÄÚ?æ, ÍøÉϵÄ?úÂë??Äã?éÒ?ÏÂcreatefilemapping,openfilemapping, mapviewoffile,unmapviewoffileÕâ???öº?Êý???? c# code public class sharemem{[dllimport("user32.dll", charset = charset.auto)]public static extern intptr sendmessage(intptr hwnd, int msg, in...
内存共享
内存共享 ?ø?Ì?ä??ÏíÄÚ?æ, ÍøÉϵÄ?úÂë??Äã?éÒ?ÏÂ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Õâ?ö??ºÏ?øÐÐ?æÍ?.
/
本文档为【内存共享】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索