Readfile()偶尔会返回998 / ERROR_NOACCESS

时间:2015-11-03 00:08:33

标签: windows winapi buffer readfile

我正在尝试在Windows下实现快速IO,并努力实现Overlapped IO。在我的研究中,Unbuffered IO需要页面对齐的缓冲区。我试图在下面的代码中实现这一点。但是,我偶尔会有Readfiles最后一次错误报告无法访问(错误998,ERROR_NOACCESS) - 在完成读取之前,以及几次读取页面对齐缓冲区之后。有时16.有时4等等。

我不能为我的生活弄清楚为什么我偶尔会抛出一个错误。任何见解都会有所帮助。

ci::BufferRef CinderSequenceRendererApp::CreateFileLoadWinNoBufferSequential(fs::path path) {
HANDLE file = CreateFile(path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING | FILE_FLAG_SEQUENTIAL_SCAN, 0);

if (file == INVALID_HANDLE_VALUE)
{
    console() << "Could not open file for reading" << std::endl;
}

ci::BufferRef latestAvailableBufferRef = nullptr;

LARGE_INTEGER nLargeInteger = { 0 };

GetFileSizeEx(file, &nLargeInteger);

// how many reads do we need to fill our buffer with a buffer size of x and a read size of y
// Our buffer needs to hold 'n' sector sizes that wil fit the size of the file

SYSTEM_INFO si;
GetSystemInfo(&si);
long readAmount = si.dwPageSize;

int numReads = 0;
ULONG bufferSize = 0;

// calculate sector aligned buffer size that holds our file size
while (bufferSize < nLargeInteger.QuadPart)
{
    numReads++;
    bufferSize = (numReads) * readAmount;
}

// need one page extra for null if we need it
latestAvailableBufferRef = ci::Buffer::create(bufferSize + readAmount);

if (latestAvailableBufferRef != nullptr)
{
    DWORD outputBytes = 1;

    // output bytes = 0 when OEF
    void* address = latestAvailableBufferRef->getData();
    DWORD bytesRead = 0;
    while (outputBytes != 0)
    {
        bool result = ReadFile(file, address, readAmount, &outputBytes, 0);
        if (!result )//&& (outputBytes == 0))
        {
            getLastReadError();
        }

        address = (void*)((long)address + readAmount);
        bytesRead += outputBytes;

    }
}

CloseHandle(file);  

// resize our buffer to expected file size?
latestAvailableBufferRef->resize(nLargeInteger.QuadPart);

return latestAvailableBufferRef;
}

1 个答案:

答案 0 :(得分:0)

施展到很久 - 我正在截断我的指针地址。咄。感谢@ jonathan-potter