user1463923
user1463923

Reputation: 21

Sharing a struct with Memory Mapping, c++, ERROR_NOT_ENOUGH_MEMORY

I'm trying to get my software to communicate through Memory Mapping to a pre existing piece of third party software. Ive been told to write a struct to a memory mapped file that gets created by the other piece of software. Ive managed to open the file, and its definitely being created properly, but I'm getting an error 8 (ERROR_NOT_ENOUGH_MEMORY) when I try and map the file.

#include "stdafx.h"
#include <Windows.h>

struct MMFSTRUCT
{
    unsigned char flags;
    DWORD   packetTime;
    float   telemetryMatrix[16];
    float   velocity[3];
    float   accel[3];
};

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD Time = 0;
    HANDLE hMapFile;
    void* pBuf;
    TCHAR szName[]=TEXT("$FILE$");

    hMapFile = OpenFileMapping(
                   FILE_MAP_ALL_ACCESS,   // read/write access
                   FALSE,                 // do not inherit the name
                   szName);               // name of mapping object
    if (hMapFile == NULL)
    {
        _tprintf(TEXT("Could not open file mapping object (%d).\n"),
               GetLastError());
        return 1;
    }
    while(true)
    {
        pBuf = MapViewOfFile(hMapFile,   // handle to map object
                            FILE_MAP_WRITE, // read/write permission
                            0,
                            0,
                            0);
        if (pBuf == NULL)
        {
            _tprintf(TEXT("Could not map view of file (%d).\n"),
                   GetLastError());
            CloseHandle(hMapFile);
            return 1;
        }
        MMFSTRUCT test_data;
        // define variables here
        CopyMemory(pBuf, &test_data,  sizeof(MMFSTRUCT));
    }
    // etc
    return 0;
}

MSDN said this may happen if the shared memory is not set to grow by the program that created it and that I should try use these functions to set the pointer size:

SetFilePointer(hMapFile, sizeof(MMFSTRUCT) , NULL, FILE_CURRENT);
SetEndOfFile(hMapFile);

But I'm still getting error 8, any help would be appreciated, thanks.

Upvotes: 1

Views: 1378

Answers (1)

CapelliC
CapelliC

Reputation: 60034

I think MapViewOfFile inside loop make little sense. That could be a typo? That apart, you should pass the size of the memory mapped to MapViewOfFile, because your file is presumably empty:

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

pBuf = MapViewOfFile(hMapFile,   // handle to map object
                        FILE_MAP_WRITE, // read/write permission
                        0,
                        0,
                        sizeof(MMFSTRUCT));
if (pBuf == NULL)
{
        _tprintf(TEXT("Could not map view of file (%d).\n"),
               GetLastError());
        CloseHandle(hMapFile);
        return 1;
 }
 MMFSTRUCT test_data;
 // define variables here
 CopyMemory(pBuf, &test_data,  sizeof(MMFSTRUCT));

 UnmapViewOfFile(pBuf);
 CloseHandle(hMapFile);

Upvotes: 1

Related Questions