Important alert: (current site time 5/19/2013 9:08:05 AM EDT)
 

VB icon

Help! Terminate Process

Email
Submitted on: 7/9/2012 5:48:39 PM
By: Rey-An  
Level: Beginner
User Rating: By 1 Users
Compatibility: VB Script
Views: 1601
 
     Terminate process if the user click OK button. If CANCEL button, the process will not terminate. (This code also display msgbox even there is no running process) Can someone help me? How can i modified this code like this: *Will not display msgbox if there is no running process* Thanks in advance! :)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: Help! Terminate Process
' Description:Terminate process if the user click OK button. If CANCEL button, the process will not terminate.
(This code also display msgbox even there is no running process)
Can someone help me? How can i modified this code like this:
*Will not display msgbox if there is no running process*
Thanks in advance! :)
' By: Rey-An
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=74434&lngWId=1'for details.'**************************************

Option Explicit
Dim strExitCode, strProcStat1, strMsgRet, strCmdLine
Dim strPro(3), strProcReturn, strProcStat, strItem
Dim objShell
Set objShell = CreateObject("wscript.shell")
strProcStat = False
	
strPro(0)="CALC.EXE"
strPro(1)="WINWORDL.EXE"
strPro(2)="NOTEPAD.EXE"
strPro(3)="OUTLOOK.EXE"
strMsgRet = MsgBox("DO YOU WANT TO EXIT?", vbOKCANCEL)
If strMsgRet = 1 then
	For Each strItem In strPro
 	strProcReturn = CheckProcess(strItem)
 		if strProcReturn = TRUE then 
			strCmdLine = "cmd /C taskkill /f /im " & strItem
		objShell.Run strCmdLine, 1
				End if
	Next
	strExitCode = 0
Else
strExitCode = 321
End If
WScript.Quit(strExitCode)
Function CheckProcess(strPro)
	Dim objShell, strCmdLine, objProcess
	Set objShell = CreateObject("wscript.shell")
	Set objProcess = GetObject("winmgmts:").execquery("select * from Win32_Process where Name='" & strPro & "' and KernelModeTime>1 ")
	if objProcess.count > 0 Then
		CheckProcess = TRUE
	End If
End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/11/2012 5:07:03 AMPawel Ciechanowicz

Asuming your program works (I guess it does).... This would either not show any message or show as many messages as there are processes:

Replace the following code:

If strMsgRet = 1 then
For Each strItem In strPro
strProcReturn = CheckProcess(strItem)
if strProcReturn = TRUE then
strCmdLine = "cmd /C taskkill /f /im " & strItem
objShell.Run strCmdLine, 1
End if
Next
strExitCode = 0
Else
strExitCode = 321
End If

with this code:

For Each strItem In strPro
strProcReturn = CheckProcess(strItem)
If strProcReturn = TRUE then
If strMsgRet = 1 then
strCmdLine = "cmd /C taskkill /f /im " & strItem
objShell.Run strCmdLine, 1
End if
End if
Next
strExitCode = 321


(If this comment was disrespectful, please report it.)

 
7/11/2012 11:13:59 AMDavid Cunha

Try this:

Option Explicit
Dim strExitCode, strProcStat1, strMsgRet, strCmdLine
Dim strPro(3), strProcReturn, strProcStat, strItem
Dim ShowMessage as boolean
Dim objShell
Set objShell = CreateObject("wscript.shell")
strProcStat = False

strPro(0)="CALC.EXE"
strPro(1)="WINWORDL.EXE"
strPro(2)="NOTEPAD.EXE"
strPro(3)="OUTLOOK.EXE"

For
Each strItem In strPro
strProcReturn = CheckProcess(strItem)
If strProcReturn = TRUE then
ShowMessage = TRUE
Exit For
Else
ShowMessage = FALSE
End If
Next

If ShowMessage = TRUE Then
strMsgRet = MsgBox("DO YOU WANT TO EXIT?", vbOKCANCEL)
If strMsgRet = 1 Then
For Each strItem In strPro
strProcReturn = CheckProcess(strItem)
if strProcReturn = TRUE then
strCmdLine = "cmd /C taskkill /f /im " & strItem
objShell.Run strCmdLine, 1
End if
Next
strExitCode = 0
Else
strExitCode = 321
End If
End If
WScript.Quit(strExitCode)
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.