要在进程A中终止进程B
进程A采用shell启动
代码如下:
Private Sub Command1_Click()
jc = Shell("C:\Program Files\WinRAR\WinRAR.exe", 1)
End SubPrivate Sub Command3_Click()
Dim hProcess  As Long
hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, jc)
re = TerminateProcess(hProcess, 3838)
End Sub
但TerminateProcess总是返回0,说明执行失败。请问为什么?????????

解决方案 »

  1.   

    你的jc应该定义为全局变量
    另外,如果在你点击Command3前你以关闭WinRAR.exe,则也回返回0
      

  2.   

    我试了一下,没有问题,你是不是jc没有定义为全局变量:Private Const PROCESS_TERMINATE = &H1
    Private Const PROCESS_QUERY_INFORMATION = &H400
    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 LongDim jc As LongPrivate Sub Command1_Click()
        jc = Shell("C:\Program Files\WinRAR\WinRAR.exe", 1)
    End SubPrivate Sub Command3_Click()
        Dim hProcess  As Long
        
        hProcess = OpenProcess(PROCESS_TERMINATE Or PROCESS_QUERY_INFORMATION, False, jc)
        re = TerminateProcess(hProcess, 3838)
    End Sub
      

  3.   

    以“PROCESS”打头的几个常量的值有谁能提供一下马
      

  4.   

    Dim hProcess As Long
    '调用Api关闭Excel进程
    '---------- API类型声明   -----------
    Public 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 TypePublic Type MODULEENTRY32                  '模块
        dwsize As Long
        th32ModuleID As Long
        th32ProcessID As Long
        GlblcntUsage As Long
        ProccntUsage As Long
        modBaseAddr As Byte
        modBaseSize As Long
        hModule As Long
        szModule As String * 256
        szExePath As String * 1024
    End TypePublic Type THREADENTRY32                  '线程
        dwsize As Long
        cntusage As Long
        th32threadID As Long
        th32OwnerProcessID As Long
        tpBasePri As Long
        tpDeltaPri As Long
        dwFlags As Long
    End Type
    '----------------------------------------- API声明 --------------------------------------
    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long   '取得快照
    Public Declare Function Module32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long                  '遍历所需要的API函数
    Public Declare Function Module32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As MODULEENTRY32) As Long
    Public Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Public Declare Function Thread32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
    Public Declare Function Thread32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As THREADENTRY32) As Long
    Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Public Declare Function GetCurrentProcessId Lib "kernel32" () As Long'---------------------------------------- API常数声明 -----------------------------------Public Const TH32CS_SNAPHEAPLIST = &H1
    Public Const TH32CS_SNAPPROCESS = &H2
    Public Const TH32CS_SNAPTHREAD = &H4
    Public Const TH32CS_SNAPMODULE = &H8
    Public Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
    Public Const TH32CS_INHERIT = &H80000000
    Public Const PROCESS_TERMINATE = &H1&
    ---------------------------------------------------------------------------------------
    '----------------------此过程用来将进程中的EXcel退出-----------------
    Sub DetectExcel()
      Dim Process As PROCESSENTRY32
      Dim ProcSnap As Long
      Dim chj$
      hProcess = 0
      ProcSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
      If ProcSnap Then
        Process.dwsize = 1060                                        ' 通常用法
        Process32First ProcSnap, Process
        Do Until Process32Next(ProcSnap, Process) < 1     ' 遍历所有进程直到返回值为False
           chj = UCase(Trim(Process.szExeFile))
           If InStr(1, chj, "EXCEL") <> 0 Then
              hProcess = OpenProcess(PROCESS_TERMINATE, False, Process.th32ProcessID)
              If hProcess Then TerminateProcess hProcess, 0
           End If
        Loop
      End If
      While hProcess
        DetectExcel
      Wend
      CloseHandle (ProcSnap)
    End Sub
    这个是我用来关闭进程中"Excel.exe"进程的,实现的过程是先得到进程的快照,再遍历快照找出与"Excel"字符匹配的进程并关闭之.其中包括API声明和常数声明
      

  5.   

    你的系统是不是nt/2000/xp??????
    如果要在上述系统里使用TerminateProcess要先提升权限。。
      

  6.   

    一个例子:'模块中:
    Option Explicit
    Private Type LUID
       lowpart As Long
       highpart As Long
    End Type
    Private Type TOKEN_PRIVILEGES
        PrivilegeCount As Long
        LuidUDT As LUID
        Attributes As Long
    End TypeConst TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
    Const PROCESS_ALL_ACCESS = &H1F0FFFPrivate Declare Function GetVersion _
        Lib "kernel32" () As Long
    Private Declare Function GetCurrentProcess _
        Lib "kernel32" () As Long
    Private Declare Function CloseHandle _
        Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function OpenProcessToken _
        Lib "advapi32" (ByVal ProcessHandle As Long, _
        ByVal DesiredAccess As Long, _
        TokenHandle As Long) As Long
    Private Declare Function LookupPrivilegeValue _
        Lib "advapi32" Alias "LookupPrivilegeValueA" _
        (ByVal lpSystemName As String, _
        ByVal lpName As String, _
        lpLuid As LUID) As Long
    Private Declare Function AdjustTokenPrivileges _
        Lib "advapi32" (ByVal TokenHandle As Long, _
        ByVal DisableAllPrivileges As Long, _
        NewState As TOKEN_PRIVILEGES, _
        ByVal BufferLength As Long, _
        PreviousState As Any, ReturnLength As Any) 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 LongPublic Function KillProcess(ByVal hProcessID As Long, Optional ByVal ExitCode As Long) As Boolean
        Dim hToken As Long
        Dim hProcess As Long
        Dim tp As TOKEN_PRIVILEGES    If GetVersion() >= 0 Then
         
            If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) = 0 Then
                GoTo CleanUp
            End If
            
            If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
                GoTo CleanUp
            End If
        
            tp.PrivilegeCount = 1
            tp.Attributes = SE_PRIVILEGE_ENABLED        If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, ByVal 0&) = 0 Then
                GoTo CleanUp
            End If
        End If
        
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
        If hProcess Then
            
            KillProcess = (TerminateProcess(hProcess, ExitCode) <> 0)
            ' close the process handle
            CloseHandle hProcess
        End If
        
        If GetVersion() >= 0 Then
            ' under NT restore original privileges
            tp.Attributes = 0
            AdjustTokenPrivileges hToken, False, tp, 0, ByVal 0&, ByVal 0&
            
    CleanUp:
            If hToken Then CloseHandle hToken
        End If
    End Function'程序中:
    Dim pID As Long
    Private Sub Command1_Click()
    pID = Shell("Notepad.Exe", vbNormalFocus)
    End SubPrivate Sub Command2_Click()
    If KillProcess(pID, 0) Then
        MsgBox "Notepad was terminated"
    End If
    End Sub