Solved

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

Posted on 2004-09-09
21
307 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

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

Expert Comment

by:cookre
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Now I'm confused.  Are you opening a window from the service?
0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now