如何在文件上使用不同范围重复调用 MapViewOfFile 以写入更大的缓冲区。
我正在使用共享内存函数,将数据写入文件。 问题: 当我想使用下面的函数写入缓冲区 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);
}
如果我应该做任何更正,请建议我。谢谢。
I am using shared memory functions, to write data into file.
Problem:
When I want to write the buffer 10MB, to file using below function, i am able to write only in first iteration, second iteration i could not write buffer next part in to memory. please help me if any suggestion. or any wrong in the below code. i have only control on this part. i dont have control in other part which has CreateFileMapping.
any thing i need to change in "dwFileOffsetLow" or "dwFileOffsetHigh". from second iteration onwards the
"MapViewOfFile" returning null. Some place i got input as I need to repeate call MapViewOfFile different ranges. But how to call, any help? previously asked question on this. For reference!
WriteBuffer function i defined as follows:
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);
}
Pls suggestme if any correction i should do. thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如以下链接所述,
http ://msdn.microsoft.com/en-us/library/windows/desktop/aa366761(v=VS.85).aspx
请检查“分配粒度”,我认为您应该使用此参数来设置“dwFileOffsetLow”或“dwFileOffsetHigh”的值。
as described in the following link,
http://msdn.microsoft.com/en-us/library/windows/desktop/aa366761(v=VS.85).aspx
can you please check "allocation granularity", I think you should use this parameter to set the values for "dwFileOffsetLow" or "dwFileOffsetHigh".