|
-
Dec 6th, 2001, 12:46 AM
#1
Thread Starter
Frenzied Member
Forcing file to close
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
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Dec 7th, 2001, 10:01 AM
#2
-
Dec 7th, 2001, 10:34 AM
#3
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.
-
Dec 7th, 2001, 11:08 AM
#4
Frenzied Member
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.
-
Dec 7th, 2001, 05:24 PM
#5
Thread Starter
Frenzied Member
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
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Dec 7th, 2001, 05:29 PM
#6
Frenzied Member
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"
-
Dec 8th, 2001, 02:40 AM
#7
Thread Starter
Frenzied Member
Thanks Evan. How does that work if you have multiple instances of a program then? ....just out of curiosity.....
There are 3 types of people in this world.........those that can count, and those that can't.
Blobby
-
Dec 10th, 2001, 08:28 AM
#8
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!)
-
Dec 10th, 2001, 11:56 AM
#9
Frenzied Member
Originally posted by Blobby
Thanks Evan. How does that work if you have multiple instances of a program then? ....just out of curiosity.....
Terminites all of them
-
Dec 10th, 2001, 11:56 AM
#10
Frenzied Member
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!)
Oops! But ty =)
-
Dec 10th, 2001, 03:38 PM
#11
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|