Hey guys, well first off i'm a new member here. My question is how do you go about killing/stopping a process? Thanks, Clipper34.

I'm running on Windows Vista, but say if i create a process using this:

int OpenProcess( LPTSTR process_name )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );

int retval = CreateProcess( process_name,
NULL,NULL,NULL,FALSE, NULL, 
NULL,NULL,&si,&pi);

CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );

return retval;
}

And i want to kill that process and another thats existing allready. e.g., notepad.exe Thanks, Clipper34.

Thanks for the replys, i have a concept of how the function works but not the usage. Would it be something along the lines of this:

#include <windows.h>

using namespace std;

int main() {
    BOOL TerminateProcess(
    IN HANDLE "notepad.exe
    IN DWORD "0"    
    );
    
return 0;


}

Thanks, Clipper34.

Well, not really. Call TermninateProcess() only if you already know the HANDLE to the process, such as when you called CreateProcess() to spawn the process. If you did not call CreateProcess() then all you can do is get the HWND of the window you want to kill and then send it a WM_CLOSE event message.

First call EnumWindows(), which will give you the HWND of every top-level window on the computer.

Here is how to kill Notepad.exe

#include <windows.h>
#include <iostream>
#include <cstring>


BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
    char WindowText[255] = {0};
    GetWindowText(hwnd,WindowText,sizeof(WindowText));
    if( strstr(WindowText,"Notepad") != NULL)
    {
        std::cout << WindowText << "\n";
        SendNotifyMessage(hwnd,WM_CLOSE,0,0);
        return FALSE;
    }
    return TRUE;
}


int main()
{
   BOOL b = EnumWindows(EnumWindowsProc, 0);
}

Good grief. Only use TerminateProcess() to kill the unruly.

Otherwise, just ask it nicely: send it a WM_CLOSE message. If it doesn't respond in, say, 500 ms or so, use CreateRemoteThread() and ExitProcess(). If it still doesn't quit after another half-second or so, then swat it with TerminateProcess().

Hope this helps.

Comments
Much better ideas
This article has been dead for over six months. Start a new discussion instead.