JDBasic
|
SIMÃO DIAS SE - BRASIL
|
|
ENUNCIADA !
|
|
|
Postada em 25/06/2008 17:39 hs
Boa tarde pessoal.
Preciso fechar um programa em execucao no windows passando o nome do exe e apenas a instancia do usuario local, pois esse comando sera executado no windows server que terah outros usuarios com o mesmo exe aberto.
Ja encontrei varios exemplos na internet, muitos passando o caption do form o que nao serve para o meu caso, pois o caption desse exe eh variavel.
Um dos exemplos que encontrei foi esse:
O unico problema eh que ele nao fecha o exe do usuario local e sim o primeiro que encontrar.
Alguem teria um melhor? Public Const TH32CS_SNAPPROCESS As Long = 2&
Public 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 PROCESS_ALL_ACCESS As Long = &H1F0FFF
Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" _ (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function ProcessMin Lib "kernel32" _ Alias "Process32Min" _ (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 OpenProcess Lib "kernel32" _ (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _ ByVal dwProcessId As Long) As Long
Public Declare Function TerminateProcess Lib "kernel32" _ (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Declare Sub CloseHandle Lib "kernel32" _ (ByVal hPass As Long)
[c]Public Function CloseProcess(EXEName As String) As Boolean Dim hSnapShot As Long Dim uProcess As PROCESSENTRY32 Dim hProcess As Long
CloseProcess = False hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If hSnapShot = -1 Then Exit Function
uProcess.dwSize = Len(uProcess) If ProcessMin(hSnapShot, uProcess) = 1 Then Do If LCase$(Left$(uProcess.szExeFile, InStr(1, uProcess.szExeFile, vbNullChar) - 1)) = LCase$(EXEName) Then hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID) CloseProcess = TerminateProcess(hProcess, ByVal 0&) > 0 Exit Do End If Loop While ProcessNext(hSnapShot, uProcess) End If
Call CloseHandle(hSnapShot) End Function
|
|
|
|
|
Postada em 26/06/2008 09:50 hs
|
|
|
JDBasic
|
SIMÃO DIAS SE - BRASIL
|
|
ENUNCIADA !
|
|
|
Postada em 26/06/2008 12:27 hs
Achei um solucao pratica. Eu capturei o ID do processo quando eu executo no Shell. Dim ProcID As Long
ProcID = Shell("Programa.exe", vbNormalFocus) Pronto, como cada processo tem um ID diferente. Depois chamei a funcao de API TerminateProcess para fechar aquele ID especifico. Call ProcessTerminate(ProcID)
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject 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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Function ProcessTerminate(Optional lProcessID As Long) As Boolean Dim lhwndProcess As Long Dim lExitCode As Long Const PROCESS_TERMINATE = &H1 On Error Resume Next If lProcessID Then '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 Fica a dica para quem precisar. T+
|
|
|
|