0

共有メモリに int の配列と bool の配列を作成しようとしています。これまでのところ、エラーなしで実行され、「明らかに」メモリが作成される次のコードがありますが、LPCTSTR を使用して配列のようなデータにアクセスできるかどうかはわかりません。MSDNが非常に欠けていて苦痛だと思うので、誰かがこれを行う最善の方法を説明してもらえますか.

void createSharedMemory()
{
    const char slotsName[]="Slots";
    const char flagsName[]="Flags";

    const LONG BufferSize = sizeof(int);
    const LONG Buffers = 10;
    const LONG FlagSize = sizeof(bool); 

    HANDLE hSlots = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, BufferSize * Buffers, SLOTSNAME);
    assert(hSlots != NULL);

    HANDLE hFlags = CreateFileMapping((HANDLE)0xFFFFFFFF, NULL, PAGE_READWRITE, 0, FlagSize * Buffers, flagsName);
    assert(hSlots != NULL);

    std::cout << "Created shared memory!" << std::endl;
}


int main(int argc, char* argv[])
{
    createSharedMemory();

    HANDLE hSlots;
    LPCTSTR pSlots;

    hSlots = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, SLOTSNAME);

    if(hSlots == NULL)
    {
        std::cout << "Could not open slots file mapping object:" << GetLastError() << std::endl;
        getchar();
        return 0;
    }

    pSlots = (LPTSTR) MapViewOfFile(hSlots, FILE_MAP_ALL_ACCESS, 0, 0, 10 * sizeof(int));

    if(pSlots == NULL)
    {
        std::cout << "Could not map view of slots file:" << GetLastError() << std::endl;
        CloseHandle(hSlots);
        getchar();
        return 0;
    }

    std::cout << "Mapped slots correctly!" << std::endl;

    HANDLE hFlags;
    LPCTSTR pFlags;

    hFlags = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, FLAGSNAME);

    if(hFlags == NULL)
    {
        std::cout << "Could not open flags file mapping object:" << GetLastError() << std::endl;
        getchar();
        return 0;
    }

    pFlags = (LPTSTR) MapViewOfFile(hFlags, FILE_MAP_ALL_ACCESS, 0, 0, 10 * sizeof(bool));

    if(pFlags == NULL)
    {
        std::cout << "Could not map view of flags file:" << GetLastError() << std::endl;
        CloseHandle(hFlags);
        getchar();
        return 0;
    }

    std::cout << "Mapped flags correctly!" << std::endl;

    //Access the data here

    getchar();

    UnmapViewOfFile(pSlots);
    CloseHandle(hSlots);

    UnmapViewOfFile(pFlags);
    CloseHandle(hFlags);

    return 0;
}
4

1 に答える 1