Results 1 to 11 of 11

Thread: Forcing file to close

  1. #1

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512

    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

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    What kind of file?

  3. #3
    jim mcnamara
    Guest
    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.

  4. #4
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    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.

  5. #5

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    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

  6. #6
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    oh! close it with this code. It works with the file path
    of the target program!

    VB Code:
    1. Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    2. Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    3. Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    4. Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    5. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    6. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    7.  
    8. Type PROCESSENTRY32
    9.     dwSize As Long
    10.     cntUsage As Long
    11.     th32ProcessID As Long
    12.     th32DefaultHeapID As Long
    13.     th32ModuleID As Long
    14.     cntThreads As Long
    15.     th32ParentProcessID As Long
    16.     pcPriClassBase As Long
    17.     dwFlags As Long
    18.     szexeFile As String * 6400
    19. End Type
    20.  
    21.  
    22. Public Function StopApp(myName As String) As Boolean
    23.     Const PROCESS_ALL_ACCESS = 0
    24.     Dim uProcess As PROCESSENTRY32
    25.     Dim rProcessFound As Long
    26.     Dim hSnapshot As Long
    27.     Dim szExename As String
    28.     Dim exitCode As Long
    29.     Dim myProcess As Long
    30.     Dim AppKill As Boolean
    31.     Dim appCount As Integer
    32.     Dim i As Integer
    33.     On Local Error GoTo Finish
    34.     appCount = 0
    35.    
    36.     Const TH32CS_SNAPPROCESS As Long = 2&
    37.    
    38.     uProcess.dwSize = Len(uProcess)
    39.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    40.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    41.    
    42.     Do While rProcessFound
    43.         i = InStr(1, uProcess.szexeFile, Chr(0))
    44.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    45.         If Right$(szExename, Len(myName)) = LCase$(myName) Then
    46.             StopApp = True
    47.             appCount = appCount + 1
    48.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    49.             AppKill = TerminateProcess(myProcess, exitCode)
    50.             Call CloseHandle(myProcess)
    51.         End If
    52.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    53.     Loop
    54.  
    55.     Call CloseHandle(hSnapshot)
    56. Finish:
    57. End Function

    VB Code:
    1. [b]Usage:[/b]
    2. KillAppByPath "C:\Windows\Calc.exe"

  7. #7

    Thread Starter
    Frenzied Member Blobby's Avatar
    Join Date
    Oct 2001
    Location
    England
    Posts
    1,512
    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

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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!)

  9. #9
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    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

  10. #10
    Frenzied Member
    Join Date
    Sep 1999
    Location
    Phoenix, az
    Posts
    1,517
    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 =)

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    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
  •  



Click Here to Expand Forum to Full Width