在VB中检查应用程序(EXE)是否已运行的程序怎么写?

duranduan 2003-08-25 11:26:42
应该是一个函数,参数是此应用程序的路径,返回值是真或假
...全文
137 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
duranduan 2003-08-26
  • 打赏
  • 举报
回复
rainstormmaster(rainstormmaster),太谢谢你了。我要给你加分。
rainstormmaster 2003-08-26
  • 打赏
  • 举报
回复
修正一下,通过判断文件可否删除进行判定,如果可以,说明没有运行,如果正在运行,肯定不能删除,会触发75号错误。当然别忘了备份、还原文件。代码如下:

Option Explicit

Private Function IsRun(ByVal filename As String) As Boolean
On Error GoTo myerr
Dim mytempfile As String
mytempfile = App.Path + "\mytemp.bak"
FileCopy filename, mytempfile
Kill filename
IsRun = False
FileCopy mytempfile, filename
Kill mytempfile
Exit Function
myerr:
Select Case Err.Number
Case 75
Kill mytempfile
IsRun = True
Exit Function
Case Else
MsgBox Err.Description

End Select
End Function
'调用
Private Sub Command1_Click()
MsgBox IsRun("d:\mc\Duba_CodeBlue.EXE")
End Sub

duranduan 2003-08-25
  • 打赏
  • 举报
回复
liushanyu(兵工厂)老大,你能不能把你的程序加点注释,而且我把应用程序的路径作为参数,在你的程序里面怎么把这个参数加进去?
duranduan 2003-08-25
  • 打赏
  • 举报
回复
rainstormmaster,你给的函数不行呀,运行到Open filename For Input Lock Read Write As #1,无论filename参数(为一个应用程序的路径)是否运行,函数IsRun的返回值都为False.这是怎么回事?
liushanyu 2003-08-25
  • 打赏
  • 举报
回复
应该是可以比较的。
下面是我找的一个例子。
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
Dim hSnapShot As Long, uProcess As PROCESSENTRY32
'Takes a snapshot of the processes and the heaps, modules, and threads used by the processes
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
'set the length of our ProcessEntry-type
uProcess.dwSize = Len(uProcess)
'Retrieve information about the first process encountered in our system snapshot
r = Process32First(hSnapShot, uProcess)
'set graphics mode to persistent
Me.AutoRedraw = True
Do While r
Me.Print Left$(uProcess.szExeFile, IIf(InStr(1, uProcess.szExeFile, Chr$(0)) > 0, InStr(1, uProcess.szExeFile, Chr$(0)) - 1, 0))
'Retrieve information about the next process recorded in our system snapshot
r = Process32Next(hSnapShot, uProcess)
Loop
'close our snapshot handle
CloseHandle hSnapShot
End Sub
duranduan 2003-08-25
  • 打赏
  • 举报
回复
我现在公司以前的程序员这样写的,但是有毛病:

Public Function ProgramRunning(strFileName As String) As Boolean
'检查程序是否已运行
Dim lSnapShot As Long '取得的快照的句柄
Dim typProcessInfo As PROCESSENTRY32
Dim bAppRunning As Boolean
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
bAppRunning = False

If lSnapShot <> 0 Then
typProcessInfo.dwSize = Len(typProcessInfo)
If (Process32First(lSnapShot, typProcessInfo)) = 1 Then
Do

If StrComp(Trim(typProcessInfo.szExeFile), Trim(strFileName), 1) = 0 Then
bAppRunning = True
Exit Do
End If
Loop Until (Process32Next(lSnapShot, typProcessInfo) <> 1)
End If
End If
CloseHandle lSnapShot
ProgramRunning = bAppRunning

End Function

问题出在Trim(typProcessInfo.szExeFile)是win2000中“进程”中的应用程序名称,而Trim(strFileName)则是应用程序的路径,两者永远也无法作比较呀。
rainstormmaster 2003-08-25
  • 打赏
  • 举报
回复
AustinLei(黄瓜杀手) 的代码不能解决问题

同意pigpag(噼里啪啦) 的,文件打开会触发70号错误,下面楼主需要的函数:

Private Function IsRun(ByVal filename As String) As Boolean
On Error GoTo myerr
Open filename For Input Lock Read Write As #1
Close #1
IsRun = False
Exit Function
myerr:
Select Case Err.Number
Case 70
IsRun = True
Exit Function
End Select
End Function
'调用
Private Sub Command1_Click()
MsgBox IsRun("d:\mc\ttest.exe")
End Sub

AustinLei 2003-08-25
  • 打赏
  • 举报
回复
If App.PrevInstance = True Then End
pigpag 2003-08-25
  • 打赏
  • 举报
回复
看看能不能独占打开

Open "exe" For Input Lock Read Write As #1

出错说明文件已被打开。对于EXE而言,“文件打开”大都意味着正在运行。

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧