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. Detecting a file close event

Detecting a file close event

Scheduled Pinned Locked Moved C / C++ / MFC
jsonhelpquestion
7 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.
  • H Offline
    H Offline
    Harold Bamford
    wrote on last edited by
    #1

    Greetings, I have a program in which I spawn a process (AcroRd32.exe with the proper parameters) and I want to wait until it has finished writing a file and then close the process. (I am "printing" to a file.) I know that I can poll the file's size and wait for it to stop growing for some arbitrary time, but this is unsatisfactory for obvious reasons. It seems like the OS knows when a file gets opened and when the file handle is closed. Is there an easy way, given the full pathname to the file, to detect when a file handle gets closed? I vaguely recall that there was some Win32 API that might help do this but I cannot find it. I really have searched for it, but I guess I'm not using the right keywords! Thanks for any pointers. -- Harold

    N 1 Reply Last reply
    0
    • H Harold Bamford

      Greetings, I have a program in which I spawn a process (AcroRd32.exe with the proper parameters) and I want to wait until it has finished writing a file and then close the process. (I am "printing" to a file.) I know that I can poll the file's size and wait for it to stop growing for some arbitrary time, but this is unsatisfactory for obvious reasons. It seems like the OS knows when a file gets opened and when the file handle is closed. Is there an easy way, given the full pathname to the file, to detect when a file handle gets closed? I vaguely recall that there was some Win32 API that might help do this but I cannot find it. I really have searched for it, but I guess I'm not using the right keywords! Thanks for any pointers. -- Harold

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      Why not just wait for the spawned process to finish? You can do this with CreateProcess() and WaitForSingleObject(). If you want specific code let know. Neville Franks, Author of ED for Windows. www.getsoft.com

      H 1 Reply Last reply
      0
      • N Neville Franks

        Why not just wait for the spawned process to finish? You can do this with CreateProcess() and WaitForSingleObject(). If you want specific code let know. Neville Franks, Author of ED for Windows. www.getsoft.com

        H Offline
        H Offline
        Harold Bamford
        wrote on last edited by
        #3

        Oh, would that it were so easy! :laugh: I am spawning something like:

        AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
        

        to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the /n doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner.

        J N 2 Replies Last reply
        0
        • H Harold Bamford

          Oh, would that it were so easy! :laugh: I am spawning something like:

          AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
          

          to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the /n doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner.

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          Possibly there's a neater way using some functions from the Shell library, but the following hack should work: Keep trying to open filexyz.ps in non-create, write mode periodically until you succeed. Then you know AcroRd32.exe is done. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

          H 1 Reply Last reply
          0
          • J Joaquin M Lopez Munoz

            Possibly there's a neater way using some functions from the Shell library, but the following hack should work: Keep trying to open filexyz.ps in non-create, write mode periodically until you succeed. Then you know AcroRd32.exe is done. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

            H Offline
            H Offline
            Harold Bamford
            wrote on last edited by
            #5

            Sounds promising, assuming that AcroRd32.exe normally locks its output file (or the OS does this automatically). I'll give that a try and report back (tomorrow, probably). Thanks!

            1 Reply Last reply
            0
            • H Harold Bamford

              Oh, would that it were so easy! :laugh: I am spawning something like:

              AcroRd32.exe /n /t "PSPrinter" "HP LaserJet 5000 Series PS" "filexyz.pdf" "filexyz.ps"
              

              to print a PDF file to a Postscript file. Unfortunately, AcroRd32.exe doesn't exit after doing this. It just sits around, using up resources. And removing the /n doesn't help. Sigh... But thanks for the suggestion. Ordinarily that would be the clear winner.

              N Offline
              N Offline
              Neville Franks
              wrote on last edited by
              #6

              Harold Bamford wrote: Oh, would that it were so easy! Yep.;) I'd like to think AcroRd32.exe locked the file it was printing to. If it does then you could try opening it for write access from time to time and when the open succeeded you'd be away. There is a Win API FindFirstChangeNotification() which would be worth looking at, but in this case I don't think it will help. See: http://www.codeproject.com/file/filewatch.asp[^]etc. Also have a look at www.sysinternals.com (seems to be down at present). They have a program called called something like FileWatch which should provide a solution. Neville Franks, Author of ED for Windows. www.getsoft.com

              H 1 Reply Last reply
              0
              • N Neville Franks

                Harold Bamford wrote: Oh, would that it were so easy! Yep.;) I'd like to think AcroRd32.exe locked the file it was printing to. If it does then you could try opening it for write access from time to time and when the open succeeded you'd be away. There is a Win API FindFirstChangeNotification() which would be worth looking at, but in this case I don't think it will help. See: http://www.codeproject.com/file/filewatch.asp[^]etc. Also have a look at www.sysinternals.com (seems to be down at present). They have a program called called something like FileWatch which should provide a solution. Neville Franks, Author of ED for Windows. www.getsoft.com

                H Offline
                H Offline
                Harold Bamford
                wrote on last edited by
                #7

                Well, I tried it and the method suggested by you and Joaquín M López Muñoz worked perfectly. On the off-chance that AcroRd32.exe creates an empty file before writing to it, I added a check for a non-zero length file, but basically the technique works. Thanks to both of you!

                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