api TerminateProcess

  • 19 years ago

    how does the TerminateProcess api call work and how could i use it to kill a program by its exe name?
    This is what the apim viewer says about it:


    Public Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

  • 19 years ago

    In order to use TerminateProcess you need the process handle. The exit code is sets the exit code of the application that you are terminating (in rather a drastic way!).


    Below is what the MSDN library has to say...


    Parameters


    hProcess
    [in] Handle to the process to terminate.
    Windows NT/2000: The handle must have PROCESS_TERMINATE access. For more information, see Process Security and Access Rights.


    uExitCode
    [in] Specifies the exit code for the process and for all threads terminated as a result of this call. Use the GetExitCodeProcess function to retrieve the process's exit value. Use the GetExitCodeThread function to retrieve a thread's exit value.


    Return Values


    If the function succeeds, the return value is nonzero.


    If the function fails, the return value is zero. To get extended error information, call GetLastError.


    Remarks
    The TerminateProcess function is used to unconditionally cause a process to exit. Use it only in extreme circumstances. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.


    TerminateProcess causes all threads within a process to terminate, and causes a process to exit, but DLLs attached to the process are not notified that the process is terminating.


    Terminating a process causes the following:


    All of the object handles opened by the process are closed.
    All of the threads in the process terminate their execution.
    The state of the process object becomes signaled, satisfying any threads that had been waiting for the process to terminate.
    The states of all threads of the process become signaled, satisfying any threads that had been waiting for the threads to terminate.
    The termination status of the process changes from STILL_ACTIVE to the exit value of the process.
    Terminating a process does not cause child processes to be terminated.


    Terminating a process does not necessarily remove the process object from the system. A process object is deleted when the last handle to the process is closed.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”