可以这样判断文件是否已经打开:Function IsOpen(sFile As String) As Boolean Dim fFile As Integer fFile = FreeFile() On Error GoTo ErrOpen Open sFile For Binary Lock Read Write As fFile Close fFile Exit Function ErrOpen: If Err.Number <> 70 Then Msg = "Error # " & Str(Err.Number) & " was generated by " _ & Err.Source & Chr(13) & Err.Description MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext Else IsOpen = True End If End Function
Public Sub subFindProcess(ByVal strProcess As String) Dim strComputer As String Dim objWMIService As Object Dim colProcessList Dim objProcess As ObjectOn Error Resume Nextt strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = '" & strProcess & "' For Each objProcess In colProcessList Public Sub subKillProcess(ByVal strProcess As String) Dim strComputer As String Dim objWMIService As Object Dim colProcessList Dim objProcess As ObjectOn Error Resume Next strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colProcessList = objWMIService.ExecQuery _ ("Select * from Win32_Process Where Name = '" & strProcess & "' For Each objProcess In colProcessList Debug.Print colProcessList Next End Sub Nextt: End Sub
在发这个贴子时,已找到了方法,下面的这种大家看看如何Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate 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 * 1024 End Type Const TH32CS_SNAPPROCESS = &H2 Function IsOpen(hfile As String) As Boolean Dim my As PROCESSENTRY32 Dim l As Long Dim l1 As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If l Then my.dwSize = 1060 If (Process32First(l, my)) Then Do If Left(Trim(my.szExeFile), 10) = Left(hfile, 10) Then IsOpen = True '针对具体文件,文件名是10 个字符就是10 Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False If IsOpen Then GoTo aa End If IsOpen = False aa:
Dim fFile As Integer
fFile = FreeFile()
On Error GoTo ErrOpen
Open sFile For Binary Lock Read Write As fFile
Close fFile
Exit Function
ErrOpen:
If Err.Number <> 70 Then
Msg = "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
Else
IsOpen = True
End If
End Function
Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As ObjectOn Error Resume Nextt
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcess & "'
For Each objProcess In colProcessList
Public Sub subKillProcess(ByVal strProcess As String)
Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As ObjectOn Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcess & "' For Each objProcess In colProcessList
Debug.Print colProcessList
Next
End Sub
Nextt:
End Sub
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate 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 * 1024
End Type
Const TH32CS_SNAPPROCESS = &H2 Function IsOpen(hfile As String) As Boolean
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then
Do
If Left(Trim(my.szExeFile), 10) = Left(hfile, 10) Then IsOpen = True '针对具体文件,文件名是10 个字符就是10
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
If IsOpen Then GoTo aa
End If
IsOpen = False
aa:
l1 = CloseHandle(l)
End If
End Function