一个结束进程的API函数的问题
这是第一个小问题:请大家帮帮忙啊.
以上是我自己写的小程序,后来我想删除一个进程就出现了问题.
不知道怎么删除..我都调试看过了变量了啊都返回了进程的句柄了啊.
删除不了进程,不知道问题在哪里
第二个小问题:就是怎么从一个字符串中取出想要的值,,,比如有一个这样的字符串 "520 中间是有3个空格 QQ.exe"
520是进程的PID值 QQ.exe 是进程文件名
我想取的就是数字,也就是进程PID的值..因为有时候进程PID的值有时候是4位数字的..在不知道的情况下我不知道怎么准确的取这个进程PID值》》
第三个小问题:暂时就这么多了.请大家帮帮忙啊.祝大家天天开心.工作顺利......哈哈 别笑我写的乱啊..
(今天才知道怎么结贴..-.-!)(以前只知道发帖的)
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32.dll" (ByVal dwFlags As Long, ByVal
th32ProcessID As Long) As Long '这个API返回的是长整型值,就是快照系统的后的句柄 ,,,最后一个参数0表示快照当前所有的进程
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 OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Const PROCESS_QUERY_INFORMATION = &H400
Const PROCESS_VM_READ = &H10
Const TH32CS_SNAPPROCESS = &H2
Private Type PROCESSENTRY32
dwSize As Long
cntUseage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
swFlags As Long
szExeFile As String * 1024
End Type
Dim QQStr As String
Dim Jhwnd As String
Private Sub Command1_Click() '判断QQ进程是否存在
List1.Clear
Call Form_Load
End Sub
Private Sub Command2_Click() '删除进程的按钮
Dim ProcessHwnd As Long
Dim Ret As Long
Jhwnd = List1.List(List1.ListIndex)
Jhwnd = Trim(Left(Jhwnd, 3))
ProcessHwnd = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, False, Jhwnd)
If ProcessHwnd <> 0 Then
Ret = TerminateProcess(ByVal ProcessHwnd, 0&)
If Ret <> 0 Then
MsgBox "结束进程成功", vbInformation, "提示信息"
Else
MsgBox "结束进程失败", vbCritical, "错误"
End If
End If
End Sub
Private Sub Form_Load()
Dim Mysnapshot As Long
Mysnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If Mysnapshot <> 0 Then
Dim processinfor As PROCESSENTRY32
processinfor.dwSize = Len(processinfor)
If Process32First(Mysnapshot, processinfor) <> 0 Then
List1.AddItem "第一个进程PID值是" & processinfor.th32ProcessID
List1.AddItem "第一个进程文件名是" & processinfor.szExeFile
While Process32Next(Mysnapshot, processinfor) <> 0
List1.AddItem processinfor.th32ProcessID & " " & processinfor.szExeFile
Wend
End If
Dim i As Integer
'为什么会返回空? MsgBox List1.Text
For i = 1 To List1.ListCount
QQStr = List1.List(i)
If InStr(QQStr, "QQ.exe") Then
MsgBox "QQ.exe进程存在", vbInformation, "提示"
Exit For
End If
Next
If Right(QQStr, 6) <> "QQ.exe" Then
MsgBox "QQ.exe进程不存在", vbInformation, "提示"
End If
Label1.Caption = "进程数: " & List1.ListCount
End If
End Sub
[解决办法]
TerminateProcess这个都没用 怎么结束
------解决方案--------------------
找到进程的PID,直接用Taskkill来处理
[解决办法]
processinfor.th32ProcessID就是你要的数字