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. Force flush of unwritten data to USB stick?

Force flush of unwritten data to USB stick?

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestion
6 Posts 3 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.
  • D Offline
    D Offline
    Dominik Reichl
    wrote on last edited by
    #1

    Hello! When writing to an USB stick, Windows caches all writes. We are therefore required to safely eject the USB stick in Windows before removing it. Unfortunately it frequently happens that users remove USB sticks without ejecting it first. I would now like to force Windows to flush a file immediately when it's closed programmatically, i.e. I'm looking for an API function that allows such flushing. I am not looking for a way to completely disable the caching. The file is written using CreateFile/WriteFile. Is there such an API or way to achieve flushing? Thanks and best regards Dominik


    Too many passwords to remember? Try KeePass Password Safe!

    D J 2 Replies Last reply
    0
    • D Dominik Reichl

      Hello! When writing to an USB stick, Windows caches all writes. We are therefore required to safely eject the USB stick in Windows before removing it. Unfortunately it frequently happens that users remove USB sticks without ejecting it first. I would now like to force Windows to flush a file immediately when it's closed programmatically, i.e. I'm looking for an API function that allows such flushing. I am not looking for a way to completely disable the caching. The file is written using CreateFile/WriteFile. Is there such an API or way to achieve flushing? Thanks and best regards Dominik


      Too many passwords to remember? Try KeePass Password Safe!

      D Offline
      D Offline
      dontknowitall
      wrote on last edited by
      #2

      I've done that before. IIRC, you have to delve into DeviceIoControl().

      D 1 Reply Last reply
      0
      • D dontknowitall

        I've done that before. IIRC, you have to delve into DeviceIoControl().

        D Offline
        D Offline
        Dominik Reichl
        wrote on last edited by
        #3

        Do you remember the control code you used? I've only found IOCTL_STORAGE_EJECT_MEDIA and such things, but no code to only flush write buffers. Thanks for your reply, best regards Dominik


        Too many passwords to remember? Try KeePass Password Safe!

        D 1 Reply Last reply
        0
        • D Dominik Reichl

          Do you remember the control code you used? I've only found IOCTL_STORAGE_EJECT_MEDIA and such things, but no code to only flush write buffers. Thanks for your reply, best regards Dominik


          Too many passwords to remember? Try KeePass Password Safe!

          D Offline
          D Offline
          dontknowitall
          wrote on last edited by
          #4

          Look at "FSCTL_LOCK_VOLUME". MSDN Library has this to say about it: "The system flushes all cached data to the volume before locking it. For example, any data held in a lazy-write cache is written to the volume."

          D 1 Reply Last reply
          0
          • D dontknowitall

            Look at "FSCTL_LOCK_VOLUME". MSDN Library has this to say about it: "The system flushes all cached data to the volume before locking it. For example, any data held in a lazy-write cache is written to the volume."

            D Offline
            D Offline
            Dominik Reichl
            wrote on last edited by
            #5

            I'll try that, thanks a lot!!


            Too many passwords to remember? Try KeePass Password Safe!

            1 Reply Last reply
            0
            • D Dominik Reichl

              Hello! When writing to an USB stick, Windows caches all writes. We are therefore required to safely eject the USB stick in Windows before removing it. Unfortunately it frequently happens that users remove USB sticks without ejecting it first. I would now like to force Windows to flush a file immediately when it's closed programmatically, i.e. I'm looking for an API function that allows such flushing. I am not looking for a way to completely disable the caching. The file is written using CreateFile/WriteFile. Is there such an API or way to achieve flushing? Thanks and best regards Dominik


              Too many passwords to remember? Try KeePass Password Safe!

              J Offline
              J Offline
              JudyL_MD
              wrote on last edited by
              #6

              FlushFileBuffers doesn't work for a USB file? Judy

              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