We help IT Professionals succeed at work.

Kill process like Task Manager do

MainMa
MainMa asked
on
Medium Priority
3,251 Views
Last Modified: 2013-12-03
I work under Windows XP. How can I kill a process in Visual C++ program, by just specifing it's name?
For example, I have Media Player running. Task Manager shows that the program name is wmplayer.exe. Now, can I end the process as I do it with Task Manager?

Thanks,
MainMa
Comment
Watch Question

jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:

Commented:
The above-mentioned method is how to do it when the program is still in relatively good condition (i.e., still processing windows messages).  You rely on it correctly processing the WM_CLOSE message.  If it is unable to do so , such as when it is in an infinite loop, or stuck waiting for a resource, then:

Get the process' handle (see 2nd above-mentioned link), and then use
::TerminateProcess().
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
The above method covers 'TerminateProcess()' also.

Author

Commented:
Is it possible to have a function like GetProcessImageFileName, but which, instead of returning the filename of an executable from a handle will return a handle from a filename? I can only use an executable filename, because, for some processes, I haven't got windows IDs or handles.
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
You need to enumerate all running processes and check whether the filename matches - see e.g. http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q175/0/30.ASP&NoWebContent=1 ("HOWTO: Enumerate Applications Using Win32 APIs") as mentioned earlier.

Commented:
@ Author

No, you can't.  Logically, this makes sense.  Which handle would be returned if two instances of an application were running (i.e., have the same name).  You see this with svchost.exe and iexplore.exe, but could see it with one of your own applications, as well.
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
jkr
CERTIFIED EXPERT
Top Expert 2012

Commented:
>>Try this:

Funny, looks like the code of both aforementioned articles combined...
>> Funny, looks like the code of both aforementioned articles combined...

I don't know anymore where i got this code. It has been some years ago and most likely it has been from of any of these sites above.

But i adopted it - i added STL, removed any MFC parts and used my own 'notation' ;-)  - and it is tested now.

Regards, Alex

Author

Commented:
Sorry, I were absent, so I couldn't answer. The code, given by "itsmeandnobodyelse" works, but only for some programs. I haven't much time to see what doesn't work, but I suppose I can ask for more information after accepting your answer.

MainMa
Because of this statement:

  HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, dw32Id);

you can only kill processes that allow process termination.

However, when i replaced MFC CString by std::string i made a mistake that makes the flag bKill always true. After calling killproc i got a black desktop cause all programs had been teminated. I had to call TaskManager using CTRL-ALT-DEL and restart explorer to get my desktop back ;-)

Regards, Alex

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.