How to block a file on Windows?

How to lock a file in Windows so that this file can only be opened / read / written by one process?

I found out that a file can be locked by CreateFile

pointing 0

to dwShareMode

. It works, but only the processed descriptor can be used to operate on the file. But I want to be able to lock the file for other processes and at the same time create multiple handles in my process.

Please help me to solve this problem or give some advice ...

+2


a source to share


2 answers


Why would you need to create the same file twice in the same process? You can use one descriptor across all I / O functions in your process without reopening the file. If you need to pass a handle to another process, you can use the function DuplicateHandle

.



+2


a source


I don't think you can do this. The closest you can use CreateFile

to open / lock a file the first time and then use DuplicateHandle

to create multiple handles from what you already have



+1


a source







All Articles