欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

VBS进程判断代码

程序员文章站 2022-03-07 13:14:24
vbs核心代码 option explicit dim objwmiservice,colprocesslist,strcomputer strcomputer = "....

vbs核心代码

option explicit
dim objwmiservice,colprocesslist,strcomputer
strcomputer = "."
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 then
	msgbox "检测到excel程序运行中,程序退出!"
	wscript.quit
end if
set colprocesslist = nothing
set objwmiservice = nothing
wscript.quit

当然你可以判断 winrar.exe等等

下面附一个代码,原来中文命名的,已经修改为英文命名并且正常运行了,因为时间问题,需要的朋友可以自行修改精简

'检测进程
proname = "qq.exe"
rename = isprocess(proname)
if rename = true then
  msgbox "发现进程"
elseif rename = false then
  msgbox "没有发现进程"
end if
'检测进程 优化后的代码
if isprocess("qq.exe") = true then 
  msgbox "发现进程"
else 
  msgbox "没有发现进程"
end if
'检测进程组
proname_all = "qq.exe|notepad.exe"
rename = isprocessex(proname_all)
if rename = true then
  msgbox "发现进程"
elseif rename = false then
  msgbox "没有发现进程"
end if
'检测进程组 优化后的代码
if isprocessex("qq.exe|notepad.exe") = true then 
  msgbox "发现进程"
else 
  msgbox "没有发现进程"
end if
'结束进程 前台执行
proname = "qq.exe"
  call closeprocess(proname, 1)
'结束进程 后台执行
proname = "qq.exe"
  call closeprocess(proname, 0)
  '结束进程组 前台执行
proname_all = "qq.exe|notepad.exe"
  call closeprocessex(proname_all, 1)
'结束进程组 后台执行
proname_all = "qq.exe|notepad.exe"
  call closeprocessex(proname_all, 0)
'实例应用 结束进程 前台执行 10秒超时
proname = "qq.exe"
for i=1 to 10
  call closeprocess(proname,1)
  delay 1000
  rename = isprocess(proname)
  if rename = false then
    exit for
  end if
next
if rename=true then
  msgbox "结束进程失败"
else
  msgbox "结束进程成功"
end if
'实例应用 结束进程 前台执行 优化后的代码(直到型循环) 有些进程vbs检测不到 所以先关闭后检测
do
  call closeprocess("qq.exe",1)
  delay 1000
loop while isprocess("qq.exe")=true
msgbox "结束进程成功"
'实例应用 结束进程组 后台执行 10秒超时
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 if
next
if rename=true then
  msgbox "结束进程失败"
else
  msgbox "结束进程成功"
end if
'实例应用 结束进程组 后台执行 优化后的代码(直到型循环) 有些进程vbs检测不到 所以先关闭后检测
do
  call closeprocessex( "qq.exe|notepad.exe",0)
  delay 1000
loop while isprocessex( "qq.exe|notepad.exe")=true
msgbox "结束进程成功"
'函数 子程序部分代码
'检测进程
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 = nothing
end function
'结束进程
sub closeprocess(exename,runmode)
  dim ws
  set ws = createobject("wscript.shell")
  ws.run "cmd.exe /c taskkill /f /im " & exename,runmode
  set ws = nothing
end sub
'检测进程组
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 = nothing
end function
'结束进程组
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 = nothing
end sub

好了这篇关于vbs进程判断的文章就介绍到这

相关标签: VBS 进程 判断