vb进程管理
'关闭进程
Private Declare Function FindWindows Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub Command1_Click()
'winhwnd = FindWindows("...", vbNullString) '知道窗口类名,关闭
winhwnd = FindWindows(vbNullString, "Regshot 1.61e5 Final") '知道窗口标题,关闭
Call PostMessage(winhwnd, WM_CLOSE, 0&, 0&)
End Sub
'========================================
'判断进程(某程序是否运行)
Function CheckApplicationIsRun(ByVal szExeFileName As String) As Boolean
On Error GoTo Err
Dim WMI
Dim Obj
Dim Objs
CheckApplicationIsRun = False
Set WMI = GetObject("WinMgmts:")
Set Objs = WMI.InstancesOf("Win32_Process")
For Each Obj In Objs
If InStr(UCase(szExeFileName), UCase(Obj.Description)) <> 0 Then
CheckApplicationIsRun = True
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
Exit Function
End If
Next
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
Exit Function
Err:
If Not Objs Is Nothing Then Set Objs = Nothing
If Not WMI Is Nothing Then Set WMI = Nothing
End Function
Private Sub Command1_Click()
If CheckApplicationIsRun("notepad.exe") = True Then
MsgBox "已经运行了记事本程序"
Else
MsgBox "记事本程序没有运行"
End If
If CheckApplicationIsRun("1.exe") = True Then
MsgBox "已经运行了1.exe程序"
Else
MsgBox "1.exe程序没有运行"
End If
End Sub
_____________________________________________________________________________ _
中磊UG二次开发教程 梅雷著 qq1821117007
学UG就上UG网 http://www.9sug.com/ |