用错误陷阱实现的: Option ExplicitPrivate 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_CodeBlue1.EXE") End Sub
lprocessid是process的id,exename是程序的名字,fqexename是程序的全路径,priority是process的优先级。 Public Sub GetProcessInfo(ByVal lProcessId As Long, ExeName As String, FQExeName As String, Priority As Long) Dim hProcess As Long Dim sExeName As String, sFQExeName As String, lPriority As Long Dim hExe As Long Dim cbNeeded As Long Dim lret As Long hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, _ 0&, _ lProcessId)
Select Case lProcessId Case 0 ' System Idle Process sExeName = "Idle Process" sFQExeName = "Idle Process" Case 2 sExeName = "System" sFQExeName = "System" Case 28 sExeName = "csrss.exe (Win32)" sFQExeName = "csrss.exe (Win32)" End Select
If hProcess <> 0 Then
' Now get the handle of the first module ' in this process, since first module is EXE hExe = 0 lret = EnumProcessModules(hProcess, hExe, 4&, cbNeeded)
If hExe <> 0 Then
' Get the name of the module sExeName = String$(MAX_PATH, 0) lret = GetModuleBaseName(hProcess, hExe, sExeName, Len(sExeName)) sExeName = Trim0(sExeName)
' Get full path name sFQExeName = String$(MAX_PATH, 0) lret = GetModuleFileNameEx(hProcess, hExe, sFQExeName, Len(sFQExeName)) sFQExeName = Trim0(sFQExeName)
' Get priority lPriority = GetPriorityClass(hProcess)
End If ' EXE <> 0
End If ' hProcess <> 0 ExeName = sExeName FQExeName = sFQExeName Priority = lPriority
' Close handle lret = CloseHandle(hProcess)
End SubPrivate Function Trim0(sName As String) As String' Keep left portion of string sName up to first 0. Useful with Win API null terminated strings.Dim x As Integer x = InStr(sName, Chr$(0)) If x > 0 Then Trim0 = Left$(sName, x - 1) Else Trim0 = sNameEnd Function Private Const MAX_PATH = 260 其他的api自己加。
Option ExplicitPrivate 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_CodeBlue1.EXE")
End Sub
思路开阔这是做一个优秀程序员的必备品质。
小弟我发现一个小问题。就是如果那个程序的属性是只读的话,就算没运行也是true
Public Sub GetProcessInfo(ByVal lProcessId As Long, ExeName As String, FQExeName As String, Priority As Long)
Dim hProcess As Long
Dim sExeName As String, sFQExeName As String, lPriority As Long
Dim hExe As Long
Dim cbNeeded As Long
Dim lret As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, _
0&, _
lProcessId)
Select Case lProcessId
Case 0 ' System Idle Process
sExeName = "Idle Process"
sFQExeName = "Idle Process"
Case 2
sExeName = "System"
sFQExeName = "System"
Case 28
sExeName = "csrss.exe (Win32)"
sFQExeName = "csrss.exe (Win32)"
End Select
If hProcess <> 0 Then
' Now get the handle of the first module
' in this process, since first module is EXE
hExe = 0
lret = EnumProcessModules(hProcess, hExe, 4&, cbNeeded)
If hExe <> 0 Then
' Get the name of the module
sExeName = String$(MAX_PATH, 0)
lret = GetModuleBaseName(hProcess, hExe, sExeName, Len(sExeName))
sExeName = Trim0(sExeName)
' Get full path name
sFQExeName = String$(MAX_PATH, 0)
lret = GetModuleFileNameEx(hProcess, hExe, sFQExeName, Len(sFQExeName))
sFQExeName = Trim0(sFQExeName)
' Get priority
lPriority = GetPriorityClass(hProcess)
End If ' EXE <> 0
End If ' hProcess <> 0
ExeName = sExeName
FQExeName = sFQExeName
Priority = lPriority
' Close handle
lret = CloseHandle(hProcess)
End SubPrivate Function Trim0(sName As String) As String' Keep left portion of string sName up to first 0. Useful with Win API null terminated strings.Dim x As Integer
x = InStr(sName, Chr$(0))
If x > 0 Then Trim0 = Left$(sName, x - 1) Else Trim0 = sNameEnd Function
Private Const MAX_PATH = 260
其他的api自己加。