본문 바로가기

Windows/MFC

Creating named shared Memory

code sample by MSDN

process 간 통신을 하기 위해선 우린 -_ - 삽질이 필요하다.. 근데 이렇게도 해지만.. 그냥 파일 열어서도 가능할 것 같다.. 구조가 비슷해보이는 것이....

[code]First process[/code]
#include <windows.h>
#include <stdio.h>
#include <conio.h>

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

void main()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    hMapFile = CreateFileMapping(
                INVALID_HANDLE_VALUE,    // use paging file
                NULL,                    // default security
                PAGE_READWRITE,         // read/write access
                0,                     // max. object size
                BUF_SIZE,                // buffer size
                szName);                 // name of mapping object

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

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

    CopyMemory((PVOID)pBuf, szMsg, strlen(szMsg));
    getch();
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
}

[code]Second Process[/code]
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <windows.h>

#define BUF_SIZE 256
TCHAR szName[]=TEXT("MyFileMappingObject");

void main()
{
    HANDLE hMapFile;
    LPCTSTR pBuf;

    hMapFile = OpenFileMapping(
                FILE_MAP_ALL_ACCESS, // read/write access
                FALSE,                 // do not inherit the name
                szName);             // name of mapping object

    if (hMapFile == NULL)
    {
        printf("Could not open file mapping object (%d).\n", GetLastError());
        return;
    }

    pBuf = MapViewOfFile(hMapFile,    // handle to mapping object
                        FILE_MAP_ALL_ACCESS, // read/write permission
                        0,
                        0,
                        BUF_SIZE);

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

    MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
    UnmapViewOfFile(pBuf);
    CloseHandle(hMapFile);
}

'Windows > MFC' 카테고리의 다른 글

global memory CreateFileMapping MapViewOfFile  (0) 2013.10.02
Displaying the Installed Devices  (0) 2013.10.02
expand dialog  (0) 2013.10.02
status bar icon  (0) 2013.10.02
microtime  (0) 2013.10.02