Solved

Kill a process

Posted on 2006-07-14
9
307 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …

760 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