测试代码_共享内存

1、VC6

#include <stdio.h>
#include <windows.h>

void main()
{
    char* pc = "AAA";
    HANDLE hMem = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4, pc);
    if (hMem == NULL)
    {
        printf("CreateFileMapping return NULL");
        return;
    }

    void* pMem = MapViewOfFile(hMem, FILE_MAP_WRITE, 0, 0, 0);
    if (pMem == NULL)
    {
        printf("MapViewOfFile return NULL");
        return;
    }

    UnmapViewOfFile(pMem);
    CloseHandle(hMem);

// ***
    hMem = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4, pc);
    if (hMem == NULL)
    {
        printf("CreateFileMapping return NULL");
        return;
    }

    pMem = MapViewOfFile(hMem, FILE_MAP_WRITE, 0, 0, 0);
    if (pMem == NULL)
    {
        printf("MapViewOfFile return NULL");
        return;
    }

    UnmapViewOfFile(pMem);
    CloseHandle(hMem);

// ***
    hMem = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 4, pc);
    if (hMem == NULL)
    {
        printf("CreateFileMapping return NULL");
        return;
    }

    pMem = MapViewOfFile(hMem, FILE_MAP_WRITE, 0, 0, 0);
    if (pMem == NULL)
    {
        printf("MapViewOfFile return NULL");
        return;
    }

    UnmapViewOfFile(pMem);
    CloseHandle(hMem);
}

 

2、

3、

4、

5、

 

posted @ 2018-01-02 08:52  DrMess  阅读(222)  评论(0编辑  收藏  举报