如何在文件上使用不同范围重复调用 MapViewOfFile 以写入更大的缓冲区。

发布于 2024-12-14 14:56:18 字数 1965 浏览 1 评论 0原文

我正在使用共享内存函数,将数据写入文件。 问题: 当我想使用下面的函数写入缓冲区 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-12-21 14:56:18

如以下链接所述,

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文