1. ホーム
  2. スクリプト・コラム
  3. 腹筋

VBS処理判定コード

2022-01-07 06:39:47

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 "Detected EXCEL program running, the program exits! "
	WScript.Quit
End If
Set colProcessList = Nothing
Set objWMIService = Nothing
WScript.Quit

もちろんwinrar.exeなどの判定も可能です。

ここでは、コードは、元の中国の名前は、BinaryDevelopは、英語の名前と通常の操作に変更されている、時間の問題のために、あなたは友人を変更する必要があります合理化することができる

'Detect process
proname = "qq.exe"
reName = IsProcess(proname)
If reName = True Then
  msgbox "Process found"
ElseIf reName = False Then
  msgbox "No process found"
End If
'Detect process Optimized code
If IsProcess("qq.exe") = True Then 
  msgbox "Process found"
Else 
  msgbox "No process found"
End If
'Detect process group
proName_all = "qq.exe|notepad.exe"
reName = IsProcessEx(proName_all)
If reName = True Then
  msgbox "Found process"
ElseIf reName = False Then
  msgbox "No process found"
End If
'Detect process group Optimized code
If IsProcessEx("qq.exe|notepad.exe") = True Then 
  msgbox "Process found"
Else 
  msgbox "No process found"
End If
'End process foreground execution
proname = "qq.exe"
  Call CloseProcess(proname, 1)
'End the process.
proname = "qq.exe"
  Call CloseProcess(proname, 0)
  'End the process group.
proName_all = "qq.exe|notepad.exe"
  Call CloseProcessEx(proName_all, 1)
'End the process group Execute in the background
proName_all = "qq.exe|notepad.exe"
  Call CloseProcessEx(proName_all, 0)
'Example application End process foreground execution 10 seconds timeout
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 "Failed to end process"
Else
  msgbox "End process successfully"
End If
'Example application End process Execute in foreground Optimized code (until loop) Some processes are not detected by VBS, so close first and detect later
Do
  Call CloseProcess("qq.exe",1)
  Delay 1000
Loop While IsProcess("qq.exe")=True
msgbox "End process successfully"
'Example application End process group Background execution 10 seconds timeout
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 "Failed to end process"
Else
  msgbox "End process successfully"
End If
'Example application End process group Execute optimized code in background (until loop) Some processes are not detected by VBS so close first and detect later
Do
  Call CloseProcessEx( "qq.exe|notepad.exe",0)
  Delay 1000
Loop While IsProcessEx( "qq.exe|notepad.exe")=True
msgbox "End process successfully"
' function Subroutine part of the code
'Detect process
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
'End the process
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
'Detect process group
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
'End the process group
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プロセスの決定に関する今回の記事は以上です。