Visual basic prog run as service won't show in sys tray

Hello,

I am using this module to display my proggie in the system tray, problem is it runs as a service and there is no system tray before the login screen.  The program runs fine but does not appear in the sys tray.  Ideas??



Declare Function Shell_NotifyIcon Lib "shell32.dll" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long

Public Const NIM_ADD = &H0
Public Const NIM_DELETE = &H2
Public Const NIM_MODIFY = &H1

Public Const NIF_ICON = &H2
Public Const NIF_MESSAGE = &H1
Public Const NIF_TIP = &H4

Public Const WM_LBUTTONDBLCLK = &H203
Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202
Public Const WM_MOUSEMOVE = &H200
Public Const WM_RBUTTONDBLCLK = &H206
Public Const WM_RBUTTONDOWN = &H204
Public Const WM_RBUTTONUP = &H205

Type NOTIFYICONDATA
        cbSize As Long
        hwnd As Long
        uID As Long
        uFlags As Long
        uCallbackMessage As Long
        hIcon As Long
        szTip As String * 64
End Type

Public IconData As NOTIFYICONDATA

LVL 1
bluedragon99Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
cookreConnect With a Mentor Commented:
An easy way to see if anyone is logged in is to look for aninstance of explorer.  If you're in .NET, just check GetProcessByName("explorer").  

Otherwise, you'll have to EnumProcesses() to see if explorer is among them.
0
 
a_goatCommented:
In the service settings in windows, You need to check the box that allows the service to interact with the desktop.
0
 
bluedragon99Author Commented:
Yeah I thought that was it too.  It's checked.  Once logged in if you do a restart on the service it shows up in the tray, leading me to believe that since I am not logged in yet it tries to goto the non existent tray.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
a_goatCommented:
Sorry, to clarify....

Your service can run as either the Local System account or a specific user.  If that user is logged in, they should see the icon.  If it's the system account though, there's a checkbox in the Service's Properties dialog box under the "Log On" tab that allows you to mark that the service may interact with the user's desktop.

I don't think that there's a way to set that automatically, but I could be wrong
0
 
bluedragon99Author Commented:
Using this with the microsoft NTSVC ocx and it does set the interactive check box for sure..If I set the form startup to normal rather than minimized (which would put it to the tray) it works fine.

NTService.StartService

strDisplayName = "ProgramNameHere"
NTService.ServiceName = strDisplayName
NTService.DisplayName = strDisplayName
NTService.Interactive = True
NTService.StartMode = svcStartAutomatic
0
 
a_goatCommented:
Try polling every now and then to see if the user has logged in and redisplaying the icon then.  Maybe there's some system event or a kernel call you can make to see if the user is on
0
 
a_goatCommented:
Yeah, sorry about that other post, we were typing at the same time, so I posted before I saw your response
0
 
bluedragon99Author Commented:
yeah that's what I'm looking for.  Can't think of any bulletproof things to check on though...
0
 
cookreCommented:
If no one is logged in, there's no instance of Explorer, no visible desktop, no notification area, so you have to wait until someone does log in.

Then, even if someone is logged in, the program still has to explicitly place its icon in the notification area with a call to Shell_NotifyIcon().

0
 
bluedragon99Author Commented:
K writing the explorer "checker" now I'll let ya know..don't know if the tray will be there explorer loads pretty early in the login process tho.
0
 
bluedragon99Author Commented:
worked like a charm checking for explorer..:)  Your gonna reach a million on me cookre
0
 
cookreCommented:
True, and the notification area comes up near the end.  You can either put a decent delay in after confirming explorer but before painting the icon, or do a regular re-paint.  I've often done the latter to change the tool tip as a way of showing some form of status.

Not that although you can put an icon in the notification area from a service, you'll never get any messages related to it (click, moudeover, etc).  If you must interact with the tray icon from the service, you'll have to start another process with, say, a hidden window, have the other process get the tray icon messages then notify the service about what happened.
0
 
bluedragon99Author Commented:
yeah the delay works but its kinda hokie..ie machine takes longer to boot problems arise..not to mention when you actually minimize it manuall the delay is there too...any way to distinguish when form startup minimize occurs and user clicked minimize(dont want delay here obviously)..
0
 
cookreCommented:
You can catch such messages with SetWindowsHookEx(), but it's a bit of a pain.

One service I wrote had a 5 second timer for checking things.  One of things I did therein was to goose the tray icon.  Once someone logged on, the icon would eventually show up.
0
 
bluedragon99Author Commented:
is there a way to tell the difference between form initiated(minimize is form startup type)  and user initiated minimize??
0
 
cookreCommented:
If VB creates the window minimized, then there's no minimize message.

An easier way would be to add a Form_Resize() event and check inside it to see if the window is minimized.
0
 
bluedragon99Author Commented:
yeah thats where the current delay check for explorer code is..it's prob not gonna work due to the fact that the user clicks minimize once the computer is at the desktop and there is a 15 sec delay due to the timer1 set for checking for explorer check.  If i set the timer to say 1 sec it sees explorer and quickly minimizes which is too quick (no system tray yet.)  hmmmm...
0
 
cookreCommented:
Now I'm confused.  Are you opening a window from the service?
0
 
bluedragon99Author Commented:
the program runs as a service shows up in active processes and is interactive.  The main form yes is a service..
0
 
cookreCommented:
The check for Explorer should be at the very beginning before you show the form, i.e., in FormLoad().

Also, I think (meaninig I don't really know) the resize event caused by a minimize in your case will be triggered only by user action, so it should be safe to put it in the tray at that point.
0
 
cookreCommented:
Also, don't forget that as a service, even INTERACTIVE, you won't be able to interact with the tray icon, all you'll be able to do is show it and delete it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.