Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Terminate Process with MsgBox

Status
Not open for further replies.

ReyAn

Programmer
Jul 7, 2012
1
HELP! How can I make this code like this:
No msgbox confirmation will appear if there is no running process. Then It will continue.

This code have a pop up msgbox even there is no running process.

Option Explicit


Dim strPro(3), strItem, strProcReturn, strProcStat
Dim strProcStat1, strExitCode, strCmdLine, strMsgRet
Dim objShell

Set objShell = CreateObject("wscript.shell")

strProcStat = False


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


strMsgRet = MsgBox("THERE IS AN OPEN APPLICATION THAT WILL BE TERMINATE.", 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
 
Because there is no condition for execution, therefore, it will always execute. Run through the list of processes and if one is found then execute the msgbox.
Code:
[s]if strMsgRet = 1 then[/s]
for each strItem In strPro
   strProcReturn = CheckProcess(strItem)
   if strProcReturn = TRUE then 
      [green]strMsgRet = MsgBox("THERE IS AN OPEN APPLICATION THAT WILL BE TERMINATE.", vbOKCANCEL)
      if strMsgRet = 1 then
         strCmdLine = "cmd /C taskkill /f /im " & strItem
         objShell.Run strCmdLine, 1
      end if[/green]
      strExitCode = 0
   else
      strExitCode = 321
   end if 
next

-Geates


"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top