VB结束进程
    用VB结束进程一般有两种办法,可以结束进程,也可以结束一个窗体。
    结束进程:
    最简单的办法就是用sheel调用taskkill,用法为:sheel"taskkill /f /im 程序.exe"。可以结束一些一般的程序。
    还可以用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 * 260
End Type
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 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 Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Const TH32CS_SNAPPROCESS = &H2&
'关闭指定名称的进程
Private Sub KillProcess(sProcess As String)
    Dim lSnapShot As Long
    Dim lNextProcess As Long
    Dim tPE As PROCESSENTRY32
    lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
    If lSnapShot <> -1 Then
        tPE.dwSize = Len(tPE)
vb计算器代码大全
        lNextProcess = Process32First(lSnapShot, tPE)
        Do While lNextProcess
            If LCase$(sProcess) = LCase$(Left(tPE.szExeFile, InStr(1, tPE.szExeFile, Chr(0)) - 1)) Then
                Dim lProcess As Long
                Dim lExitCode As Long
                lProcess = OpenProcess(1, False, tPE.th32ProcessID)
                TerminateProcess lProcess, lExitCode
                CloseHandle lProcess
            End If
            lNextProcess = Process32Next(lSnapShot, tPE)
        Loop
        CloseHandle (lSnapShot)
    End If
End Sub
Private Sub command1_click()
    Call KillProcess("程序.exe") '结束"程序.exe"进程
End Sub
    用结束进程的方法对有一些程序没办法结束,比如一些大型游戏或其它全屏的程序。这时可以试试结束窗体。
  结束窗体:
  先获取程序的窗口句柄,然后向该窗体发送结束的消息,一般可以将上述无法关闭的程序给结束掉。注意这里要注意用的是窗口句柄而不是程序进程句柄,比如计算器程序的窗口句柄是"计算器",而程序进程句柄是""。在任务管理器里,应用程序->任务下显示的是窗口句柄,而进程->映像名称下显示的就是进程句柄。
  例如要结束计算器程序,可以用如下代码:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10
Private Sub Command1_Click()
Dim hProcess As Long
hProcess = FindWindow(vbNullString, "计算器")
SendMessage hProcess, WM_CLOSE, 0, 0
End Sub
    这些方法对付一般程序应该差不多了,但要想结束像杀毒程序之类的,还是无能为力,还要想其它的办法。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。