I am working on file locking for first time, and couldn't find relevant posts for solution in Google.
I am locking a file using this code, to lock file.
ifile = CreateFileW(FileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
In next line I am trying to open the same file using
errno_t ErrorNumber = _wfopen_s(FileHandle, FileName, "rb");
The purpose is to lock the file to prevent any other process from writing to it, while this function is reading its contents. I am getting EACCESS : 13 error code when opening the file with "rb".
Any ideas why and how to enable reading the file after locking it ?
Thanks Sujatha
To create a "lock file" on Win32 that won't allow other processes to open it:
ifile = CreateFileW(FileName, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_FLAG_DELETE_ON_CLOSE, NULL);
CREATE_NEW
and 0
for the sharing mode ensures that the file will only be opened by your process and FILE_FLAG_DELETE_ON_CLOSE
ensures that it'll be auto-deleted when you close the file or your process (heaven forbid) crashes.
This is a somewhat clumsy way of achieving cross-process locks on Win32 though. Shared Mutex's were invented to solve this problem.
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.