If a file has been abnormally terminated which has left it open, Is it possible to forcefully close it so it can be deleted?
Thanks in advance
Printable View
If a file has been abnormally terminated which has left it open, Is it possible to forcefully close it so it can be deleted?
Thanks in advance
What kind of file?
Without going into OS internals - No. The OS has a list of file handles. If a process aborts (like a GPF) it doesn't release the handle. Rebooting is the only solution. Period.
IF you want to see what's going on with files download the free PStools from www.sysinternals.com - there is a FileMon tool (or whatever they call it now). Shows all open files, handles etc.
You can see exactly what I mean with this tool. They may have a another tool which can force file handles off the list - I dunno. The toolset changes constantly.
If you open a bunch of files and then you get an error, in your error handling, just use the command Close. It will close every file you have open. I dont know if thats what you want though.
Its a program running in a DOS window executed by a batch command. It isnt my program that has the handle.
Thanks for your help guys
oh! close it with this code. It works with the file path
of the target program!
VB Code:
Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szexeFile As String * 6400 End Type Public Function StopApp(myName As String) As Boolean Const PROCESS_ALL_ACCESS = 0 Dim uProcess As PROCESSENTRY32 Dim rProcessFound As Long Dim hSnapshot As Long Dim szExename As String Dim exitCode As Long Dim myProcess As Long Dim AppKill As Boolean Dim appCount As Integer Dim i As Integer On Local Error GoTo Finish appCount = 0 Const TH32CS_SNAPPROCESS As Long = 2& uProcess.dwSize = Len(uProcess) hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&) rProcessFound = ProcessFirst(hSnapshot, uProcess) Do While rProcessFound i = InStr(1, uProcess.szexeFile, Chr(0)) szExename = LCase$(Left$(uProcess.szexeFile, i - 1)) If Right$(szExename, Len(myName)) = LCase$(myName) Then StopApp = True appCount = appCount + 1 myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID) AppKill = TerminateProcess(myProcess, exitCode) Call CloseHandle(myProcess) End If rProcessFound = ProcessNext(hSnapshot, uProcess) Loop Call CloseHandle(hSnapshot) Finish: End Function
VB Code:
[b]Usage:[/b] KillAppByPath "C:\Windows\Calc.exe"
Thanks Evan. How does that work if you have multiple instances of a program then? ....just out of curiosity.....
Evan: What is KillAppByPath in the usage section of your post?
Is there a missing sub or function, or did you mean StopApp?
(I like the code a lot!)
Terminites all of themQuote:
Originally posted by Blobby
Thanks Evan. How does that work if you have multiple instances of a program then? ....just out of curiosity.....
Oops! But ty =)Quote:
Originally posted by Hack
Evan: What is KillAppByPath in the usage section of your post?
Is there a missing sub or function, or did you mean StopApp?
(I like the code a lot!)
Actually, I like KillAppByName a lot better than StopApp, so when I dumped it into my code library (properly documented with respect to posted by and when posted of course), I changed the name of the sub.
Up to this one, the only code I had to kill a running application required that you know the window caption. I like this one better.