Implementing inter-process memory sharing with file mapping (Mapping)
Last Update:2016-05-14
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>