Implementing inter-process memory sharing with file mapping (Mapping)

Source: Internet
Author: User
<span id="Label3"></p><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">HANDLE</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">hmapfile</span><span style=""color:" red"="">=</span><span style=""color:" black"="">NULL</span><span style=""color:" red"="">;</span> </span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">Lpbyte</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">lpmapaddress</span><span style=""color:" red"="">=</span><span style=""color:" black"="">NULL</span><span style=""color:" red"="">;</span> </span></span><strong><strong><span style=""font-size:" 14px"=""><span style=""color:" #000000"="">1. Create a named file mapping object</span></span></strong></strong><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">Hmapfile</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">=</span> <span style=""color:" black"="">createfilemapping</span><span style=""color:" red"="">(</span><span style=""color:" black"="">invalid_handle_value</span><span style=""color:" red"="">,</span><span style=""color:" green"="">//</span> </span></span><span style=""font-size:" 9pt; color: green"><span style=""font-size:" 9pt;="" color:="" green"="">Create a memory-mapped file</span></span><span style=""font-size:" 9pt; color: green"><span style=""font-size:" 9pt;="" color:="" green"="">(</span></span><span style=""font-size:" 9pt; color: green"><span style=""font-size:" 9pt;="" color:="" green"="">do not use disk files</span></span><span style=""font-size:" 9pt; color: green"><span style=""font-size:" 9pt;="" color:="" green"="">) </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">NULL</span><span style=""color:" red"="">,</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">page_readwrite</span><span style=""color:" red"="">,</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">0,</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">1024x768,</span><span style=""color:" green"="">//</span> </span></span><span style=""font-size:" 9pt; color: green"><span style=""font-size:" 9pt;="" color:="" green"="">mapped memory area size</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" teal"="">"filemapping_a9279e4a75917e23c2a"</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: blue"><span style=""font-size:" 9pt;="" color:="" blue"="">if</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">(</span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">Hmapfile</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">==</span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">NULL</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">)</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">{</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">TRACE</span><span style=""color:" red"="">(</span><span style=""color:" teal"="">"createfilemapping = = null\n"</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">}</span></span><span style=""color:" #000000"><span style=""color:" #000000"=""> <span style=""font-size:" 9pt"="">2.</span> <span style=""font-size:" 9pt"="">Open a named file mapping object that already exists</span> </span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">Hmapfile</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">=</span> <span style=""color:" black"="">openfilemapping</span><span style=""color:" red"="">(</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">file_map_all_access</span><span style=""color:" red"="">,</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">FALSE</span><span style=""color:" red"="">,</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" teal"="">"filemapping_a9279e4a75917e23c2a"</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: blue"><span style=""font-size:" 9pt;="" color:="" blue"="">if</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">(</span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">Hmapfile</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">==</span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">NULL</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">)</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">{</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">TRACE</span><span style=""color:" red"="">(</span><span style=""color:" teal"="">"openfilemapping = = null\n"</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">}</span></span> <span style=""color:" #000000"><span style=""color:" #000000"=""> <span style=""font-size:" 9pt"="">3.</span> <span style=""font-size:" 9pt"="">get the mapped memory space first address</span> </span></span><span style=""font-size:" 9pt; color: black"><span style=""font-size:" 9pt;="" color:="" black"="">lpmapaddress</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">=</span> <span style=""color:" red"="">(</span><span style=""color:" black"="">lpbyte</span><span style=""color:" red"="">)</span><span style=""color:" black"="">mapviewoffile</span><span style=""color:" red"="">(</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">hmapfile</span><span style=""color:" red"="">,</span><span style=""color:" green"="">//Handle to mapping object.</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">file_map_all_access</span><span style=""color:" red"="">,</span><span style=""color:" green"="">//read/write permission.</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">0,</span><span style=""color:" green"="">//high-order DWORD of Offset.</span> </span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">0,</span><span style=""color:" green"="">//low-order DWORD of Offset.</span> </span></span><span style=""font-size:" 9pt">( <span style=""font-size:" 9pt"=""> <span style=""color:" red"="">1024x768);</span> <span style=""color:" green"="">//number of bytes to map.</span> </span></span><span style=""font-size:" 9pt; color: blue"><span style=""font-size:" 9pt;="" color:="" blue"="">if</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">(</span><span style=""color:" black"="">lpmapaddress</span><span style=""color:" red"="">= =</span><span style=""color:" black"="">NULL</span><span style=""color:" red"="">)</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">{</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">TRACE</span><span style=""color:" red"="">(</span><span style=""color:" teal"="">"mapviewoffile = = null\n"</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">}</span></span><span style=""color:" #000000"><span style=""color:" #000000"=""> <span style=""font-size:" 9pt"="">4.</span> <span style=""font-size:" 9pt"="">use: This memory can now be manipulated using</span> <span style=""font-size:" 9pt"=""></span> <span style=""font-size:" 9pt"="">functions such as</span> memset,memcpy</span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">...;</span></span><span style=""color:" #000000"><span style=""color:" #000000"=""> <span style=""font-size:" 9pt"="">5.</span> <span style=""font-size:" 9pt"="">release after use</span> </span></span><span style=""font-size:" 9pt; color: blue"><span style=""font-size:" 9pt;="" color:="" blue"="">if</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">(</span><span style=""color:" black"="">lpmapaddress</span>!<span style=""color:" red"="">=</span><span style=""color:" black"="">NULL</span><span style=""color:" red"="">)</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">{</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">unmapviewoffile</span><span style=""color:" red"="">(</span><span style=""color:" black"="">lpmapaddress</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">}</span></span><span style=""font-size:" 9pt; color: blue"><span style=""font-size:" 9pt;="" color:="" blue"="">if</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" red"="">(</span><span style=""color:" black"="">hmapfile</span>!<span style=""color:" red"="">=</span><span style=""color:" black"="">NULL</span><span style=""color:" red"="">)</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">{</span></span><span style=""font-size:" 9pt"><span style=""font-size:" 9pt"=""> <span style=""color:" black"="">closehandle</span><span style=""color:" red"="">(</span><span style=""color:" black"="">hmapfile</span><span style=""color:" red"="">);</span> </span></span><span style=""font-size:" 9pt; color: red"><span style=""font-size:" 9pt;="" color:="" red"="">}</span></span><p><p>Implementing inter-process memory sharing with file mapping (Mapping)</p></p></span>

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.