Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Kill a process

Posted on 2006-07-14
9
Medium Priority
?
314 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

Technology Partners: 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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

721 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