FECHAR PROGRAMA EXTERNO PELO NOME DO PROGRAMA
Olá!!!
Gostaria de saber como faço para fechar um programa externo pelo nome do programa. Vi no fórum uma maneira de fechar através do caption da tela, porém eu tenho três executáveis que se chamam faturamento, aà fechar pelo caption não me adianta.
Valeu!!!
Gostaria de saber como faço para fechar um programa externo pelo nome do programa. Vi no fórum uma maneira de fechar através do caption da tela, porém eu tenho três executáveis que se chamam faturamento, aà fechar pelo caption não me adianta.
Valeu!!!
matando processos
Public Const MAX_PATH As Integer = 260
Public Const TH32CS_SNAPPROCESS = &H2
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
Public Declare Function CloseHandle Lib "kernel32" (ByVal hFile As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID 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 ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Type LUID: UsedPart As Long: IgnoredForNowHigh32BitPart As Long: End Type
Private Type TOKEN_PRIVILEGES: PrivilegeCount As Long: TheLuid As LUID: Attributes As Long: End Type
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (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
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId 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 OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Function GetProcessIDByEXEName(ByVal EXEName As String) As Long
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim R As Long, lStrtemp As String
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = -1 Then Exit Function
uProcess.dwSize = Len(uProcess)
R = ProcessFirst(hSnapShot, uProcess)
Do While R
If InStr(UCase(uProcess.szExeFile), UCase(EXEName)) <> 0 Then
GetProcessIDByEXEName = uProcess.th32ProcessID
Call CloseHandle(hSnapShot)
Exit Function
End If
R = ProcessNext(hSnapShot, uProcess)
Loop
Call CloseHandle(hSnapShot)
End Function
é exatamente o que o YAMAMOTO estava colocando
Feche pelo nome do processo, tem um exemplo no site,ÂÂÂ'um código do ARTWORK
http://www.vbmania.com.br/vbmdetail.php?varID=218
Coloque num módulo:
AÃ você coloca uma sub assim no seu formulário
Feche pelo nome do processo, tem um exemplo no site,ÂÂÂ'um código do ARTWORK
http://www.vbmania.com.br/vbmdetail.php?varID=218
Coloque num módulo:
Option Explicit
Public Const MAX_PATH As Integer = 260
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
Public Const TH32CS_SNAPPROCESS = &H2
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Global hSnapShot As Long
Global uProcess As PROCESSENTRY32
Public Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Global rProcess As Long
Global tPID As Long
Global tMID As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hFile As Long) As Long
Public Const PROCESS_TERMINATE = &H1
Global cescolhido As String
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Global hProcess As Long
Global lExitCode As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Type LUID: UsedPart As Long: IgnoredForNowHigh32BitPart As Long: End Type
Private Type OSVERSIONINFO: dwOSVersionInfoSize As Long: dwMajorVersion As Long: dwMinorVersion As Long: dwBuildNumber As Long: dwPlatformId As Long: szCSDVersion As String * 128: End Type
Private Type TOKEN_PRIVILEGES: PrivilegeCount As Long: TheLuid As LUID: Attributes As Long: End Type
Private Declare Function AdjustTokenPrivileges Lib "advapi32" (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
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId 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 OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Function GetProcessIDByEXEName(ByVal EXEName As String) As Long
Dim hSnapShot As Long
Dim uProcess As PROCESSENTRY32
Dim R As Long, lStrtemp As String
hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnapShot = -1 Then Exit Function
uProcess.dwSize = Len(uProcess)
R = ProcessFirst(hSnapShot, uProcess)
Do While R
If InStr(UCase(uProcess.szExeFile), UCase(EXEName)) <> 0 Then
GetProcessIDByEXEName = uProcess.th32ProcessID
Call CloseHandle(hSnapShot)
Exit Function
End If
R = ProcessNext(hSnapShot, uProcess)
Loop
Call CloseHandle(hSnapShot)
End Function
Function ProcessTerminate(Optional lProcessID As Long, Optional lHwndWindow As Long) As Boolean
Dim lhwndProcess As Long
Dim lExitCode As Long
Dim lRetVal As Long
Dim lhThisProc As Long
Dim lhTokenHandle As Long
Dim tLuid As LUID
Dim tTokenPriv As TOKEN_PRIVILEGES, tTokenPrivNew As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF, PROCESS_TERMINATE = &H1
Const ANYSIZE_ARRAY = 1, TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8, SE_DEBUG_NAME As String = "SeDebugPrivilege"
Const SE_PRIVILEGE_ENABLED = &H2
On Error Resume Next
If lHwndWindow Then
'Get the process ID from the window handle
lRetVal = GetWindowThreadProcessId(lHwndWindow, lProcessID)
End If
If lProcessID Then
'Give Kill permissions to this process
lhThisProc = GetCurrentProcess
OpenProcessToken lhThisProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lhTokenHandle
LookupPrivilegeValue "", SE_DEBUG_NAME, tLuid
'Set the number of privileges to be change
tTokenPriv.PrivilegeCount = 1
tTokenPriv.TheLuid = tLuid
tTokenPriv.Attributes = SE_PRIVILEGE_ENABLED
'Enable the kill privilege in the access token of this process
AdjustTokenPrivileges lhTokenHandle, False, tTokenPriv, Len(tTokenPrivNew), tTokenPrivNew, lBufferNeeded
'Open the process to kill
lhwndProcess = OpenProcess(PROCESS_TERMINATE, 0, lProcessID)
If lhwndProcess Then
'Obtained process handle, kill the process
ProcessTerminate = CBool(TerminateProcess(lhwndProcess, lExitCode))
Call CloseHandle(lhwndProcess)
End If
End If
On Error GoTo 0
End Function
AÃ você coloca uma sub assim no seu formulário
Sub Finalizar(NomeExe as String)
Dim IDProcesso as long
idprocesso = (GetProcessIDByEXEName(NomeExe)=0)
if not idprocesso = 0 then ProcessTerminate idprocesso
End Sub
'PARA FINALIZAR PELO NOME DO EXECUTÃ ÂVEL
finalizar NomeDoExecutavel
Corrigindo o Sub
Sub Finalizar(NomeExe as String)
Dim IDProcesso as long
idprocesso = GetProcessIDByEXEName(NomeExe)
if not idprocesso = 0 then ProcessTerminate idprocesso
End Sub
Valeu Thiago, funcionou mesmo!!!
Tópico encerrado , respostas não são mais permitidas