dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] Should I use CloseHandle even if process is "killed" using TerminateProcess?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Resolved [RESOLVED] Should I use CloseHandle even if process is "killed" using TerminateProcess?

    I use this code to terminate a process:
    Code:
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal handle As Long) As Long
    
    Dim ProcessHandle As Long
    
    
    Public Function KillProcess(ProcessID As Long)
    
    ProcessHandle = OpenProcess(&H1, False, ProcessID)
    
    Call TerminateProcess(ProcessHandle, 0)
    
    Call CloseHandle(ProcessHandle)
    
    End Function
    Should I use CloseHandle even if process is "killed" using TerminateProcess?

  2. #2
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,057

    Re: Should I use CloseHandle even if process is "killed" using TerminateProcess?

    According to Process Handles and Identifiers:

    Quote Originally Posted by MSDN
    The handles are valid until closed, even after the process or thread they represent has been terminated.
    You should always close any handle you open/create/duplicate/obtain/etc. to avoid memory leaks and related issues, unless the API mentions that it's unnecessary.


    BTW, terminating a process (or thread) abruptly is bad practice.

    Quote Originally Posted by MSDN
    If a process is terminated by TerminateProcess, all threads of the process are terminated immediately with no chance to run additional code. This means that the thread does not execute code in termination handler blocks. In addition, no attached DLLs are notified that the process is detaching. If you need to have one process terminate another process, the following steps provide a better solution: Read more...
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  3. #3
    Frenzied Member
    Join Date
    Feb 2015
    Posts
    1,584

    Re: Should I use CloseHandle even if process is "killed" using TerminateProcess?

    Yes. When calling OpenProcess create a new handle, which is valid only for your process. When calling TerminateProcess process is completed and close all handles creating / opening them. Your handle is not closed until the process is completed or until you are not close it explicitly through CloseHandle. The opening handle, in this case, you can use to obtain information about the terminated process.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,993

    Re: Should I use CloseHandle even if process is "killed" using TerminateProcess?

    Per msdn documentation of CloseHandle
    To remove a process object, you must terminate the process, then close all handles to the process. For more information, see Terminating a Process.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: Should I use CloseHandle even if process is "killed" using TerminateProcess?

    Thank you all! RESOLVED

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width