简体   繁体   中英

Locking Entire file using LockFileEx API in Windows

It is possible to do lock particular offset using LockFileEx API in windows using C++, I tried this and i got successful result.

But I tried to lock entire file using LockFileEx , I failed to do. i didn't found any document on website how to do full file locking using LockfileEX.

    ifile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);

    if (ifile == INVALID_HANDLE_VALUE) {
        printf("CreateFile failed (%d)\n", GetLastError());
        return 1;
    }

    OVERLAPPED overlapvar;
    overlapvar.Offset = 0; 
    overlapvar.OffsetHigh = 0;

    success = LockFileEx(ifile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &overlapvar);

I tried with above code in windows but it is not taking lock on entire file. In my windows code i gave overlapvar.Offset = 0 to say start of the file and 5th argument of LockFileEx to 0 to lock till end of the file.

I tried the same method in linux using fcntl like below.

struct  flock   param ;
param.l_type = F_RDLCK ;
param.l_whence = 0 ;
param.l_start = 0 ; //start of the file
param.l_len = 0 ; // 0 means end of the file

fcntl(FileFd, F_SETLKW, &param)

based on the two variable param.l_start and param.l_len we used to decide on which offset to take the lock. by setting these two variable to 0 help us to take lock from file starting to end in Linux .

I expecting same behaviour in Windows using LockFileEx or any other API.

Is it possible to do full file lock in windows using LockFileEx ?

Is there any other API available to do full file lock ?

Is there any other method to do the same in windows?

Thanks.

It seems you unconditionally want to lock the entire contents of the file immediately after opening it. If that's the case, then you should simply pass 0 (no sharing) as the third argument to CreateFile instead of using LockFileEx .

Regarding LockFileEx usage in particular, you are locking a region starting at offset 0 (though the OVERLAPPED argument) and of length 0 (through the preceding two parameters). You can't specify "until end of file" for the length of the region by using zeroes. If you wanted to lock a region that spans the entire file you would simply specify both as MAXDWORD .

The documentation explicitly tells you that the hEvent member of the OVERLAPPED structure must be valid. And OVERLAPPED doc says

Any unused members of this structure should always be initialized to zero before the structure is used in a function call. Otherwise, the function may fail and return ERROR_INVALID_PARAMETER .

Why are you passing uninitialized structures to API functions? Try initializing overlapvar properly:

OVERLAPPED overlapvar = { 0 };

success = LockFileEx(ifile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY,
                            0, MAXDWORD, MAXDWORD, &overlapvar);

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