2012-06-18 3 views
1

내 소프트웨어가 메모리 매핑을 통해 기존 타사 소프트웨어와 통신하도록하려고합니다. 필자는 다른 소프트웨어에 의해 생성 된 메모리 맵핑 된 파일에 구조체를 작성해야한다고 들었다. 필자는 파일을 열어서 제대로 만들었지 만 파일을 매핑하려고하면 오류 8 (ERROR_NOT_ENOUGH_MEMORY)이 표시됩니다.메모리 매핑을 사용하여 구조체 공유, C++, ERROR_NOT_ENOUGH_MEMORY

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

그러나 I :

#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이 공유 메모리를 생성 한 프로그램에 의해 성장이 설정되지 않은 경우 일이 내가 포인터의 크기를 설정하기 위해이 기능을 사용하여 시도해야 할 수 있습니다 말했다 여전히 오류 8, 어떤 도움을 주시면 감사하겠습니다 감사합니다.

답변

1

루프 내부의 MapViewOfFile은별로 의미가 없다고 생각합니다. 그게 오타 일 수 있니? 파일이 공백 일 가능성이 있으므로 파일 크기가 비어있어 MapViewOfFile에 매핑 된 메모리 크기를 전달해야합니다.

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); 
관련 문제