Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Killing a process with a batch

256 views
Skip to first unread message

Sandman

unread,
Mar 27, 2002, 7:02:56 PM3/27/02
to
First off thank you to anybody who even reads this post and even more
gratitude for those that reply, however helpful. :)

Okay, I'm new to batch programming and I'd like to write a batch file that
takes as a parameter a String (or maybe an int?) which represents a running
process and kills that process (similar to the End Process button in the
Windows Task Manager). I'm not sure if it matters, but it would need to
work on a NT/2000 machine.

Okay, now being new to batch programs and such, my problem is two fold:
1. how do I kill a process from the command line
2. how do I identify a process (is it a number or a string?). if it is a
number (which I believe intuitively it is), how would I aquire that number
from a command line given that I only had the string representation of the
process?

please be gentle,
--josh

"If I had a tumor, I'd name it Marla."


Dean Wells

unread,
Mar 27, 2002, 7:07:16 PM3/27/02
to
Processes can be referenced by their PID, executable image name or
window title when using the KILL.EXE command from the Support Tools. You
may be interested in TLIST.EXE as well.

HTH

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Sandman" <liam...@hotmail.com> wrote in message
news:#XoKYte1BHA.2532@tkmsftngp04...
: First off thank you to anybody who even reads this post and even more

:
:


Sandman

unread,
Mar 28, 2002, 2:46:00 PM3/28/02
to
thank you sincerely, Dean, for pointing me in the right direction. now,
does anybody have any more contributions (don't hold back. :) )?
--josh

--


"If I had a tumor, I'd name it Marla."

"Dean Wells" <dwe...@mask.msetechnology.com> wrote in message
news:a7tmrm$o4dcq$1...@ID-132980.news.dfncis.de...

Alex K. Angelopoulos

unread,
Mar 28, 2002, 4:11:43 PM3/28/02
to
Yes... but I don't think you want the API call info - unless you want to write your own "kill"... :)


"Sandman" <liam...@hotmail.com> wrote in message news:OSQ$jCp1BHA.2112@tkmsftngp02...


> thank you sincerely, Dean, for pointing me in the right direction. now,
> does anybody have any more contributions (don't hold back. :) )?
> --josh
>

(SNIP)

Dean Wells

unread,
Mar 28, 2002, 4:20:11 PM3/28/02
to
What more do you need?

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]
R e m o v e t h e m a s k t o s e n d e m a i l


"Sandman" <liam...@hotmail.com> wrote in message

news:OSQ$jCp1BHA.2112@tkmsftngp02...
: thank you sincerely, Dean, for pointing me in the right direction.

: > :
: >
: >
:
:


Alex K. Angelopoulos

unread,
Mar 28, 2002, 4:41:42 PM3/28/02
to
Well, maybe he *did* want this...

This is actually handy from a scripting standpoint since it can be compiled into an ActiveX DLL and then just called with
object.KillProcess <processID>.

In fact, you can use it from VBA to kill process, so you can actually start a VBScript that starts VBA that calls this to kill something - no kill command or WMI needed.

I wouldn't recommend this for the casual user though. And no, I didn't just do this for the heck of it - I was working on it yesterday and figured this post would be a good excuse to modify it for easy scriptable use.

FWIW, I think your method is faster, Dean... :)))

'=============================

1. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
2. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
3. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
4. Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
5. Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
6. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
7. Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
8. Private Const ANYSIZE_ARRAY = 1
9. Private Const TOKEN_ADJUST_PRIVILEGES = &H20
10. Private Const TOKEN_QUERY = &H8
11. Private Const SE_DEBUG_NAME = "SeDebugPrivilege"
12. Private Const SE_PRIVILEGE_ENABLED = &H2
13. Private Const PROCESS_TERMINATE = &H1
14. Private Type LUID
15. LowPart As Long
16. HighPart As Long
17. End Type
18. Private Type LUID_AND_ATTRIBUTES
19. pLuid As LUID
20. Attributes As Long
21. End Type
22. Private Type TOKEN_PRIVILEGES
23. PrivilegeCount As Long
24. TheLuid As LUID
25. Attributes As Long
26. End Type
27. Dim m_lProcessID As Long
28.
29.
30. Public Sub AdjustToken()
31. Dim hdlProcessHandle As Long
32. Dim hdlTokenHandle As Long
33. Dim tmpLuid As LUID
34. Dim tkp As TOKEN_PRIVILEGES
35. Dim tkpNewButIgnored As TOKEN_PRIVILEGES
36. Dim lBufferNeeded As Long
37. hdlProcessHandle = GetCurrentProcessId()
38. OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
39. LookupPrivilegeValue "", SE_DEBUG_NAME, tmpLuid
40. tkp.PrivilegeCount = 1 ' One privilege to set
41. tkp.TheLuid = tmpLuid
42. tkp.Attributes = SE_PRIVILEGE_ENABLED
43. ' Enable the kill privilege in the access token of this
44. ' process.
45. AdjustTokenPrivileges hdlTokenHandle, False, tkp, _
46. Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
47. End Sub
48. Public Sub KillProcess(m_lProcessID As Long)
49. Dim hProcess As Long
50. AdjustToken
51. hProcess = OpenProcess(PROCESS_TERMINATE, False, m_lProcessID)
52. TerminateProcess hProcess, -1&
53. CloseHandle hProcess
54. End Sub
55.

'=============================

"Dean Wells" <dwe...@mask.msetechnology.com> wrote in message news:a801ec$okhan$1...@ID-132980.news.dfncis.de...

Phil Robyn

unread,
Mar 28, 2002, 5:07:24 PM3/28/02
to
"Alex K. Angelopoulos" wrote:
>
> Well, maybe he *did* want this...
>
> This is actually handy from a scripting standpoint since it can be compiled into an ActiveX DLL and then just called with
> object.KillProcess <processID>.
>
> In fact, you can use it from VBA to kill process, so you can actually start a VBScript that starts VBA that calls this to kill something - no kill command or WMI needed.
>
> I wouldn't recommend this for the casual user though. And no, I didn't just do this for the heck of it - I was working on it yesterday and figured this post would be a good excuse to modify it for easy scriptable use.
>
> FWIW, I think your method is faster, Dean... :)))

for /f "tokens=1" %a in ('tlist ^| findstr /i "your process or program"') do echo kill %a /f


If this appears to do what you want, remove the word 'echo'. If you want to run it as a batch file,
then

for /f "tokens=1" %%a in ('tlist ^| findstr /i %1') do echo kill %%a /f

where '%1' is the window title or program name of the process.

--

u n z i p m y a d d r e s s t o s e n d e - m a i l

Alex K. Angelopoulos

unread,
Mar 28, 2002, 5:16:39 PM3/28/02
to
Give up, Phil. I hold the record for the longest solution.. :))

"Phil Robyn" <pro...@uclink.berkzipeley.edu> wrote in message news:3CA3941C...@uclink.berkzipeley.edu...

(snip)


>
> for /f "tokens=1" %a in ('tlist ^| findstr /i "your process or program"') do echo kill %a /f
>
>
> If this appears to do what you want, remove the word 'echo'. If you want to run it as a batch file,
> then
>
> for /f "tokens=1" %%a in ('tlist ^| findstr /i %1') do echo kill %%a /f
>
> where '%1' is the window title or program name of the process.
>
>

(snip)

Dean Wells

unread,
Mar 28, 2002, 6:02:59 PM3/28/02
to
KILL.EXE does that by itself (with the exception of the active command
prompt) and is capable of using the PID or pattern matching against the
executable image name or window title.

Dean

--
Dean Wells [MVP / Windows platform]
MSEtechnology
[[ Please respond to the Newsgroup only regarding posts ]]

R e m o v e t h e m a s k t o s e n d e m a i l


"Phil Robyn" <pro...@uclink.berkzipeley.edu> wrote in message
news:3CA3941C...@uclink.berkzipeley.edu...

Sandman

unread,
Mar 29, 2002, 1:02:02 AM3/29/02
to
thank you all for you're contributions. i always like to look at a problem
through multiple sets of eyes (no offense intended Dean). I am eternally
gracious.
--josh

--


"If I had a tumor, I'd name it Marla."

"Sandman" <liam...@hotmail.com> wrote in message
news:#XoKYte1BHA.2532@tkmsftngp04...

Al Dunbar

unread,
Mar 29, 2002, 8:51:46 PM3/29/02
to

"Sandman" <liam...@hotmail.com> wrote in message
news:eHzx1au1BHA.392@tkmsftngp03...

> thank you all for you're contributions. i always like to look at a
problem
> through multiple sets of eyes (no offense intended Dean). I am eternally
> gracious.

"Eternally grateful" I could believe, "eternally gracious" simply remains to
be seen (and through multiple sets of eyes).

0 new messages