Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2002
    Messaggi
    957

    [VB 6] Terminare un processo

    Ciao volevo sapere come posso fermare un processo in WinXP avendo solo il nome dell'exe, senza avere l'ID del processo.
    Grazie, ciao

  2. #2
    Prova a creare un modulo con questo:
    codice:
    '=====STOPS A PROGRAM DEAD IN ITS TRACKS!==============
    ' usage: killapp("process_nam") in NT
    ' ie, only the name that appears in the task
    '     manager (since process_name.exe is too long)
    'in w2k, XP, etc, use full name "process_name.exe"
    'for compatability, use both (won't report any error if proc not found):
    'killapp("process_nam")
    'killapp("process_name.exe")
    '======================================================
    Option Explicit
    Const MAX_PATH& = 260
    
    Declare 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 Long
    Private 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 Long
    
    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 Type
    
    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 Type
    
    Const TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
    Const PROCESS_ALL_ACCESS = &H1F0FFF
    
    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
                    'put a message here to say something when the program
                    'is successfully terminated
                End If
    
           End If
           rProcessFound = ProcessNext(hSnapshot, uProcess)
       Loop
       Call CloseHandle(hSnapshot)
       Exit Function
    Finish:
        ' Only comes here if there's an error - dont do anything
        
    End Function
    
    Function KillProcess(ByVal hProcessID As Long, Optional ByVal exitCode As Long) As Boolean
        
        'Terminate any application and return an exit code to Windows.
        
        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
    
    Public Function pid_from_name(myName As String) As Long
       
       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
               appCount = appCount + 1
               myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                pid_from_name = (uProcess.th32ProcessID)
           End If
           rProcessFound = ProcessNext(hSnapshot, uProcess)
       Loop
       Call CloseHandle(hSnapshot)
       Exit Function
    Finish:
        ' Only comes here if there's an error - dont do anything
    
    End Function

    Poi richiama la funzione ad esempio...

    codice:
     
    Call KillApp("PROGRAMMA.EXE")

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.