鎴戝彲浠ヨ疆寰殑鍒楀嚭绯荤粺鐨勬墍鏈夎繘绋嬨€備絾鏄湪鎴戝湪鍒ゆ柇绯荤粺涓槸鍚﹀瓨鍦ㄥ凡鐭ヨ繘绋嬬殑鏃跺€欙紝鍗存壘涓嶅埌鍚屽凡鐭ヨ繘绋嬬浉鍚岀殑杩涚▼銆?
渚嬪锛欼f LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If鎻愮ず妗嗕腑鍙互鏄剧ず姝e湪杩愯鐨剋ord杩涚▼銆備絾鏄缁堜笉鏄剧ず'ok'銆?
涓€涓嬫槸鎴戠殑鍘熺爜锛?
'脤铆录脫脠媒赂枚CommandButton潞脥脪禄赂枚ListBoxOption Explicit
Dim ProcessID() As Long ' 掳麓list1脰脨碌脛陆酶鲁脤脣鲁脨貌麓忙麓垄脣霉脫脨陆酶鲁脤ID'---------- API脌脿脨脥脡霉脙梅 -----------
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 * 1024
End TypePrivate Type MODULEENTRY32 '脛拢驴茅
dwsize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End TypePrivate Type THREADENTRY32 '脧脽鲁脤
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type'----------------------------------------- API脡霉脙梅 -------------------------------------------------------
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API鲁拢脢媒脡霉脙梅 ------------------------------------------------------
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "ok!", vbOKOnly + vbInformation, "message"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Form_Load
Wend
End If
End SubPrivate Sub Command3_Click()
Unload Me
End SubPrivate Sub Command4_Click()
Form_Load
End SubPrivate Sub Form_Load()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 '
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 茅脌煤脣霉脫脨陆酶鲁脤脰卤碌陆路碌禄脴脰碌脦陋False
List1.AddItem Trim(Process.szExeFile)
If LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 茅脌煤脣霉脫脨陆酶鲁脤脰卤碌陆路碌禄脴脰碌脦陋False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub
渚嬪锛欼f LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If鎻愮ず妗嗕腑鍙互鏄剧ず姝e湪杩愯鐨剋ord杩涚▼銆備絾鏄缁堜笉鏄剧ず'ok'銆?
涓€涓嬫槸鎴戠殑鍘熺爜锛?
'脤铆录脫脠媒赂枚CommandButton潞脥脪禄赂枚ListBoxOption Explicit
Dim ProcessID() As Long ' 掳麓list1脰脨碌脛陆酶鲁脤脣鲁脨貌麓忙麓垄脣霉脫脨陆酶鲁脤ID'---------- API脌脿脨脥脡霉脙梅 -----------
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 * 1024
End TypePrivate Type MODULEENTRY32 '脛拢驴茅
dwsize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End TypePrivate Type THREADENTRY32 '脧脽鲁脤
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type'----------------------------------------- API脡霉脙梅 -------------------------------------------------------
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API鲁拢脢媒脡霉脙梅 ------------------------------------------------------
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "ok!", vbOKOnly + vbInformation, "message"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Form_Load
Wend
End If
End SubPrivate Sub Command3_Click()
Unload Me
End SubPrivate Sub Command4_Click()
Form_Load
End SubPrivate Sub Form_Load()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 '
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 茅脌煤脣霉脫脨陆酶鲁脤脰卤碌陆路碌禄脴脰碌脦陋False
List1.AddItem Trim(Process.szExeFile)
If LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' 茅脌煤脣霉脫脨陆酶鲁脤脰卤碌陆路碌禄脴脰碌脦陋False
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub
乱码被我破译了$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$我可以轮循的列出系统的所有进程。但是在我在判断系统中是否存在已知进程的时候,却找不到同已知进程相同的进程?
例如:If LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If提示框中可以显示正在运行的word进程。但是始终不显示'ok'?
一下是我的原码?
'ìí?óèy??CommandButtonoíò???ListBoxOption Explicit
Dim ProcessID() As Long ' °′list1?Dμ???3ì?3Dò′?′¢?ùóD??3ìID'---------- APIààDíéù?÷ -----------
Private Type PROCESSENTRY32 '??3ì
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 TypePrivate Type MODULEENTRY32 '?£?é
dwsize As Long
th32ModuleID As Long
th32ProcessID As Long
GlblcntUsage As Long
ProccntUsage As Long
modBaseAddr As Byte
modBaseSize As Long
hModule As Long
szModule As String * 256
szExePath As String * 1024
End TypePrivate Type THREADENTRY32 '??3ì
dwsize As Long
cntusage As Long
th32threadID As Long
th32OwnerProcessID As Long
tpBasePri As Long
tpDeltaPri As Long
dwFlags As Long
End Type'----------------------------------------- APIéù?÷ -------------------------------------------------------
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API3£êyéù?÷ ------------------------------------------------------
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const PROCESS_TERMINATE = &H1&Private Sub Command2_Click()
Dim c As Integer
If List1.ListIndex < 0 Then
MsgBox "ok!", vbOKOnly + vbInformation, "message"
Else
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_TERMINATE, False, ProcessID(List1.ListIndex))
If hProcess Then TerminateProcess hProcess, 0
c = List1.ListCount
While List1.ListCount = c
Form_Load
Wend
End If
End SubPrivate Sub Command3_Click()
Unload Me
End SubPrivate Sub Command4_Click()
Form_Load
End SubPrivate Sub Form_Load()
Dim Process As PROCESSENTRY32
Dim ProcSnap As Long
Dim cntProcess As Long
cntProcess = 0
List1.Clear
ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If ProcSnap Then
Process.dwsize = 1060 '
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' éàú?ùóD??3ì?±μ?·μ???μ?aFalse
List1.AddItem Trim(Process.szExeFile)
If LCase(Trim(Process.szExeFile)) = "winword.exe" Then
MsgBox "ok"
Else
MsgBox Trim(Process.szExeFile), vbOKOnly
End If
cntProcess = cntProcess + 1
Loop
End If
ReDim ProcessID(cntProcess) As Long
Dim i As Long
i = 0
Process32First ProcSnap, Process
Do Until Process32Next(ProcSnap, Process) < 1 ' éàú?ùóD??3ì?±μ?·μ???μ?aFalse
ProcessID(i) = Process.th32ProcessID
i = i + 1
Loop
CloseHandle (ProcSnap)
End Sub
是以\0为字符串终结符的
用Trim去掉空格是无效的给你一个去掉\0的函数:
function Trim0(szText as String)as string
dim iPos as long
iPos = InStr(1, szText, vbNullChar, vbBinaryCompare)
if iPos<>0 then
Trim0 = Left$(szText, iPos-1)
else
Trim0 = szText
end if
end function