From: Naveen on
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
From: Joseph M. Newcomer on
1 + 1 - 1 = 1

Simple arithmetic. If you take a handle, duplicate it, and close the duplicate, so what?
Nothing is going to happen to the original handle, and closing a duplicate handle closes,
guess what, the duplicate handle! It doesn't close any OTHER handle. If you think this
was working, you are probably wrong, and observed something else.

You cannot delete an open file, and duplicating a handle will not have any effect,
especially if you immediately close the duplicate (other than wasting time and
intellectual energy, the whole sequence has no net effect on the program).

You can force a handle to close with tools like the sysinternals process viewer, but they
do some VERY advanced things to make this happen, using the undocumented Windows API. You
can't do it with ordinary API calls.

If you hit a time interval during which the file might be closed, you can delete it
(whether the handle is duplicated or not), but in general the whole idea of doing this is
amazingly deeply flawed. If you manage to close a file out from under some app, you will
lose data, and the app might end up failing in interesting and creative ways.

You should not be trying to delete a file that is being kept open by another process.
joe

On Tue, 13 May 2008 19:39:00 -0700, Naveen <Naveen(a)discussions.microsoft.com> wrote:

> 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
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Doug Harrison [MVP] on
On Tue, 13 May 2008 19:39:00 -0700, Naveen
<Naveen(a)discussions.microsoft.com> wrote:

> 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.

You're essentially adding then subtracting one from a reference count that
is > 0 to start with, so this sounds like a pointless exercise.

>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.

If a file is open in another process, I assume I can't rip it away from the
other process. I wouldn't want to, because I fear I'd corrupt the file or
mess up the other process. That said, ISTR it is possible under some
conditions to rename a file that's in use, or even delete it, because the
system doesn't carry out those actions until all handles on the file are
closed. I just tried to repro this on Vista with a .pdf file in Adobe
Reader, and Explorer displayed a dialog box stating that it was open in
that process, giving me a chance to Try Again. You're not going to be able
to force the issue, so I would say just try to delete, rename, or whatever,
and if it fails, fall back on MoveFileEx.

--
Doug Harrison
Visual C++ MVP
From: Naveen on
Thanks for the reply

>> Nothing is going to happen to the original handle, and closing a duplicate handle closes,

I have specified the DUPLICATE_CLOSE_SOURCE in the DuplicateHandle()
function. so i guess, the source handle will be closed.

> You can force a handle to close with tools like the sysinternals process viewer, but they

I tried closing the handle with process viewer, But in that cases also i
wasnt able to delete the file.

>>do some VERY advanced things to make this happen, using the undocumented Windows API

Can you please tell me those things???

"Joseph M. Newcomer" wrote:

> 1 + 1 - 1 = 1
>
> Simple arithmetic. If you take a handle, duplicate it, and close the duplicate, so what?
> Nothing is going to happen to the original handle, and closing a duplicate handle closes,
> guess what, the duplicate handle! It doesn't close any OTHER handle. If you think this
> was working, you are probably wrong, and observed something else.
>
> You cannot delete an open file, and duplicating a handle will not have any effect,
> especially if you immediately close the duplicate (other than wasting time and
> intellectual energy, the whole sequence has no net effect on the program).
>
> You can force a handle to close with tools like the sysinternals process viewer, but they
> do some VERY advanced things to make this happen, using the undocumented Windows API. You
> can't do it with ordinary API calls.
>
> If you hit a time interval during which the file might be closed, you can delete it
> (whether the handle is duplicated or not), but in general the whole idea of doing this is
> amazingly deeply flawed. If you manage to close a file out from under some app, you will
> lose data, and the app might end up failing in interesting and creative ways.
>
> You should not be trying to delete a file that is being kept open by another process.
> joe
>
> On Tue, 13 May 2008 19:39:00 -0700, Naveen <Naveen(a)discussions.microsoft.com> wrote:
>
> > 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
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
From: Naveen on
> You're essentially adding then subtracting one from a reference count that
> is > 0 to start with, so this sounds like a pointless exercise.\

But I am specifying the DUPLICATE_CLOSE_SOURCE in the DuplicateHandle()
function. so I guess the spurce handle is closed.

"Doug Harrison [MVP]" wrote:

> On Tue, 13 May 2008 19:39:00 -0700, Naveen
> <Naveen(a)discussions.microsoft.com> wrote:
>
> > 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.
>
> You're essentially adding then subtracting one from a reference count that
> is > 0 to start with, so this sounds like a pointless exercise.
>
> >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.
>
> If a file is open in another process, I assume I can't rip it away from the
> other process. I wouldn't want to, because I fear I'd corrupt the file or
> mess up the other process. That said, ISTR it is possible under some
> conditions to rename a file that's in use, or even delete it, because the
> system doesn't carry out those actions until all handles on the file are
> closed. I just tried to repro this on Vista with a .pdf file in Adobe
> Reader, and Explorer displayed a dialog box stating that it was open in
> that process, giving me a chance to Try Again. You're not going to be able
> to force the issue, so I would say just try to delete, rename, or whatever,
> and if it fails, fall back on MoveFileEx.
>
> --
> Doug Harrison
> Visual C++ MVP
>