VB 如何检测一个程序是否在运行中?

VB 如何检测一个程序是否在运行中?,第1张

看是否运行:

Private

Declare

Function

FindWindow

Lib

"user32"

Alias

"FindWindowA"

(ByVal

lpClassName

As

String,

ByVal

lpWindowName

As

String)

As

Long

Private

Sub

Command5_Click()

Dim

lHwnd

As

Long

lHwnd

=

FindWindow(vbNullString,

"程序的Title或Caption")

If

lHwnd

<>

0

Then

MsgBox

"程序正在运行!"

End

If

End

Sub

向它发送指令

AppActivate

"程序的Title或Caption"

SendKeys

"指令"

要启动程序:

Call

Shell("完整路径和程序名称.exe")

'检查进程是否运行,根据进程名

Private Function CheckExeIsRun(exeName As String) As Boolean

On Error GoTo Err

Dim WMI

Dim Obj

Dim Objs

CheckExeIsRun = 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

CheckExeIsRun = 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 CheckExeIsRun("A.exe") Then

MsgBox "运行了啊"

Else

MsgBox "没有运行"

End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11977347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存