Solved

Kill a process

Posted on 2006-07-14
9
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

763 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