-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFindRunningProcess.rvb
66 lines (61 loc) · 2.78 KB
/
FindRunningProcess.rvb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FindRunningProcess.rvb -- February 2012
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Example of how to find and terminate a running process.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub TestGetRunningProcesses
Dim strFile, nCount
strFile = "Notepad.exe"
nCount = FindRunningProcess(strFile)
If (0 = nCount) Then
Call Rhino.Print("There are no instances of " & CQuote(strFile) & " running.")
ElseIf (1 = nCount) Then
Call Rhino.Print("There is 1 instance of " & CQuote(strFile) & " running.")
Else
Call Rhino.Print("There are " & CStr(nCount) & " instances of " & CQuote(strFile) & " running.")
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description:
' Tests to see if a process, or application, is running.
' Parameters:
' strName [in] - The name of the process, or application (e.g. "Notepad.exe")
' Returns:
' The number of running instances of strName
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FindRunningProcess(strName)
Dim objWMIService, colProcess
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select Name from Win32_Process WHERE Name='" & strName & "'")
FindRunningProcess = colProcess.Count
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description:
' Ends, or terminates, a process, or application.
' Parameters:
' strName [in] - The name of the process, or application (e.g. "Notepad.exe")
' Returns:
' The number of running instances of strName that were terminated.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function EndRunningProcess(strName)
Dim objWMIService, colProcess, nCount
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcess = objWMIService.ExecQuery("Select Name from Win32_Process WHERE Name='" & strName & "'")
nCount = 0
For Each objProcess In colProcessList
If (0 = objProcess.Terminate()) Then nCount = nCount + 1
Next
EndRunningProcess = nCount
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description:
' Surrounds a string with double-quote characters.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function CQuote(val)
CQuote = Chr(34) & CStr(val) & Chr(34)
End Function