Solved

Kill a process

Posted on 2006-07-14
9
308 Views
Last Modified: 2010-04-17
How can I kill a process (exe) in C++. Example say notepad.exe is running and I can see it in process viewer. I need to kill or terminate this process using c++.  I only have the name of the exe/process.

Also I need to restart the process later.

I can use TerminateProcess by I don't know how to get the handle of the process.

Pankaj
0
Comment
Question by:pb_india
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17110444
0
 
LVL 2

Expert Comment

by:kjpus
ID: 17110502
0
 
LVL 3

Expert Comment

by:x_bakos
ID: 17110727
I give you an example of the code that I use to do the same thing.

----------------------------start-------------------------------

BOOL killResult = FALSE;

// Get a handle to the process
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, procPID );
            
//Kill the process...
killResult = TerminateProcess( hProcess, 0 );

----------------------------stop-------------------------------

These are the lines that do what you want. The rest of the code is more than obvious so I don't write it here.

Hope to have helped

Bakos
0
 
LVL 3

Expert Comment

by:x_bakos
ID: 17110754
I forgot to mention that in function OpenProcess() the 3rd argument (procPID) is the PID of the process. So you have to get the process's ID. That is not a difficult thing to do...

Bakos
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Author Comment

by:pb_india
ID: 17110773
I am doing exactly the same three steps;
But when I do:
lpfGetModuleBaseName( hProc, hMod, szName,
                               sizeof(szName)/sizeof(TCHAR) );

I get garbage for szName. I am not sure why? When the process is found, the szName should give me name of process but I get garbage and hence it fails to terminate the process.
0
 
LVL 3

Expert Comment

by:x_bakos
ID: 17110867
Again,

why don't you try to take the processe's handle using its PID? It works 100% and you will do your job. The way is described in my earlier post

Bakos
0
 
LVL 2

Author Comment

by:pb_india
ID: 17110972
Like I said, I am using PID and all the steps you mentioned:

// First, get a handle to the process
          hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,aiPID[dwIndex]);
          // Now, get the process name
          if (hProc)
            {
            if (lpfEnumProcessModules(hProc, &hMod, sizeof(hMod), &dwCbneeded))
            {
                  lpfGetModuleBaseName( hProc, hMod, szName,
                               sizeof(szName)/sizeof(TCHAR) );
0
 
LVL 2

Author Comment

by:pb_india
ID: 17110992
Does it matter if my project is under unicode ?
0
 
LVL 3

Accepted Solution

by:
x_bakos earned 125 total points
ID: 17111156
If I am not misunderstanding something, in the code you provide us above you write:

>// First, get a handle to the process
>        hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,FALSE,aiPID[dwIndex]);

This means that you finally take a handle to the process with Access Rights (PROCESS_QUERY_INFORMATION | PROCESS_VM_READ). OK, but you can't use this handle to terminate the process, because you don't have this Access Right. I do not know where this part of the  code exists in your project, but I am quite sure that in order to be able to terminate a process you have to have a handle for it with PROCESS_TERMINATE Access Right. This is done by applying:

-------------
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, aiPID[dwIndex] );
-------------

Ofcourse you can use something like the following in order to have your handle able to do the other jobs you use it for:

-------------
  hProc = OpenProcess( PROCESS_TERMINATE || PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aiPID[dwIndex] );
-------------

As far as Unicode is concerned, I am not sure if there is a problem with it, but I don't really believe that this is the problem. Try the above and hope to be that.... When using Unicode you can do any conversion you want, and have anything needed in strings/char*.... and so on....

Bakos
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What is the compatible hibernate version for spring 4.3.1 10 76
changePi Challenge 15 104
Arduino EDI - Programming language 3 80
tidtcpserver connection lost handle 2 70
This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now