Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-09
21
Medium Priority
?
315 Views
Last Modified: 2008-02-01
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

0
Comment
Question by:bluedragon99
  • 9
  • 8
  • 4
21 Comments
 
LVL 3

Expert Comment

by:a_goat
ID: 12017188
In the service settings in windows, You need to check the box that allows the service to interact with the desktop.
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 12017211
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
 
LVL 3

Expert Comment

by:a_goat
ID: 12017290
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:bluedragon99
ID: 12017339
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
 
LVL 3

Expert Comment

by:a_goat
ID: 12017402
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
 
LVL 3

Expert Comment

by:a_goat
ID: 12017411
Yeah, sorry about that other post, we were typing at the same time, so I posted before I saw your response
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 12017417
yeah that's what I'm looking for.  Can't think of any bulletproof things to check on though...
0
 
LVL 22

Expert Comment

by:cookre
ID: 12017706
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
 
LVL 22

Accepted Solution

by:
cookre earned 2000 total points
ID: 12017836
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 12018006
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 12018416
worked like a charm checking for explorer..:)  Your gonna reach a million on me cookre
0
 
LVL 22

Expert Comment

by:cookre
ID: 12018557
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 12018664
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
 
LVL 22

Expert Comment

by:cookre
ID: 12019290
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 12021424
is there a way to tell the difference between form initiated(minimize is form startup type)  and user initiated minimize??
0
 
LVL 22

Expert Comment

by:cookre
ID: 12022076
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
 
LVL 1

Author Comment

by:bluedragon99
ID: 12023406
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
 
LVL 22

Expert Comment

by:cookre
ID: 12023563
Now I'm confused.  Are you opening a window from the service?
0
 
LVL 1

Author Comment

by:bluedragon99
ID: 12044709
the program runs as a service shows up in active processes and is interactive.  The main form yes is a service..
0
 
LVL 22

Expert Comment

by:cookre
ID: 12044919
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
 
LVL 22

Expert Comment

by:cookre
ID: 12044989
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question