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.

Recommended Answers

All 11 Replies

What Operating System are you on ?

If you are on Unix or Linux then look up the kill() system call here

Depends on the operating system. In MS-Windows just go to Task Manager and stop it.

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.

You can look up ExitProcess() and TerminateProcess() on msdn. They might give you what you are looking for.

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.

commented: Much better ideas +21

Thanks Ancient i understand it now :)

The problem with the code I posted is that Notepad won't die if the user has to save the file first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.