永发信息网

vb列举进程代码

答案:1  悬赏:70  手机版
解决时间 2021-04-24 14:14
要有可以结束进程的代码
最佳答案


添加如图控件


Command1为kill process键


Command2为刷新键


其余的为系统默认名称


form1代码:


Option Explicit
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_TERMINATE = &H1


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 Function RefreshProcess()
On Error Resume Next
List1.Clear
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
List1.AddItem exename
theloop = ProcessNext(snap, proc)
Wend
End Function
Public Sub CloseProcess(ProName As String) '关闭指定名称的程序 例qq.exe
On Error Resume Next
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename, killname As String
Dim theloop As Long
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
killname = ProName & "*"
If LCase(exename) Like LCase(killname) Then
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, proc.th32ProcessID)
TerminateProcess hand, 0 '关闭进程
'MsgBox "系统已经强制结束了" & ProName & "程序!", vbExclamation, "系统提示"
End If
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
RefreshProcess
End Sub


Private Sub Command1_Click()
CloseProcess Text1.Text
End Sub


Private Sub Command2_Click()
RefreshProcess
End Sub


Private Sub Form_Load()
RefreshProcess
Dim ret As Long
preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, AddressOf Wndproc)
idHotKey = 1
Modifiers = MOD_ALT + MOD_CONTROL
uVirtKey = vbKeyX
ret = RegisterHotKey(Me.hwnd, idHotKey, Modifiers, uVirtKey)
End Sub



Private Sub Form_Unload(Cancel As Integer)
Dim ret As Long
ret = SetWindowLong(Me.hwnd, GWL_WNDPROC, preWinProc)
Call UnregisterHotKey(Me.hwnd, uVirtKey)
End Sub



Private Sub Form_Resize()
On Error Resume Next
'Label1.Left = 60
'Label1.Width = 1155
Command1.Left = Form1.Width - Command1.Width - 140
'Command1.Top = 0
'Text1.Top = 0
Text1.Width = Form1.Width - 2490
List1.Width = Form1.Width - 120
List1.Height = Form1.Height - List1.Top - 825
Command2.Top = List1.Top + List1.Height + 10
Command2.Left = Form1.Width - 2000
End Sub


Private Sub List1_Click()
Text1.Text = List1.Text
End Sub
Module1代码:


Option Explicit


Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function RegisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
Declare Function UnregisterHotKey Lib "User32" (ByVal hwnd As Long, ByVal id As Long) As Long
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long



Public Const WM_HOTKEY = &H312
Public Const MOD_ALT = &H1
Public Const MOD_CONTROL = &H2
Public Const MOD_SHIFT = &H4
Public Const GWL_WNDPROC = (-4)
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPheaplist = &H1
Private Const TH32CS_SNAPthread = &H4
Private Const TH32CS_SNAPmodule = &H8
Private Const TH32CS_SNAPall = TH32CS_SNAPPROCESS + TH32CS_SNAPheaplist + TH32CS_SNAPthread + TH32CS_SNAPmodule
Private Const MAX_PATH As Integer = 260
Private Const PROCESS_TERMINATE = &H1


Public preWinProc As Long
Public Modifiers As Long, uVirtKey As Long, idHotKey As Long



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 Type taLong
ll As Long
End Type


Private Type t2Int
lWord As Integer
hWord As Integer
End Type



Private Function RefreshProcess()
On Error Resume Next
Form1.List1.Clear
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
Form1.List1.AddItem exename
theloop = ProcessNext(snap, proc)
Wend
End Function
Public Sub CloseProcess(ProName As String) '关闭指定名称的程序 例qq.exe
On Error Resume Next
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename, killname As String
Dim theloop As Long
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
killname = ProName & "*"
If LCase(exename) Like LCase(killname) Then
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, proc.th32ProcessID)
TerminateProcess hand, 0 '关闭进程
'MsgBox "系统已经强制结束了" & ProName & "程序!", vbExclamation, "系统提示"
End If
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
RefreshProcess
End Sub



Public Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then
CloseProcess "qq.exe"
End If
End If
End If
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function

我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
口袋妖怪白金在哪里下载?
QQ校友退出班级了,可农场里还有他们的好友,
冒牌太阳能
河流形成的原因,什么是大坝截流 大坝截流是为
帮忙写求爱情诗.谢谢
求教,谢谢各位了
滴露消毒药水可以杀真菌吗?
七夕送周洁一首藏头诗
怎么样减肥最有效
目前哪种挖掘机最好!
为什么我的电脑用09版QQ不能跟手机QQ用户发图
财政分局乔迁之喜贺词,乔迁之喜贺词?
拿渡地址在哪,我要去那里办事
怎么打开好123,网页自动就转到http://www.vol
什么叫喜欢?什么叫爱?喜欢和爱有什么区别吗?
推荐资讯
22岁男士混合型皮肤用什么护肤品好
唉 开学了 以后咋还能像这样 玩
海贼王,银魂,死神,火影,家教给个客观的排
啤酒喝多了会怎么样
QQ三国职业之豪杰
兑换是什么,各位喜欢黑方兑什么饮料?
小英的妈妈是年过三十快四十的人了,但到2008
可以用怎样的事物形容文科与理科
鼎鑫置业在什么地方啊,我要过去处理事情
男生为什么那么花心?
谁有些好听的歌推荐下啊?
我玩游戏CPU飙到过100%该怎么解决啊
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?