如果我想做一段结束指定进程的代码,能同时在win98和win2000 XP这样的系统下运行。我该怎么做。反正98下没什么问题已经搞定,但XP下就麻烦大了,好像与系统操作的权限有关。
我对NT内核的系统很陌生感,见过“关机”的程序在这两种系统下的代码就截然不同。求高手指点一二。

解决方案 »

  1.   

    需要进程有 PROCESS_TERMINATE 权限。
      

  2.   

    Under Win32, the operating system promises to clean up resources owned by a process when it shuts down. This does not, however, mean that the process itself has had the opportunity to do any final flushes of information to disk, any final communication over a remote connection, nor does it mean that the process' DLL's will have the opportunity to execute their PROCESS_DETACH code. This is why it is generally preferable to avoid terminating an application under Windows 95 and Windows NT.
      

  3.   

    唉最头疼E文了,翻译了一下,错误难免还望大家指正。
    但这文章内容中的意思,咋不大明白呢? 译文如下:
    在Win32系统下,操作系统有约定在一个进程结束时自动的回收它所占用的资源。这没什么问题,然而,这意味着进程本身有机会将最后的数据流信息传至磁盘,最后的消息传递来终结远程的连接,还有就是意味着进程的DLL库有机会运行它的PROCESS_DETACH代码。这就是为什么一般的情况下人们宁愿选择避免在Windows 95和Windows NT系统下结束应用程序的原因。
      

  4.   

    XP中只能结非系统级的进程。晕~~~~
    还有在win98系统下可以取得创建进程之程序的路径。winXP下路径则没有,为什么 。
    win98与winXP的进程标识,模块ID,都不是一样的数值,98下会被显示成负的,还是要分系统区别对待。
    还是权限不够,不知怎么提升。那个PROCESS_TERMINATE在MSDN 2002中也找不着。估计是我搜索的方法不对。
      

  5.   

    'Search Process Functions
    Private Const MAX_PATH = 260
    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 TypePrivate 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 Any) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As Any) As LongPrivate Const TH32CS_SNAPHEAPLIST = &H1
    Private Const TH32CS_SNAPPROCESS = &H2
    Private Const TH32CS_SNAPTHREAD = &H4
    Private Const TH32CS_SNAPMODULE = &H8
    Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST + TH32CS_SNAPPROCESS + TH32CS_SNAPTHREAD + TH32CS_SNAPMODULE)
    Private Const TH32CS_INHERIT = &H80000000
    'Terminate Process Functions
    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
    Const PROCESS_ALL_ACCESS = 0
    '查找进程和终结进程
      Dim hSnapshot As Long, lRet As Long, P As PROCESSENTRY32
      Dim exitCode As Long
      Dim myProcess As Long
      Dim AppKill As Boolean
        
      P.dwSize = Len(P)
      hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, ByVal 0)
      If hSnapshot Then
          lRet = Process32First(hSnapshot, P)
          Do While lRet
              If InStr(P.szExeFile, "LANCLT.EXE") <> 0 Then
                 myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, P.th32ProcessID)
                 AppKill = TerminateProcess(myProcess, exitCode)
                 Call CloseHandle(myProcess)
              End If
              lRet = Process32Next(hSnapshot, P)
          Loop
          lRet = CloseHandle(hSnapshot)
      End If
    后来发现 PROCESS_ALL_ACCESS (值为0)用 1& 代替,在win2000 XP 之下就可以结束进程了。
    但系统进程不能终结,比如svchost.exe
      

  6.   

    //PROCESS_ALL_ACCESS (值为0)用 1& 代替PROCESS_TERMINATE的值为1
      

  7.   

    谢谢,因为API Viewer查了一圈没查着。终于在MSDN里找到权限的说明了,呼,大进展。但我不保证就能弄的懂,因为——关机的那个取权限过程就怎么也弄不明白。好在我不愁时间,硬骨头慢慢啃。装的是MSDN Library 2002 的,地址是这个:
    ms-help://MS.MSDNQTR.2002JAN.1033/dllproc/prothred_68ab.htm
      

  8.   

    //终于在MSDN里找到权限的说明了,呼,大进展。但我不保证就能弄的懂,因为——关机的那个取权限过程就怎么也弄不明白。好在我不愁时间,硬骨头慢慢啃。呵呵
    不懂就过来问:)
      

  9.   

    感谢大家的帮助,进程的有关问题到此终于解决了,主要难点都已被攻克。
    将找到的文章重贴出来:'KillProcess - Terminate any applicationPrivate Type LUID
       lowpart As Long
       highpart As Long
    End TypePrivate 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 Long' Terminate any application and return an exit code to Windows
    ' This works under NT/2000, even when the calling process
    ' doesn't have the privilege to terminate the application
    ' (for example, this may happen when the process was launched
    '  by yet another program)
    '
    ' Usage:  Dim pID As Long
    '         pID = Shell("Notepad.Exe", vbNormalFocus)
    '         '...
    '         If KillProcess(pID, 0) Then
    '             MsgBox "Notepad was terminated"
    '         End IfFunction 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
        
        ' Windows NT/2000 require a special treatment
        ' to ensure that the calling process has the
        ' privileges to shut down the system
        
        ' under NT the high-order bit (that is, the sign bit)
        ' of the value retured by GetVersion is cleared
        If GetVersion() >= 0 Then
            ' open the tokens for the current process
            ' exit if any error
            If OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES Or _
                TOKEN_QUERY, hToken) = 0 Then
                GoTo CleanUp
            End If
            
            ' retrieves the locally unique identifier (LUID) used
            ' to locally represent the specified privilege name
            ' (first argument = "" means the local system)
            ' Exit if any error
            If LookupPrivilegeValue("", "SeDebugPrivilege", tp.LuidUDT) = 0 Then
                GoTo CleanUp
            End If
        
            ' complete the TOKEN_PRIVILEGES structure with the # of
            ' privileges and the desired attribute
            tp.PrivilegeCount = 1
            tp.Attributes = SE_PRIVILEGE_ENABLED
        
            ' try to acquire debug privilege for this process
            ' exit if error
            If AdjustTokenPrivileges(hToken, False, tp, 0, ByVal 0&, _
                ByVal 0&) = 0 Then
                GoTo CleanUp
            End If
        End If
        
        ' now we can finally open the other process
        ' while having complete access on its attributes
        ' exit if any error
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, hProcessID)
        If hProcess Then
            ' call was successful, so we can kill the application
            ' set return value for this function
            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
      

  10.   

    我装完成的程序贴在学院的BBS上了,下面是地址:
    页面:http://www.shizhenyuan.com/bbs/topic.php?forumid=10&filename=f_64
    下载:http://www.shizhenyuan.com/bbs/attachment.php?am=0&filename=f_64&forumid=10&article=0
    程序并不完善,代码也不规整,但我打算过一段时间再升级了。也可以理解消化一段时间。仔细算算其实里面没多少东西是自己的。
    就送给所有喜欢它的人自由使用吧。
    再次谢谢大家的帮助。准备结贴了。