Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

c++ - Crash when calling ReadFile after LockFileEx

I have several processes that try to read and write the same file. I want each of them to lock the file so that only one of them accesses it at a time.

I tried this (edit: this is a complete test code this time):

#include "stdafx.h"
#include "Windows.h"


bool test()
{
        const char* path = "test.txt";

        HANDLE hFile = CreateFileA(path,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        NULL,
                        OPEN_ALWAYS,
                        FILE_ATTRIBUTE_NORMAL,
                        NULL);

        if (hFile == INVALID_HANDLE_VALUE)
        {
                printf("ERROR: Cannot open file %s
", path);
                return false;
        }

        // Lock the file
        {
                OVERLAPPED overlapped = {0};
                BOOL res = LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, ~0, ~0, &overlapped);
                if (!res)
                {
                        printf("ERROR: Cannot lock file %s
", path);
                        return false;
                }
        }

        DWORD fileSize = GetFileSize(hFile, NULL);
        if (fileSize > 0)
        {
                char* content = new char[fileSize+1];

                // Read the file
                BOOL res = ReadFile(hFile, content, fileSize, NULL, NULL);
                if (!res)
                {
                        printf("ERROR: Cannot read file %s
", path);
                }

                delete[] content;
        }


        const char* newContent = "bla";
        int newContentSize = 3;

        // Write the file
        BOOL res = WriteFile(hFile, newContent, newContentSize, NULL, NULL);
        if (!res)
        {
                //int err = GetLastError();
                printf("ERROR: Cannot write to file
");
        }

        // Unlock the file
        {
                OVERLAPPED overlapped = {0};
                UnlockFileEx(hFile, 0, ~0, ~0, &overlapped);
        }

        CloseHandle(hFile);

        return true;
}

int _tmain(int argc, _TCHAR* argv[])
{
        bool res = test();

        return 0;
}

This works fine on my computer, which has Windows 8. But on my colleague's computer, which has Windows 7, it crashes. Specifically, the calls to ReadFile and WriteFile crash, always.

Note that it never enters the code paths with the error printfs. This code triggers no error except for a write at location 0x00000000 in ReadFile (when run on Windows 7).

We tried to also pass the overlapped struct to the ReadFile and WriteFile calls. It prevents the crash but the lock doesn't work anymore, the file is all scrambled (not with this test code, with the real code).

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Looks like your problem is:

lpNumberOfBytesRead [out, optional] argument is null in your call.

This parameter can be NULL only when the lpOverlapped parameter is not NULL.

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365467%28v=vs.85%29.aspx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...