我正在使用共享内存函数,将数据写入文件。问题:当我想使用以下函数将缓冲区写入 10MB 到文件时,我只能在第一次迭代中写入,第二次迭代我无法将缓冲区下一部分写入内存。如果有任何建议,请帮助我。或以下代码中的任何错误。我只能控制这部分。我无法控制具有 CreateFileMapping 的其他部分。
我需要在“dwFileOffsetLow”或“dwFileOffsetHigh”中更改的任何内容。从第二次迭代开始,“MapViewOfFile”返回 null。在某些地方我得到了输入,因为我需要重复调用 MapViewOfFile 不同的范围。但是怎么打电话,有帮助吗?之前问过这个问题。供参考!
WriteBuffer 函数我定义如下:
BOOL CWriter::Write(char* pMemName,char* pBinary,long lBuffSize)
{
long lUnitSize = MEM_UNIT_SIZE;
if( lBuffSize <= 0 && lUnitSize <=0 )
return FALSE;
//Open named file mapping object.
HANDLE hFileMMF = OpenFileMapping(FILE_MAP_WRITE,FALSE,pMemName);
if(hFileMMF == NULL)
{
DWORD dwErr = GetLastError();
return (FALSE);
}
int nCount = 0;
nCount = (int)(lBuffSize / lUnitSize) + 1;
for(int n =0; n<nCount; ++n)
{
DWORD dwFileOffsetHigh = 0;
DWORD dwFileOffsetLow = lUnitSize*n;
// Map view of a file mapping into the address space of a calling process.
LPVOID pViewMMFFile = MapViewOfFile(hFileMMF,
FILE_MAP_WRITE,
dwFileOffsetHigh,
dwFileOffsetLow,
MEM_UNIT_SIZE);
if( pViewMMFFile == NULL )
return (FALSE);
CMutex mutex (FALSE, _T("MIPSMMMutexWriter"));
CString strTemp;
strTemp.Format("%s",pBinary);
// Lock memory, shared amongst processes
mutex.Lock();
try
{
CopyMemory(pViewMMFFile,pBinary,lUnitSize); // write
}
catch(CException e)
{
DWORD dw = ::GetLastError();
TRACE1("%d",dw);
}
mutex.Unlock(); // Unlock shared memory
//Unmap mapped view of a file from the calling process's address space.
UnmapViewOfFile(pViewMMFFile);
}
return (TRUE);
}
请建议我是否应该做任何更正。谢谢。