memory mapped file example

--000000000000360e1d05b0c39194
Content-Type: text/plain; charset="UTF-8"

hi guys i try to read a memory mapped file created with c++ , this is a c++
code that i have
[code]
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>

#define BUF_SIZE 256
TCHAR szName[] = TEXT("MyFileMappingObject");
TCHAR szMsg[] = TEXT("Message from first process.");

int _tmain()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    hMapFile = CreateFileMapping(
        INVALID_HANDLE_VALUE,    // use paging file
        NULL,                    // default security
        PAGE_READWRITE,          // read/write access
        0,                       // maximum object size (high-order DWORD)
        BUF_SIZE,                // maximum object size (low-order DWORD)
        szName);                 // name of mapping object

    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not create file mapping object (%d).\n"),
            GetLastError());
        return 1;
    }
    pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
        FILE_MAP_ALL_ACCESS, // read/write permission
        0,
        0,
        BUF_SIZE);

    if (pBuf == NULL)
    {
        _tprintf(TEXT("Could not map view of file (%d).\n"),
            GetLastError());

        CloseHandle(hMapFile);

        return 1;
    }


    CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));
    _getch();

    UnmapViewOfFile(pBuf);

    CloseHandle(hMapFile);

    return 0;
}
[/code]

how can read a  memory mapped created ?
thankz at all

--000000000000360e1d05b0c39194
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div>hi guys i try to read a memory mapped file created wi=
th c++ , this is a c++ code that i have <br></div><div>[code]</div><div>#in=
clude &lt;windows.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;conio.h=
&gt;<br>#include &lt;tchar.h&gt;<br><br>#define BUF_SIZE 256<br>TCHAR szNam=
e[] =3D TEXT(&quot;MyFileMappingObject&quot;);<br>TCHAR szMsg[] =3D TEXT(&q=
uot;Message from first process.&quot;);<br><br>int _tmain()<br>{<br>=C2=A0 =
=C2=A0 HANDLE hMapFile;<br>=C2=A0 =C2=A0 LPCTSTR pBuf;<br><br>=C2=A0 =C2=A0=
 hMapFile =3D CreateFileMapping(<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 INVALID_HAN=
DLE_VALUE, =C2=A0 =C2=A0// use paging file<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 N=
ULL, =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/=
/ default security<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 PAGE_READWRITE, =C2=A0 =
=C2=A0 =C2=A0 =C2=A0 =C2=A0// read/write access<br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 0, =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 // maximum object size (high-order DWORD)<br>=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 BUF_SIZE, =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0// maximum object size (low-order DWORD)<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =
szName); =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 // name of=
 mapping object<br><br>=C2=A0 =C2=A0 if (hMapFile =3D=3D NULL)<br>=C2=A0 =
=C2=A0 {<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 _tprintf(TEXT(&quot;Could not creat=
e file mapping object (%d).\n&quot;),<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 GetLastError());<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 return 1;<br>=C2=A0=
 =C2=A0 }<br>=C2=A0 =C2=A0 pBuf =3D (LPTSTR)MapViewOfFile(hMapFile, =C2=A0 =
// handle to map object<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 FILE_MAP_ALL_ACCESS,=
 // read/write permission<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 0,<br>=C2=A0 =C2=
=A0 =C2=A0 =C2=A0 0,<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 BUF_SIZE);<br><br>=C2=
=A0 =C2=A0 if (pBuf =3D=3D NULL)<br>=C2=A0 =C2=A0 {<br>=C2=A0 =C2=A0 =C2=A0=
 =C2=A0 _tprintf(TEXT(&quot;Could not map view of file (%d).\n&quot;),<br>=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 GetLastError());<br><br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 CloseHandle(hMapFile);<br><br>=C2=A0 =C2=A0 =C2=A0 =C2=
=A0 return 1;<br>=C2=A0 =C2=A0 }<br><br><br>=C2=A0 =C2=A0 CopyMemory((PVOID=
)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));<br>=C2=A0 =C2=A0 _getch();=
<br><br>=C2=A0 =C2=A0 UnmapViewOfFile(pBuf);<br><br>=C2=A0 =C2=A0 CloseHand=
le(hMapFile);<br><br>=C2=A0 =C2=A0 return 0;<br>}</div><div>[/code]</div><d=
iv><br></div><div>how can read a=C2=A0 memory mapped created ? <br></div><d=
iv>thankz at all</div><div><br></div></div>

--000000000000360e1d05b0c39194--
0
stfn77
10/3/2020 12:41:14 PM
perl.beginners 29388 articles. 4 followers. Follow

1 Replies
219 Views

Similar Articles

[PageSpeed] 49
Get it on Google Play
Get it on Apple App Store

On 2020-10-03 5:41 a.m., stefano cerbioni wrote:
> hi guys i try to read a memory mapped file created with c++ , this is a c++
> code that i have
> [code]

[snip code]


> [/code]
> 
> how can read a  memory mapped created ?
> thankz at all


A memory mapped file should be just like any other file.  Just find out 
what the file name is and determine what format the data is stored in 
that file.

John
0
jwkrahn
10/3/2020 8:06:10 PM
Reply: