简体   繁体   中英

ReadFile function on Windows XP fails with error code 2

I have a part of the project that is intended to re-read some portion of a file in a loop (prepended with the pointer movement to the beginning of the file). By start, the code opens the file and writes there the 'minimal data' correctly, but the further read (on the same file handle(!)) fails with error code 2 ('File not found').

Here the part of code related to the process:

virtual-mem-buffer.h:

/**
 * Allocates and manages page-aligned virtual memory of the given amount
 */
class VirtualMemBuffer {
public:
    explicit VirtualMemBuffer(size_t size) { /* skipped */ };
/* skipped */
protected:
    void * data;
public:
    const void * buff() const { return this->data; };
};

header-file.h:

static const size_t cFileSize = 4096;

typedef std::map<std::wstring, HANDLE> handlers_conrainer_type;
typedef std::pair<std::wstring, bool> item_type;

class Config {
public:
    typedef std::vector<item_type > container_type;

    container_type files;
    /* skipped */
};

code-file.cpp (inside some function):

VirtualMemBuffer buffer(cFileSize);

Config config(...);
config->files.push_back(item_type(L"C:\\lock-file.lock", true));

/* skipped */

for (Config::container_type::const_iterator it = config->files.begin(); 
     it != config->files.end(); 
     ++it)
{
    HANDLE hFile = CreateFile(
        (LPCWSTR)(it->first.c_str()),
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    if (hFile == INVALID_HANDLE_VALUE) {
        DWORD error = GetLastError();
        // Could not open the file, ignore it
        continue;
    } else {
        DWORD bytes_written = 0;
        BOOL write_ok = WriteFile(
                                    hFile, 
                                    buffer.buff(), 
                                    cFileSize, 
                                    &bytes_written, 
                                    NULL);

        if (!write_ok || (bytes_written != (DWORD)(cFileSize))) {
            // Could not initialize the file, skip the file
            CloseHandle(hFile);
            continue;
        };

        handlers_container.insert(
                std::pair<std::wstring, HANDLE>(it->first, hFile)
                );
    };
};

/* skipped */

for (handlers_conrainer_type::const_iterator it = handlers_container.begin(); 
     it != handlers_container.end(); 
     ++it)
{
    DWORD bytes_read = 0;
    LARGE_INTEGER li;
        li.HighPart = 0;
        li.LowPart = 0;
    BOOL move_ok = SetFilePointerEx(it->second, li, NULL, FILE_BEGIN);
    BOOL read_ok = ReadFile(
                                it->second, 
                                buffer.buff(), 
                                cFileSize, 
                                &bytes_read, 
                                NULL);

    if (!read_ok || (bytes_read != cFileSize)) {
        DWORD error = GetLastError();        // error == 2 :-(
        /* skipped */
    };
};

As you can see, SetFilePointerEx() and ReadFile() both operate on the same file handle. The first one (and CreateFile(), WriteFile() ) never failed, but ReadFile() never succeded.

Did anybody observe such a behavior or at least have any clue on this? What is wrong and how can it be fixed (or avoided)?

The code compiled on Windows XP SP3 using MS Visual C++ 2008 Express Edition

Thank you for your time and advices!

I have found the root of the problem - the error code for the ReadFile() fail was tested in MS VC++ debugger where (and when) the variable was actually out of the (block) scope and thus flooded with garbage. The value 2 was just a compiler preference for such condition.

I have just noticed that and added some further error checking and code around that, and found that the real error code is 998 ('Invalid access to memory location') which itself came from the VirtualMemBuffer class where VirtualAlloc() was called with PAGE_READONLY flag :-(.

So, it was my fault, sorry.

Thanks to everyone who spent time trying to help me with this problem.

尝试FlushFileBuffers(handle)将写入提交到磁盘,然后再尝试读取它们

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM