Closing a SysTray program

MarkWard
MarkWard used Ask the Experts™
on
I have a prog.exe that start at startup and minimises into the windows systray. How do I close it (programatically) to upgrade it with a patch. (it has no window as it is in the systray). I want the upgrade patch to close it so the User intervention (and thus instruction) is not required.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kill it with task manager.

ie Ctrl+Alt+Del and select "Task Manager"
then select the process "Prog.exe" and do an "End Task" on it.
Ken right when he says you must kill the process.  To do this in VB6 use the following code

Place the following code in a module

Option Explicit

Public Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, _
     ByVal bInheritHandle As Long, _
     ByVal dwProcessId As Long) As Long
     
Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, _
ByVal uExitCode As Long)

Private Declare Function GetExitCodeProcess Lib "kernel32" _
   (ByVal hProcess As Long, lpExitCode As Long) As Long
   
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Public Sub KillProcess(strYourEXE As String)
Dim intProcessID As Integer
Dim bComplete As Boolean

   intProcessID = Shell(strYourEXE, 1)
   Do Until bComplete
       bComplete = EndShelledProcess(intProcessID)
   Loop
End Sub

Public Function EndShelledProcess(ShellReturnValue As Long) As Boolean

'PURPOSE: End a process started with VB's Shell Statement
'INPUT: Task ID returned by Shell
'RETURNS: True if succesful, false otherwise

On Error GoTo EndShelledProcess_Error

Dim hInst As Long
Dim hProcess As Long
Dim lExitCode As Long
Dim lRet As Long

hInst = ShellReturnValue
If hInst = 0 Then Exit Function

'Get handle to process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, hInst)
If hProcess <> 0 Then
    'get exit code
    GetExitCodeProcess hProcess, lExitCode
        If lExitCode <> 0 Then
            lRet = TerminateProcess(hProcess, lExitCode)
            EndShelledProcess = lRet > 0
        End If
Else 'Not running
    EndShelledProcess = True
End If

EndShelledProcess_Error:
    App.LogEvent "A VB Run-Time error ocurred in " _
    & Err.Source _
    & "! Error: " & Err.Number & " - " & Err.Description, 1
End Function


Hope that helps, can't take credit for most of the code as I found it on Planet Source code years ago.

Thanks

Brendan

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between KenTweedie and bkeoghsmith

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial