怎样用vb检测正在运行的程序并将它们关闭

我想实现一个功能,就是检测正在运行的程序,将其关闭,然后关机,清高数我应该怎样写代码。谢谢
不是指定程序,是所有正在运行的程序

Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) 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 CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) 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 * 1024
End Type
Const TH32CS_SNAPHEAPLIST = &H1
Const TH32CS_SNAPPROCESS = &H2
Const TH32CS_SNAPTHREAD = &H4
Const TH32CS_SNAPMODULE = &H8
Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Const TH32CS_INHERIT = &H80000000
Dim pid As Long
Dim pname As String
Const sEndProess As String = "qq.exe" '进程名

Private Sub Timer2_Timer()
Dim my As PROCESSENTRY32
Dim l As Long
Dim l1 As Long
Dim flag As Boolean
Dim mName As String
Dim i As Integer
l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If l Then
my.dwSize = 1060
If (Process32First(l, my)) Then '遍历第一个进程
Do
i = InStr(1, my.szExeFile, Chr(0)) '获得映像名称
mName = LCase(Left(my.szExeFile, i - 1)) '并转换成小写
If mName = LCase(sEndProess) Then '是不是指定的程序
pid = my.th32ProcessID '获得进程ID
pname = mName
Dim mProcID As Long
mProcID = OpenProcess(1&, -1&, pid)
TerminateProcess mProcID, 0&
flag = True
Exit Sub
Else
flag = False
End If
Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
End If
l1 = CloseHandle(l)
End If
End Sub
温馨提示:内容为网友见解,仅供参考
第1个回答  2009-03-26
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function PostMessage Lib "user32" Alias "PostMessageA"(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

例如要检测“计算器”程序是否正在运行,如果是则关闭它,可以使用如下代码来实现:

Dim winHwnd As Long

Dim RetVal As Long

winHwnd=FindWindow(vbNullString,"计算器")

If winHwnd〈〉0 Then

RetVal = PostMessage(winHwnd, &&H10, 0&&, 0&&)

If RetVal = 0 Then

MsgBox "关闭计算器出错!"

End If

Else

MsgBox "计算器程序没有运行。"

End If

怎样用vb检测正在运行的程序并将它们关闭
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib...

如何在VB中关闭正在运行的应用程序
先打开任务管理器Ctri+Ait+dei三个键同时按。打开后找应用程序。在找到你不用的程序。结束进程。如果都想结束。你可以点用户注销用户。

用VB写个(强制)结束进程程序
回答:使用下面的代码可以强制结束进程,如果还不行,可以追加问。GC.SuppressFinalize(get.exe)

怎么用VB做关掉进程的软件?
关闭正在运行的程序 你可以使用API函数FindWindow和PostMessage去寻找指定的窗口,并关闭它。下面的例子教给你怎样找到并关掉一个Caption为“Caluclator”的程序。Dim winHwnd As Long Dim RetVal As Long winHwnd = FindWindow(vbNullString, "Calculator ")Debug.Print w...

怎样才能用VB.NET的代码来关闭一个在运行的程序
软糖来回答罗:通过System.Diagnostics命名空间下的Process类来关闭程序的进程 Dim 进程集合 = Process.GetProcessesByName("进程名称") For Each 进程 In 进程集合 进程.Kill() '进程.Close() '或者使用关闭 Next也可以先获取所有进程,再来判断这些进程的名称ProcessName Dim 获取本地所有进...

(VB)如何检查自身程序已经运行?
用DDE实现窗体防止运行多个实例并传递命令 上网的朋友一定都用过网络蚂蚁(Net Ants)的吧?不知你在使用过程中有没有注意过,那就是如果你想调动两个“蚂蚁”为您效力是不可能的——它总会把新运行的关闭。这点在VB中很容易实现:Private Sub Form_Load()If App.PrevInstance Then MsgBox "你已经...

VB怎样关闭一个正在运行的程序
PrivateSubCommand1_Click()'首先通过VB的菜单[工程]--[引用],勾选:MicrosoftExcel11.0ObjectLibrary'其中11.0是不一定的,要看你计算机安装的OFFICE版本而定DimxlsAppAsExcel.Application'声明对象变量SetxlsApp=Excel.Application'实例化对象xlsApp.Visible=False'使Excel隐藏不可见xlsApp.Workbooks.Open(...

...某个程序在运行,将该程序的代码关掉,这个程序怎么编?
ALL_ACCESS, False, pid) '获取进程句柄 If hProcess = 0 Then Exit Sub If GetExitCodeProcess(hProcess, lpExitCode) = 0 Then Call CloseHandle(hProcess)Exit Sub End If Call TerminateProcess(hProcess, lpExitCode) '终止进程(关闭窗口)Call CloseHandle(hProcess)End If End Sub ...

用VB如何关闭一个正在运行的进程,方法一定要可行
一般都是调用Winodws API函数TerminateProcess()来杀进程,相关的有进程快照、寻找进程、结束进程、处理进程托盘图标等。细节可以去百度,网上有不少。只例举TerminateProcess()函数的vb声明,其它自己去百度吧。Private Declare Function TerminateProcess Lib "Kernel32" (ByVal ApphProcess As Long, ByVal u...

如何用vb关闭一个应用程序
你可以使用API函数FindWindow和PostMessage来寻找一个窗口并且关闭它。下面的范例演示如何关闭一个标题为"Calculator"的窗口。'下面的代码放到模块中 Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 'FindWindowAPI...

相似回答