[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 596
  • Last Modified:

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?
0
Neal Hartman
Asked:
Neal Hartman
1 Solution
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now