在程序A中结束程序B的进程.在98下调试成功,在XP下就不行了
这是为什么呢?以下是代码:
ProcessHand = OpenProcess(0, True, lngDec)
Call TerminateProcess(ProcessHand, 0)

解决方案 »

  1.   

    Dim mProcID As Long
                 mProcID = OpenProcess(1&, -1&, pid)
                TerminateProcess mProcID, 0&
      

  2.   

    书上说TerminateProcess不适用于nt/2000/xp,但在csdn上也听人说过在nt/2000/xp里也能用TerminateProcess,但要先取得权限。但不知是不是好像关机时那样取得权限。
    没有试过
      

  3.   

    嗯,不过如果是admin应该可以
      

  4.   

    先提高你的权限,也就是取得管理员的权限
    在你OpenProcess时你的安全描述符也要重新指定!
    给你段C++的代码,我是搞VC的!
    先取得在2000下权限 HANDLE hToken;  
    TOKEN_PRIVILEGES tkp;  

    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
    MessageBox("OpenProcessToken failed!");

    LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);  

    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);  

    if (GetLastError() != ERROR_SUCCESS)  
    MessageBox("AdjustTokenPrivileges enable failed!");

    if (!ExitWindowsEx(EWX_REBOOT, 0))  
    MessageBox("ExitWindowsEx failed!");
      

  5.   

    我可以帮你翻译成VB的代码,上面是是调用EXITWINDOW的例子,本质是一样的!
      

  6.   

    参考:
    http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q185/2/15.ASP&NoWebContent=1
      

  7.   

    试出来了,下面是我上面那个连接的简化例子:'模块中:
     Option Explicit
          Public Const TOKEN_ADJUST_PRIVILEGES = &H20
          Public Const TOKEN_QUERY = &H8
          Public Const ANYSIZE_ARRAY = 1
          Public Const PROCESS_ALL_ACCESS = &H1F0FFF
          Public Const SE_DEBUG_NAME = "SeDebugPrivilege"
          Public Const SE_PRIVILEGE_ENABLED = &H2
          Type LARGE_INTEGER
             lowpart As Long
             highpart As Long
          End Type      Type Luid
             lowpart As Long
             highpart As Long
          End Type      Type LUID_AND_ATTRIBUTES
             pLuid As Luid
             Attributes As Long
          End Type      Type TOKEN_PRIVILEGES
             PrivilegeCount As Long
             Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
          End Type
          Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
          Declare Function GetCurrentProcess Lib "kernel32" () As Long
          Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
          Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As Luid) As Long
          Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
          Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
          Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
          Public Sub SeDebugSample(ApplicationPID As Long)
             Dim hProcessID As Long
             Dim hProcess As Long
             Dim hToken As Long
             Dim lPrivilege As Long
             Dim iPrivilegeflag As Boolean
             Dim lResult As Long
             hProcessID = ApplicationPID
             hProcess = GetCurrentProcess
             lResult = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
             lResult = SetPrivilege(hToken, SE_DEBUG_NAME, True)
             hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
             lResult = SetPrivilege(hToken, SE_DEBUG_NAME, False)
             lResult = TerminateProcess(hProcess, 0)
             CloseHandle (hProcess)
             CloseHandle (hToken)
          End Sub
          Private Function SetPrivilege(hToken As Long, Privilege As String, bSetFlag As Boolean) As Boolean
             Dim TP As TOKEN_PRIVILEGES
             Dim TPPrevious As TOKEN_PRIVILEGES
             Dim Luid As Luid
             Dim cbPrevious As Long
             Dim lResult As Long
             cbPrevious = Len(TP)
             lResult = LookupPrivilegeValue("", Privilege, Luid)
             If (lResult = 0) Then
                SetPrivilege = False
             End If
                TP.PrivilegeCount = 1
                TP.Privileges(0).pLuid = Luid
                TP.Privileges(0).Attributes = 0
                SetPrivilege = lResult
                lResult = AdjustTokenPrivileges(hToken, -1, TP, Len(TP), TPPrevious, cbPrevious)
             If (lResult = 0) Then
                SetPrivilege = False
             End If
             TPPrevious.PrivilegeCount = 1
             TPPrevious.Privileges(0).pLuid = Luid
             Select Case bSetFlag
                Case True: TPPrevious.Privileges(0).Attributes = TPPrevious.Privileges(0).Attributes Or (SE_PRIVILEGE_ENABLED)
                Case False: TPPrevious.Privileges(0).Attributes = TPPrevious.Privileges(0).Attributes Xor (SE_PRIVILEGE_ENABLED And TPPrevious.Privileges(0).Attributes)
             End Select
             lResult = AdjustTokenPrivileges(hToken, -1, TPPrevious, cbPrevious, TP, cbPrevious)
             If (lResult = 0) Then
                SetPrivilege = False
             Else
                SetPrivilege = True
             End If
          End Function'程序中:
    Private iAppPID As Long
    Private Sub Command1_Click()
       iAppPID = Shell("Notepad.exe", vbNormalFocus)
    End Sub
     Private Sub Command2_Click()
       SeDebugSample CLng(iAppPID)
     End Sub
    按command1启动notepad,按command2结束。
      

  8.   

    ProcessHand = OpenProcess(0, True, lngDec)
    Call TerminateProcess(ProcessHand, byval 0&)注意要加个byval
      

  9.   

    多谢了
    顺便问一下,XP下EnumProcessModules返回的进程句柄是多少位的?
    我在98下得到的是8位16进制,而在XP下只得到2位的
      

  10.   

    本质就是提升权限至SeDebugPrivilege
      

  11.   

    上面的代码好像有些问题呀
    大家能帮我看看是哪个函数出的问题吗hProcessID = ApplicationPID------->hProcessID=208(十进制)
    hProcess = GetCurrentProcess-------->-1
    lResult = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)-------->1
    lResult = SetPrivilege(hToken, SE_DEBUG_NAME, True)-------->-1
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)-------->0(这个肯定有问题)
    lResult = SetPrivilege(hToken, SE_DEBUG_NAME, False)-------->-1
    lResult = TerminateProcess(hProcess, ByVal 0&)-------->0(这个也有问题)(箭头指向的是返回值)多谢了!
      

  12.   

    上面的代码好像有些问题呀
    大家能帮我看看是哪个函数出的问题吗hProcessID = ApplicationPID------->hProcessID=208(十进制)
    hProcess = GetCurrentProcess-------->-1
    lResult = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)-------->1
    lResult = SetPrivilege(hToken, SE_DEBUG_NAME, True)-------->-1
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)-------->0(这个肯定有问题)
    lResult = SetPrivilege(hToken, SE_DEBUG_NAME, False)-------->-1
    lResult = TerminateProcess(hProcess, ByVal 0&)-------->0(这个也有问题)(箭头指向的是返回值)多谢了!
    ======================
    我这里没有你说的问题:hProcessID = ApplicationPID
             Debug.Print "hProcessID=" & Str(hProcessID)
             hProcess = GetCurrentProcess
             Debug.Print " hProcess=" & Str(hProcess)
             lResult = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
             Debug.Print " lResult=" & Str(lResult)
             lResult = SetPrivilege(hToken, SE_DEBUG_NAME, True)
             Debug.Print " lResult=" & Str(lResult)
             hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
             Debug.Print " hProcess=" & Str(hProcess)
             lResult = SetPrivilege(hToken, SE_DEBUG_NAME, False)
             Debug.Print " lResult=" & Str(lResult)
             lResult = TerminateProcess(hProcess, 0)
             Debug.Print " lResult=" & Str(lResult)
    ==============>
    hProcessID= 272
     hProcess=-1
     lResult= 1
     lResult=-1
     hProcess= 956
     lResult=-1
     lResult= 1vb6+sp5+xp下通过,启动和终止notpad...你要终止的是什么进程????不会是系统进程吧。
      

  13.   

    终于找到原因了
    我枚举了所有进程,关闭了一个我创建的程序,结果把那个进程的Handle给当作PID用了再次感谢!