1
我試圖讓我的軟件通過內存映射與現有的第三方軟件進行通信。我被告知要將結構寫入由另一部分軟件創建的內存映射文件。我設法打開該文件,並且它正確地被創建,但是當我嘗試映射文件時,出現錯誤8(ERROR_NOT_ENOUGH_MEMORY)。與內存映射共享結構,C++,ERROR_NOT_ENOUGH_MEMORY
#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說,如果共享內存不設置由創建它的程序將增長,這可能發生,我應該嘗試使用這些函數來設置指針大小:
SetFilePointer(hMapFile, sizeof(MMFSTRUCT) , NULL, FILE_CURRENT);
SetEndOfFile(hMapFile);
但我仍然收到錯誤8,任何幫助將不勝感激,謝謝。