VBS中怎么判斷進(jìn)程,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。
目前創(chuàng)新互聯(lián)建站已為上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、利川網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
vbs核心代碼
Option ExplicitDim objWMIService,colProcessList,strComputerstrComputer = "."Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'excel.exe'")If colProcessList.Count>0 ThenMsgBox "檢測(cè)到EXCEL程序運(yùn)行中,程序退出!"WScript.QuitEnd IfSet colProcessList = NothingSet objWMIService = NothingWScript.Quit
當(dāng)然你可以判斷 winrar.exe等等
下面附一個(gè)代碼,原來中文命名的,我們已經(jīng)修改為英文命名并且正常運(yùn)行了,因?yàn)闀r(shí)間問題,需要的朋友可以自行修改精簡(jiǎn)
'檢測(cè)進(jìn)程proname = "qq.exe"reName = IsProcess(proname)If reName = True Then msgbox "發(fā)現(xiàn)進(jìn)程"ElseIf reName = False Then msgbox "沒有發(fā)現(xiàn)進(jìn)程"End If'檢測(cè)進(jìn)程 優(yōu)化后的代碼If IsProcess("qq.exe") = True Then msgbox "發(fā)現(xiàn)進(jìn)程"Else msgbox "沒有發(fā)現(xiàn)進(jìn)程"End If'檢測(cè)進(jìn)程組proName_all = "qq.exe|notepad.exe"reName = IsProcessEx(proName_all)If reName = True Then msgbox "發(fā)現(xiàn)進(jìn)程"ElseIf reName = False Then msgbox "沒有發(fā)現(xiàn)進(jìn)程"End If'檢測(cè)進(jìn)程組 優(yōu)化后的代碼If IsProcessEx("qq.exe|notepad.exe") = True Then msgbox "發(fā)現(xiàn)進(jìn)程"Else msgbox "沒有發(fā)現(xiàn)進(jìn)程"End If'結(jié)束進(jìn)程 前臺(tái)執(zhí)行proname = "qq.exe" Call CloseProcess(proname, 1)'結(jié)束進(jìn)程 后臺(tái)執(zhí)行proname = "qq.exe" Call CloseProcess(proname, 0) '結(jié)束進(jìn)程組 前臺(tái)執(zhí)行proName_all = "qq.exe|notepad.exe" Call CloseProcessEx(proName_all, 1)'結(jié)束進(jìn)程組 后臺(tái)執(zhí)行proName_all = "qq.exe|notepad.exe" Call CloseProcessEx(proName_all, 0)'實(shí)例應(yīng)用 結(jié)束進(jìn)程 前臺(tái)執(zhí)行 10秒超時(shí)proname = "qq.exe"For i=1 to 10 Call CloseProcess(proname,1) Delay 1000 reName = IsProcess(proname) If reName = False Then Exit For End IfNextIf reName=True Then msgbox "結(jié)束進(jìn)程失敗"Else msgbox "結(jié)束進(jìn)程成功"End If'實(shí)例應(yīng)用 結(jié)束進(jìn)程 前臺(tái)執(zhí)行 優(yōu)化后的代碼(直到型循環(huán)) 有些進(jìn)程VBS檢測(cè)不到 所以先關(guān)閉后檢測(cè)Do Call CloseProcess("qq.exe",1) Delay 1000Loop While IsProcess("qq.exe")=Truemsgbox "結(jié)束進(jìn)程成功"'實(shí)例應(yīng)用 結(jié)束進(jìn)程組 后臺(tái)執(zhí)行 10秒超時(shí)proName_all = "qq.exe|notepad.exe"For j=1 to 10 Call CloseProcessEx(proName_all,0) Delay 1000 reName = IsProcessEx(proName_all) If reName = False Then Exit For End IfNextIf reName=True Then msgbox "結(jié)束進(jìn)程失敗"Else msgbox "結(jié)束進(jìn)程成功"End If'實(shí)例應(yīng)用 結(jié)束進(jìn)程組 后臺(tái)執(zhí)行 優(yōu)化后的代碼(直到型循環(huán)) 有些進(jìn)程VBS檢測(cè)不到 所以先關(guān)閉后檢測(cè)Do Call CloseProcessEx( "qq.exe|notepad.exe",0) Delay 1000Loop While IsProcessEx( "qq.exe|notepad.exe")=Truemsgbox "結(jié)束進(jìn)程成功"'函數(shù) 子程序部分代碼'檢測(cè)進(jìn)程Function IsProcess(ExeName) Dim WMI, Obj, Objs,i IsProcess = False Set WMI = GetObject("WinMgmts:") Set Objs = WMI.InstancesOf("Win32_Process") For Each Obj In Objs If InStr(UCase(ExeName),UCase(Obj.Description)) <> 0 Then IsProcess = True Exit For End If Next Set Objs = Nothing Set WMI = NothingEnd Function'結(jié)束進(jìn)程Sub CloseProcess(ExeName,RunMode) dim ws Set ws = createobject("Wscript.Shell") ws.run "cmd.exe /C Taskkill /f /im " & ExeName,RunMode Set ws = NothingEnd Sub'檢測(cè)進(jìn)程組Function IsProcessEx(ExeName) Dim WMI, Obj, Objs,ProcessName,i IsProcessEx = False Set WMI = GetObject("WinMgmts:") Set Objs = WMI.InstancesOf("Win32_Process") ProcessName=Split(ExeName,"|") For Each Obj In Objs For i=0 to UBound(ProcessName) If InStr(UCase(ProcessName(i)),UCase(Obj.Description)) <> 0 Then IsProcessEx = True Exit For End If Next Next Set Objs = Nothing Set WMI = NothingEnd Function'結(jié)束進(jìn)程組Sub CloseProcessEx(ExeName,RunMode) dim ws,ProcessName,CmdCode,i ProcessName = Split(ExeName, "|") For i=0 to UBound(ProcessName) CmdCode=CmdCode & " /im " & ProcessName(i) Next Set ws = createobject("Wscript.Shell") ws.run "cmd.exe /C Taskkill /f" & CmdCode,RunMode Set ws = NothingEnd Sub
關(guān)于VBS中怎么判斷進(jìn)程問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。