Gaining Exclusive Access over a used volume
-
Hi, I've written a small administration tool that does some processing on removable devices. The problem is I need to lock the volume before processing. I have managed to do this but only if noone is using the volume at the time when I am locking. Can I gain exclusive access over a volume and force other processes to not use it for a while? Mark
-
Hi, I've written a small administration tool that does some processing on removable devices. The problem is I need to lock the volume before processing. I have managed to do this but only if noone is using the volume at the time when I am locking. Can I gain exclusive access over a volume and force other processes to not use it for a while? Mark
This is not possible. The FSCTL_LOCK_VOLUME flag requires that there are no open files on the target device before you lock it. If this is a removable device, you can order the disk management system to dismount and remount the device programmatically, which will cause all access to the device to flush and stop. After the remount, you can open and exclusively lock the device. The best alternative is to enter a while-loop in your program, that will periodically attempt to lock the volume, and when it succeeds, it will then start the processing. Using a function to exclusively lock a drive and force all others out instantly is considered very bad behaviour from a software. What if the user is copying e.g. images from a camera device when you force the file copier out from the camera's media and lock it ? This, in the worst case, could cause the copier program to end into an access violation error. Using this when a disk defragmenter or similar is in use might even break the hardware or the disk's index system. You should redesign your application layout so that it can wait for it's turn in order to start the processing work. This is much more favorable than exclusive locking. If you MUST get exclusive access, then first open the device handle, then dismount it. After that, call
GetLogicalDrives
which will attempt to remount the device. Enter a successive loop that tries to open the device and lock it. When done, start your processing. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible. -
This is not possible. The FSCTL_LOCK_VOLUME flag requires that there are no open files on the target device before you lock it. If this is a removable device, you can order the disk management system to dismount and remount the device programmatically, which will cause all access to the device to flush and stop. After the remount, you can open and exclusively lock the device. The best alternative is to enter a while-loop in your program, that will periodically attempt to lock the volume, and when it succeeds, it will then start the processing. Using a function to exclusively lock a drive and force all others out instantly is considered very bad behaviour from a software. What if the user is copying e.g. images from a camera device when you force the file copier out from the camera's media and lock it ? This, in the worst case, could cause the copier program to end into an access violation error. Using this when a disk defragmenter or similar is in use might even break the hardware or the disk's index system. You should redesign your application layout so that it can wait for it's turn in order to start the processing work. This is much more favorable than exclusive locking. If you MUST get exclusive access, then first open the device handle, then dismount it. After that, call
GetLogicalDrives
which will attempt to remount the device. Enter a successive loop that tries to open the device and lock it. When done, start your processing. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.