Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Gaining Exclusive Access over a used volume

Gaining Exclusive Access over a used volume

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mmica
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • M mmica

      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

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      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.

      M 1 Reply Last reply
      0
      • A Antti Keskinen

        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.

        M Offline
        M Offline
        mmica
        wrote on last edited by
        #3

        Thanks... I will try that :-) This processing will take place at night so no users should be using the PCs in the office at that time. This is just a precautionary measure.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups