Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Kill a process

Posted on 2006-07-14
9
Medium Priority
?
316 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Starting up a Project
Loops Section Overview

926 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