NT Service and Tray Icon

I have a VB6 App that runs as an NT service set to "Automatic". The App is supposed to start as a tray icon. It does when I run the app manually. When it starts up as a service, before login, the icon is not there in the System Tray after login. The App is running though. How do I get the icon in the System Tray after login?
LVL 1
Neal HartmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

autCommented:
If you have IE4 or later installed, then you can do a simple subclassing routine to be notified when Windows Explorer starts (it also notifies when explorer crashes).

Important Note:
Since this code uses subclassing, you will not be able to run the code from within the development environment. VB will crash if you try to do that. However, when you compile and run the EXE, all will be well...


Here's the source code:

'Paste this in a module (MUST BE in a module!)
'--------------------------------
Option Explicit

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Const GWL_WNDPROC = (-4)

Public Declare Function GetProp Lib "user32" Alias "GetPropA" (ByVal hWnd As Long, ByVal lpString As String) As Long
Public Declare Function SetProp Lib "user32" Alias "SetPropA" (ByVal hWnd As Long, ByVal lpString As String, ByVal hData As Long) As Long
Public Declare Function RemoveProp Lib "user32" Alias "RemovePropA" (ByVal hWnd As Long, ByVal lpString As String) As Long

Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private WM_TASKBARCREATED As Long
Private Declare Function RegisterWindowMessage Lib "user32" Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long

Public Function SubClassProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    'This function is run when the registered event occurs
    Dim lProc As Long
    Dim lPtr As Long

    'Get the Procedure's address
    lProc = GetProp(hWnd, "ExSubClassProc")
    lPtr = GetProp(hWnd, "ExSubClassPtr")

    If wMsg = WM_TASKBARCREATED Then
        'Put your code here
        MsgBox "Windows Explorer is restarting"
    End If

    'Continue with old WindowProc
    SubClassProc = CallWindowProc(lProc, hWnd, wMsg, wParam, lParam)
End Function

Public Sub SubClass(ByRef hWnd As Long)
    Dim l As Long
   
    'Already subclassed
    If GetProp(hWnd, "ExSubClassProc") <> 0 Then Exit Sub

    'Register new event
    WM_TASKBARCREATED = RegisterWindowMessage("TaskbarCreated")
   
    'Store the new Prop
    l = GetWindowLong(hWnd, GWL_WNDPROC)
    SetProp hWnd, "ExSubClassProc", l
    SetProp hWnd, "ExSubClassPtr", SetWindowLong(hWnd, GWL_WNDPROC, AddressOf SubClassProc)
End Sub

Public Sub UnSubClass(ByRef hWnd As Long)
    Dim l As Long
   
    l = GetProp(hWnd, "ExSubClassProc")

    'Not subclassed
    If l = 0 Then Exit Sub

    'Remove the previously-stored prop
    SetWindowLong hWnd, GWL_WNDPROC, l
    RemoveProp hWnd, "ExSubClassProc"
    RemoveProp hWnd, "ExSubClassPtr"
End Sub



'And in the form that handles the tray icon, paste this code:
'--------------------------------
Private Sub Form_Load()
    SubClass Me.hWnd
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    UnSubClass Me.hWnd
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.