Não sei se isso é um bug ou uma caracterítica do recurso, mas vou deixar aqui um código que elimina o problema.
Obs.: o código não foi escrito por mim, consegui-o nas inúmeras pesquisas pela internet e estou apenas repassando aqui. Já utilizei-o por várias vezes, sempre com sucesso.
'Para usar - exemplo:
'Call Closeprocess("Excel.exe")
================================
Option Explicit
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Long = 260
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 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 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)
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 ProcessFirst(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
2 comentários:
Kazu, me ajude com o código para fechar o excel, através do acces
tenho dúvidas nas seguintes linhas
as linhas abaixo estão em vermelho no código:
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Long = 260
Public Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
Abaixo, devo trocar o nome EXEname?
Public Function CloseProcess(EXEName As String) As Boolean
Excel.exe também?
Call Closeprocess("Excel.exe")
Obrigado
Call CloseProcess("EXCEL.EXE").
observe as maiúsculas.
abs!
Postar um comentário