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. Deleting a file used by another process.

Deleting a file used by another process.

Scheduled Pinned Locked Moved C / C++ / MFC
comlinux
22 Posts 6 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.
  • N Naveen

    I have been trying to delete files that is kept opened by another process. For this, I took the handle of the file, Duplicate it and the call the CloseHandle with that handle. It works fine for almost all files. But in some cases, even though I was able to close the handle, I coudn't delete it. I am able to rename it also. For eg the event log files like CstEvent.Evt, AppEvent.Evt etc in the "C:\WINDOWS\system32\config" folder (This files are used by services.exe). If any one knows the reason, please tell me. Thanks

    nave [OpenedFileFinder]

    I Offline
    I Offline
    Iain Clarke Warrior Programmer
    wrote on last edited by
    #2

    A useful thought experiment... (Not my original idea). Imagine if you could do what you want. I'm sure you are a saint who loves puppies, but there are bad people out there too. And if you can do this thing, so can they. If a service (which is more important to system stability than a normal process, and usually lives in a different security context) tries to write to a file that suddenly stops existing, through no fault of its own, it could crash horribly and maybe take the system with it. If you mess up files running under your own security credentials, then that's your own fault - and should not have a bad effect on the overall system. Frankly, I'm surprised you can even rename those files while they're still opened. I'm really struggling to think of a legitimate reason to do what you're asking, and many bad ones. Iain,

    Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

    N 1 Reply Last reply
    0
    • I Iain Clarke Warrior Programmer

      A useful thought experiment... (Not my original idea). Imagine if you could do what you want. I'm sure you are a saint who loves puppies, but there are bad people out there too. And if you can do this thing, so can they. If a service (which is more important to system stability than a normal process, and usually lives in a different security context) tries to write to a file that suddenly stops existing, through no fault of its own, it could crash horribly and maybe take the system with it. If you mess up files running under your own security credentials, then that's your own fault - and should not have a bad effect on the overall system. Frankly, I'm surprised you can even rename those files while they're still opened. I'm really struggling to think of a legitimate reason to do what you're asking, and many bad ones. Iain,

      Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #3

      Iain Clarke wrote:

      I'm really struggling to think of a legitimate reason to do what you're asking, and many bad ones.

      :). Not for anything bad. Actaully I want to extent the feature in one of my utility OpenFileFinder[^]. In the initial stage I didnt give the close handle option purposefully cause I was also afraid that it may lead the application to an unstable state. But later I found that option is also a good one, when we cannot terminate the process( Like terminating service.exe will make the entire system unstable ). So I added that option. I tried closing the handle using the process explorer also. But that also gave me same result. But with the UnLocker[^] utiltiy, I can delete the file. I coudnt understand what he have done in it :(.

      Iain Clarke wrote:

      Frankly, I'm surprised you can even rename those files while they're still opened.

      NO no. I mean, I can rename the file after calling the CloseHandle with handle.

      nave [OpenedFileFinder]

      L 1 Reply Last reply
      0
      • N Naveen

        I have been trying to delete files that is kept opened by another process. For this, I took the handle of the file, Duplicate it and the call the CloseHandle with that handle. It works fine for almost all files. But in some cases, even though I was able to close the handle, I coudn't delete it. I am able to rename it also. For eg the event log files like CstEvent.Evt, AppEvent.Evt etc in the "C:\WINDOWS\system32\config" folder (This files are used by services.exe). If any one knows the reason, please tell me. Thanks

        nave [OpenedFileFinder]

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        Is it within your design to delete the file at the next system startup?

        "Love people and use things, not love things and use people." - Unknown

        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

        N 1 Reply Last reply
        0
        • N Naveen

          Iain Clarke wrote:

          I'm really struggling to think of a legitimate reason to do what you're asking, and many bad ones.

          :). Not for anything bad. Actaully I want to extent the feature in one of my utility OpenFileFinder[^]. In the initial stage I didnt give the close handle option purposefully cause I was also afraid that it may lead the application to an unstable state. But later I found that option is also a good one, when we cannot terminate the process( Like terminating service.exe will make the entire system unstable ). So I added that option. I tried closing the handle using the process explorer also. But that also gave me same result. But with the UnLocker[^] utiltiy, I can delete the file. I coudnt understand what he have done in it :(.

          Iain Clarke wrote:

          Frankly, I'm surprised you can even rename those files while they're still opened.

          NO no. I mean, I can rename the file after calling the CloseHandle with handle.

          nave [OpenedFileFinder]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #5

          Unlocker utilizes a driver "\\DosDevices\\UnlockerDriver5" to unlink the handle from the kernels internel handle linked list. The driver file image is named 'UnlockerDriver5.sys' and resides in the installation folder. For most locked files it is using UnlockerHook.dll which is closing the file handle from inside the remote process. For those files locked by the SYSTEM it uses the driver. I highly recommend that you do not duplicate this 'unlocking' behavior. Best Wishes, -David Delaune

          N 1 Reply Last reply
          0
          • D David Crow

            Is it within your design to delete the file at the next system startup?

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            N Offline
            N Offline
            Naveen
            wrote on last edited by
            #6

            no :(

            nave [OpenedFileFinder]

            1 Reply Last reply
            0
            • L Lost User

              Unlocker utilizes a driver "\\DosDevices\\UnlockerDriver5" to unlink the handle from the kernels internel handle linked list. The driver file image is named 'UnlockerDriver5.sys' and resides in the installation folder. For most locked files it is using UnlockerHook.dll which is closing the file handle from inside the remote process. For those files locked by the SYSTEM it uses the driver. I highly recommend that you do not duplicate this 'unlocking' behavior. Best Wishes, -David Delaune

              N Offline
              N Offline
              Naveen
              wrote on last edited by
              #7

              Randor wrote:

              For most locked files it is using UnlockerHook.dll which is closing the file handle from inside the remote process

              Exactly. Me too found out the CreateFile and WriteFile calls with this driver during the program flow.

              Randor wrote:

              For most locked files it is using UnlockerHook.dll which is closing the file handle from inside the remote process

              So you are saying that if I try to close the handle opened by service.exe, it is using the UnlockerHook.dll ?? So any guess which function it might be calling.. A similiar way I tried is using the CreateRemoteThread() by passing address of Closehandle as function entry and the handle as the parameter to it. In that case also handle closed successfully but coudn't delete the file.

              nave [OpenedFileFinder]

              1 Reply Last reply
              0
              • N Naveen

                I have been trying to delete files that is kept opened by another process. For this, I took the handle of the file, Duplicate it and the call the CloseHandle with that handle. It works fine for almost all files. But in some cases, even though I was able to close the handle, I coudn't delete it. I am able to rename it also. For eg the event log files like CstEvent.Evt, AppEvent.Evt etc in the "C:\WINDOWS\system32\config" folder (This files are used by services.exe). If any one knows the reason, please tell me. Thanks

                nave [OpenedFileFinder]

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #8

                Can you rename file and then delete it?

                N 1 Reply Last reply
                0
                • H Hamid Taebi

                  Can you rename file and then delete it?

                  N Offline
                  N Offline
                  Naveen
                  wrote on last edited by
                  #9

                  No I am not able to delete it even after renaming...

                  nave [OpenedFileFinder]

                  H 1 Reply Last reply
                  0
                  • N Naveen

                    No I am not able to delete it even after renaming...

                    nave [OpenedFileFinder]

                    H Offline
                    H Offline
                    Hamid Taebi
                    wrote on last edited by
                    #10

                    Did you check close threads or susped threads of that process?

                    N 1 Reply Last reply
                    0
                    • H Hamid Taebi

                      Did you check close threads or susped threads of that process?

                      N Offline
                      N Offline
                      Naveen
                      wrote on last edited by
                      #11

                      Hamid. wrote:

                      Did you check close threads or susped threads of that process?

                      Terminating the thread of that process is no possible. Also I dont know which thread of the process opened that handle. BTW do you think the file handle have any relation with the thread created it?

                      nave [OpenedFileFinder]

                      H 1 Reply Last reply
                      0
                      • N Naveen

                        Hamid. wrote:

                        Did you check close threads or susped threads of that process?

                        Terminating the thread of that process is no possible. Also I dont know which thread of the process opened that handle. BTW do you think the file handle have any relation with the thread created it?

                        nave [OpenedFileFinder]

                        H Offline
                        H Offline
                        Hamid Taebi
                        wrote on last edited by
                        #12

                        I guess yeah(but not sure) I saw this case with Explorer(I think it has 57 threads) on the XP and also Im agree with you its not safe way to terminate threads but when you told that you could to closehandle but you cant delete file.

                        N 1 Reply Last reply
                        0
                        • H Hamid Taebi

                          I guess yeah(but not sure) I saw this case with Explorer(I think it has 57 threads) on the XP and also Im agree with you its not safe way to terminate threads but when you told that you could to closehandle but you cant delete file.

                          N Offline
                          N Offline
                          Naveen
                          wrote on last edited by
                          #13

                          Hamid. wrote:

                          I guess yeah(but not sure) I saw this case with Explorer(I think it has 57 threads) on the XP and

                          I would like to know more about this. You mean you have faced secnario like "Renaming is possible but deletion is not possible" with explorer?

                          nave [OpenedFileFinder]

                          P H 2 Replies Last reply
                          0
                          • N Naveen

                            Hamid. wrote:

                            I guess yeah(but not sure) I saw this case with Explorer(I think it has 57 threads) on the XP and

                            I would like to know more about this. You mean you have faced secnario like "Renaming is possible but deletion is not possible" with explorer?

                            nave [OpenedFileFinder]

                            P Offline
                            P Offline
                            poda
                            wrote on last edited by
                            #14

                            I am also facing a situation same as you,need to delete a file which is locked by another process. But still do not know the solution. For your reference,he is the code to do this,but this application is also not very successful on deleting. http://sourceforge.net/project/showfiles.php?group_id=151239[^] In my case I need to delete the locked file,and again create it using a Linker(ILINK32.exe) to produce a DLL. Even when using Unlocker to delete the file,it is not possible to create the same file.The linker gives me error like "Cannot release virtual memory at addr xxxx for xxxxx bytes".

                            N 1 Reply Last reply
                            0
                            • P poda

                              I am also facing a situation same as you,need to delete a file which is locked by another process. But still do not know the solution. For your reference,he is the code to do this,but this application is also not very successful on deleting. http://sourceforge.net/project/showfiles.php?group_id=151239[^] In my case I need to delete the locked file,and again create it using a Linker(ILINK32.exe) to produce a DLL. Even when using Unlocker to delete the file,it is not possible to create the same file.The linker gives me error like "Cannot release virtual memory at addr xxxx for xxxxx bytes".

                              N Offline
                              N Offline
                              Naveen
                              wrote on last edited by
                              #15

                              It will not work for me also. He is just closing the handle using the CreateRemoteThread, which i already explained here http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2552620[^] From you name it seems ur a malayalee? arent u? :)

                              nave [OpenedFileFinder]

                              1 Reply Last reply
                              0
                              • N Naveen

                                Hamid. wrote:

                                I guess yeah(but not sure) I saw this case with Explorer(I think it has 57 threads) on the XP and

                                I would like to know more about this. You mean you have faced secnario like "Renaming is possible but deletion is not possible" with explorer?

                                nave [OpenedFileFinder]

                                H Offline
                                H Offline
                                Hamid Taebi
                                wrote on last edited by
                                #16

                                It returns to 2 or 3 years ago when I tried to monitor and control my system I saw we have a lot of detaily on the memory and memory of processes for example I deleted all threads of Explorer except a thread ot two threads of it but it works without problem I tried to copy/move/paste/open new window and other actions but it doesnt show me any error but when I deleted a thread of it then it finished,it was like when you close it of Task manager ,But your ptoblem is like when you want to close some processes that are depedns to an antivirus you cant delete then until you inactive anitivirus,but I have a suggestion for you you cant test this action on your system (and answer is determined because its harmful for your system) but you can test your program on the virtual PC with a OS.

                                N 1 Reply Last reply
                                0
                                • H Hamid Taebi

                                  It returns to 2 or 3 years ago when I tried to monitor and control my system I saw we have a lot of detaily on the memory and memory of processes for example I deleted all threads of Explorer except a thread ot two threads of it but it works without problem I tried to copy/move/paste/open new window and other actions but it doesnt show me any error but when I deleted a thread of it then it finished,it was like when you close it of Task manager ,But your ptoblem is like when you want to close some processes that are depedns to an antivirus you cant delete then until you inactive anitivirus,but I have a suggestion for you you cant test this action on your system (and answer is determined because its harmful for your system) but you can test your program on the virtual PC with a OS.

                                  N Offline
                                  N Offline
                                  Naveen
                                  wrote on last edited by
                                  #17

                                  Thanks for the information. Any how I cannot do this because, I dont know which thread of the application opened the file.

                                  nave [OpenedFileFinder]

                                  P H 2 Replies Last reply
                                  0
                                  • N Naveen

                                    Thanks for the information. Any how I cannot do this because, I dont know which thread of the application opened the file.

                                    nave [OpenedFileFinder]

                                    P Offline
                                    P Offline
                                    poda
                                    wrote on last edited by
                                    #18

                                    Before deleting the file,try using SetFileAttributes(FileName,FILE_SHARE_DELETE);

                                    N 1 Reply Last reply
                                    0
                                    • P poda

                                      Before deleting the file,try using SetFileAttributes(FileName,FILE_SHARE_DELETE);

                                      N Offline
                                      N Offline
                                      Naveen
                                      wrote on last edited by
                                      #19

                                      poda wrote:

                                      SetFileAttributes(FileName,FILE_SHARE_DELETE);

                                      FILE_SHARE_DELETE is used along with the CreateFile() API. It cannot be used in SetFileAttributes() function.

                                      nave [OpenedFileFinder]

                                      1 Reply Last reply
                                      0
                                      • N Naveen

                                        Thanks for the information. Any how I cannot do this because, I dont know which thread of the application opened the file.

                                        nave [OpenedFileFinder]

                                        H Offline
                                        H Offline
                                        Hamid Taebi
                                        wrote on last edited by
                                        #20

                                        I hope you find the answer,early.

                                        N 1 Reply Last reply
                                        0
                                        • H Hamid Taebi

                                          I hope you find the answer,early.

                                          N Offline
                                          N Offline
                                          Naveen
                                          wrote on last edited by
                                          #21

                                          No. which one?

                                          nave [OpenedFileFinder]

                                          H 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