vb怎样结束指定进程
要求:在TextBox中输入要结束的进程,比如notepad.exe,点击按钮即结束相应的进程不要用Shelltaskkill,这个我也会请提供完整代码,谢谢...
要求:在TextBox中输入要结束的进程,比如notepad.exe,点击按钮即结束相应的进程
不要用Shell taskkill,这个我也会
请提供完整代码,谢谢 展开
不要用Shell taskkill,这个我也会
请提供完整代码,谢谢 展开
6个回答
展开全部
可以参考下面的两种参考方法:
第一种:用cmd中的taskkill结束程序
shell ”cmd/c taskkill /f /im 你的进程名.exe“
第二种:(WIN7)以下的电脑可以使用,用ntsd结束程序
shell ”cmd/c ntsd -c q -pn 用户的进程名.exe“
扩展资料:
参考语句
For Each...Next 语句: 对于数组或集合中的每一个元素,重复一组语句。
FormatDateTime 函数: 返回格式化为日期或时间的表达式。
FormatNumber 函数: 返回格式化为数的表达式。
Function 语句: 声明形成 Function 过程体的名称、参数和代码。
GetObject 函数: 从文件返回对“自动”对象的访问。
参考资料来源:百度百科-Visual Basic
展开全部
我简单地提供提供三种方法:
------------------------------------------
第一种:用cmd中的taskkill结束程序
shell ”cmd/c taskkill /f /im 你的进程名.exe“
------------------------------------------
第二种:(WIN7)以下的电脑可以使用,用ntsd结束程序
shell ”cmd/c ntsd -c q -pn 你的进程名.exe“
------------------------------------------
第三种:见代码
Private Sub Command1_Click()
On Error Resume Next
Dim s
s = text1.Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & s & "'")
For Each objProcess In colProcessList
objProcess.Terminate'结束进程
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Sub
------------------------------------------
第一种:用cmd中的taskkill结束程序
shell ”cmd/c taskkill /f /im 你的进程名.exe“
------------------------------------------
第二种:(WIN7)以下的电脑可以使用,用ntsd结束程序
shell ”cmd/c ntsd -c q -pn 你的进程名.exe“
------------------------------------------
第三种:见代码
Private Sub Command1_Click()
On Error Resume Next
Dim s
s = text1.Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & s & "'")
For Each objProcess In colProcessList
objProcess.Terminate'结束进程
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试看看这个咋样
用CreateToolhelp32SnapShot创建一个SnapShot句柄,
用Process32First来获取第一个进程,
判断进程的szExeFile是不是你所指定的进程名
用TerminateProcess关闭进程(注意,不能关闭比自己级别高的进程,修改进程级别用AdjustTokenPrivileges)
用Process32Next查看下一个进程,循环.
用CreateToolhelp32SnapShot创建一个SnapShot句柄,
用Process32First来获取第一个进程,
判断进程的szExeFile是不是你所指定的进程名
用TerminateProcess关闭进程(注意,不能关闭比自己级别高的进程,修改进程级别用AdjustTokenPrivileges)
用Process32Next查看下一个进程,循环.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
已经知道某程序的文件名,用VB结束该程序
如果知道该程序在任务管理器中的名字,又如何呢?
用CreateToolhelp32SnapShot创建一个SnapShot句柄,
用Process32First来获取第一个进程,
判断进程的szExeFile是不是你所指定的进程名
用TerminateProcess关闭进程(注意,不能关闭比自己级别高的进程,修改进程级别用AdjustTokenPrivileges)
用Process32Next查看下一个进程,循环.
怎么样,说的够清楚吧
能够举个例子吗?例如我要结束的进程是ABC.exe,级别比自己高的!怎么强制结束这个程序呢?谢谢!
修改进程级别用AdjustTokenPrivileges
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 Sub Command1_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
Dim ret As ListItem
ListView1.ListItems.Clear '清空所有内容
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
Set ret = ListView1.ListItems.Add(, "first" & CStr(i), exename) '将进程名添加到第一项中
ListView1.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID '将进程ID添加到第二项中
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
End Sub
'终止进程
Private Sub Command2_Click()
Dim i As Long
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.SubItems(1))) '获取进程句柄
TerminateProcess hand, 0 '关闭进程
Call Command1_Click '调用查看进程来刷新进程列表
End Sub
Private Sub Form_Load()
Dim header As ColumnHeader
ListView1.View = lvwReport
ListView1.ColumnHeaders.Clear
Set header = ListView1.ColumnHeaders.Add(, "first", "进程", 4000) '设置ListView中项目的宽度,读者也可自行设置
Set header = ListView1.ColumnHeaders.Add(, "second", "ID", 1400)
ListView1.Refresh
End Sub
如果知道该程序在任务管理器中的名字,又如何呢?
用CreateToolhelp32SnapShot创建一个SnapShot句柄,
用Process32First来获取第一个进程,
判断进程的szExeFile是不是你所指定的进程名
用TerminateProcess关闭进程(注意,不能关闭比自己级别高的进程,修改进程级别用AdjustTokenPrivileges)
用Process32Next查看下一个进程,循环.
怎么样,说的够清楚吧
能够举个例子吗?例如我要结束的进程是ABC.exe,级别比自己高的!怎么强制结束这个程序呢?谢谢!
修改进程级别用AdjustTokenPrivileges
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 Sub Command1_Click()
Dim i As Long
Dim proc As PROCESSENTRY32
Dim snap As Long
Dim exename As String
Dim theloop As Long
Dim ret As ListItem
ListView1.ListItems.Clear '清空所有内容
snap = CreateToolhelpSnapshot(TH32CS_SNAPall, 0) '获得进程“快照”的句柄
proc.dwSize = Len(proc)
theloop = ProcessFirst(snap, proc) '获取第一个进程,并得到其返回值
i = 0
While theloop <> 0 '当返回值非零时继续获取下一个进程
exename = proc.szExeFile
Set ret = ListView1.ListItems.Add(, "first" & CStr(i), exename) '将进程名添加到第一项中
ListView1.ListItems("first" & CStr(i)).SubItems(1) = proc.th32ProcessID '将进程ID添加到第二项中
i = i + 1
theloop = ProcessNext(snap, proc)
Wend
CloseHandle snap '关闭进程“快照”句柄
End Sub
'终止进程
Private Sub Command2_Click()
Dim i As Long
Dim hand As Long
hand = OpenProcess(PROCESS_TERMINATE, True, CLng(ListView1.SelectedItem.SubItems(1))) '获取进程句柄
TerminateProcess hand, 0 '关闭进程
Call Command1_Click '调用查看进程来刷新进程列表
End Sub
Private Sub Form_Load()
Dim header As ColumnHeader
ListView1.View = lvwReport
ListView1.ColumnHeaders.Clear
Set header = ListView1.ColumnHeaders.Add(, "first", "进程", 4000) '设置ListView中项目的宽度,读者也可自行设置
Set header = ListView1.ColumnHeaders.Add(, "second", "ID", 1400)
ListView1.Refresh
End Sub
参考资料: http://iask.sina.com.cn/b/10757421.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Command1_Click()
On Error Resume Next
Dim s
s = text1.Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & s & "'")
For Each objProcess In colProcessList
MsgBox "已发现目标!"
objProcess.Terminate'结束进程
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Sub
On Error Resume Next
Dim s
s = text1.Text
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & s & "'")
For Each objProcess In colProcessList
MsgBox "已发现目标!"
objProcess.Terminate'结束进程
Next
Set objProcess = Nothing
Set colProcessList = Nothing
Set objWMIService = Nothing
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询