小弟已经一年多没来这里晃荡了,编程有些生疏,希望大家不要见笑:
现在我要写个程序在2000或者XP下关闭进程,目前已经使用PSAPI.DLL中的函数     EnumProcesses() ,EnumProcessModules() ,GetModuleFileNameExA()来取得当前进程列表,不知道为什么没办法关闭进程,我用了OpenProcess和TerminateProcess,不知道是什么方法没用对(OpenProcess的结果总是0),各位兄弟能否把代码写一遍,让我再试试,这里就把c;\winnt\regedit.exe作为要关闭的进程,不要用发送WM_CLOSE或者别的消息的方法来关闭,谢谢了程序完成后立即给分!

解决方案 »

  1.   

    //不要用发送WM_CLOSE或者别的消息的方法来关闭晕~用SetCursorPos移动鼠标到“X”上。
    用mouse_event模拟鼠标左键点击。:)
      

  2.   

    /************************************************************
    write by: C.H.K.K 03.07.26
    ************************************************************/
    //函数:关机
    EXPORT BOOL ShutDownWin2000()
    {
        TOKEN_PRIVILEGES tkp;
    HANDLE hToken;
    long fResult;
    if (!OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    &hToken) ) 
    {
        MessageBox(NULL,"OpenProcessToken failed!","",0);
    return FALSE;
    }
    //得到当前进程的存取令牌句柄以便得到关机的特权
        LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
    &tkp.Privileges[0].Luid); //获得本地机唯一的标识
    //得到关机的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(NULL,"AdjustTokenPrivileges enable failed!(未能取得关机的特权)","",0);
    return FALSE;
    }
    //开始关机并选择关机的方式
        fResult =InitiateSystemShutdown( 
                 NULL,                 // 要关的计算机用户名,可在局域网网中关掉对方的机器,NULL表示关本机
                 "由于系统不稳定,WINDOWS将在上面的时间内关机,请做好保存工作!",  // 显示关机前的消息
                 10,                                 // 关机所需的时间(seconds)
    //While this dialog box is displayed, the shutdown can be stopped by the AbortSystemShutdown function. 
                 TRUE,                               // force applications closed flag 
                 FALSE);                             //设为TRUE为重起,设为FALSE为关机    if(!fResult) 
        { 
            MessageBox(NULL,"InitiateSystemShutdown failed.","",0); 
    return FALSE;

    //禁止关机特权
        tkp.Privileges[0].Attributes = 0; 
        AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
    (PTOKEN_PRIVILEGES) NULL, 0);     if (GetLastError() != ERROR_SUCCESS) 
    {
             MessageBox(NULL,"AdjustTokenPrivileges disable failed.","",0); 
     return FALSE;
    }     ExitWindowsEx(EWX_SHUTDOWN,0);     //开始安全的关机,EWX_FORCE强制关机并不保证已经保存 return TRUE;
    }
      

  3.   

    哈哈,贴错了
    dwProcessHwnd = OpenProcess(PROCESS_TERMINATE, True, dwProcessId)
    'If dwProcessHwnd <= 0 Then
    '    MsgBox "can't open process"
    '    Exit Sub
    'End IfMsgBox dwProcessHwnd
    ans = TerminateProcess(dwProcessHwnd, 0)CloseHandle dwProcessHwnd有的时候关不了试你的权限不够
      

  4.   

    谢谢了,我的代码和你的差不多,但是我不认为我的权限不够,我是管理员的权限比如在你的程序里我只是得到的dwProcessHwnd为0
    为什么是0呢
      

  5.   

    这段代码是一个老外写的。其中,KillApp可以杀掉任何应用程序的所有实例,而KillProcess能够杀掉所有进程:'模块代码
    Option Explicit
    Const MAX_PATH& = 260Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
    Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongPrivate 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 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 LongType 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 Type
    '---------------------------------------
    Public Function KillApp(myName As String) As Boolean
       Const TH32CS_SNAPPROCESS As Long = 2&
       Const PROCESS_ALL_ACCESS = 0
       Dim uProcess As PROCESSENTRY32
       Dim rProcessFound As Long
       Dim hSnapshot As Long
       Dim szExename As String
       Dim exitCode As Long
       Dim myProcess As Long
       Dim AppKill As Boolean
       Dim appCount As Integer
       Dim i As Integer
       On Local Error GoTo Finish
       appCount = 0
       
       uProcess.dwSize = Len(uProcess)
       hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
       rProcessFound = ProcessFirst(hSnapshot, uProcess)
       Do While rProcessFound
           i = InStr(1, uProcess.szexeFile, Chr(0))
           szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
           If Right$(szExename, Len(myName)) = LCase$(myName) Then
               KillApp = True
               appCount = appCount + 1
               myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                If KillProcess(uProcess.th32ProcessID, 0) Then
                    'For debug.... Remove this
                    MsgBox "Instance no. " & appCount & " of " & szExename & " was terminated!"
                End If       End If
           rProcessFound = ProcessNext(hSnapshot, uProcess)
       Loop
       Call CloseHandle(hSnapshot)
       Exit Function
    Finish:
        MsgBox "Error!"
    End Function'Terminate any application and return an exit code to Windows.
    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'窗体代码
    Option Explicit'Example on how to use the code' this is the click event of a button named cmdKill
    Private Sub Command1_Click()
        ' Usage:
        Dim pID As Long
        Dim i As Integer
        Dim strExe As String
        strExe = "Notepad.Exe"
        For i = 0 To 4
            pID = Shell(strExe, vbNormalFocus)
        Next i
        'Five instances of notpade.exe is now created
        Debug.Assert False
        MsgBox "It is " & _
            KillApp(strExe) & _
            " that all instances of " & vbCrLf & _
            strExe & _
            " have been terminated!"
    End Sub