pb_india
asked on
Kill a process
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
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
http://www.codeproject.com/threads/getprocessid.asp
First, use EnumProcesses to get a list of processes on the system; Then, go through the list to find the process with the name you are looknig for. Finally, use terminateprocess to kill it.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/enumprocesses.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/enumerating_all_processes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/terminateprocess.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/enumprocesses.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/enumerating_all_processes.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/terminateprocess.asp
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
--------------------------
BOOL killResult = FALSE;
// Get a handle to the process
HANDLE hProcess = OpenProcess( PROCESS_TERMINATE, FALSE, procPID );
//Kill the process...
killResult = TerminateProcess( hProcess, 0 );
--------------------------
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
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
Bakos
ASKER
I am doing exactly the same three steps;
But when I do:
lpfGetModuleBaseName( hProc, hMod, szName,
sizeof(szName)/sizeof(TCHA R) );
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.
But when I do:
lpfGetModuleBaseName( hProc, hMod, szName,
sizeof(szName)/sizeof(TCHA
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.
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
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
ASKER
Like I said, I am using PID and all the steps you mentioned:
// First, get a handle to the process
hProc = OpenProcess(PROCESS_QUERY_ INFORMATIO N | PROCESS_VM_READ,FALSE,aiPI D[dwIndex] );
// Now, get the process name
if (hProc)
{
if (lpfEnumProcessModules(hPr oc, &hMod, sizeof(hMod), &dwCbneeded))
{
lpfGetModuleBaseName( hProc, hMod, szName,
sizeof(szName)/sizeof(TCHA R) );
// First, get a handle to the process
hProc = OpenProcess(PROCESS_QUERY_
// Now, get the process name
if (hProc)
{
if (lpfEnumProcessModules(hPr
{
lpfGetModuleBaseName( hProc, hMod, szName,
sizeof(szName)/sizeof(TCHA
ASKER
Does it matter if my project is under unicode ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.