Creating a Service with Systray icon

  • Thread starter Thread starter Rotzooi
  • Start date Start date
R

Rotzooi

Hi,

I have a VB.NET Service application that's running fine under the Local
System account. But for configuration purposes I don't want to be dependent
on modifying the registry manually or using an seperate application (like I
do now).

I want to implement a system tray icon when someone logs on to the console.
I've tried several samples but I can't get the icon to the system tray. I
know there are some issues because the logged on account differs from the
Local System account, and if no one is logged on to the console there is no
shell with a system tray.

How can I create a system tray icon when the shell is active? And one step
more: how can I detect if the shell is started from a Terminal Server
session or from the console?


Jeroen.
 
Rotzooi said:
Hi,

I have a VB.NET Service application that's running fine under the Local
System account. But for configuration purposes I don't want to be dependent
on modifying the registry manually or using an seperate application (like I
do now).

I want to implement a system tray icon when someone logs on to the console.
I've tried several samples but I can't get the icon to the system tray. I
know there are some issues because the logged on account differs from the
Local System account, and if no one is logged on to the console there is no
shell with a system tray.

How can I create a system tray icon when the shell is active? And one step
more: how can I detect if the shell is started from a Terminal Server
session or from the console?


Jeroen.

You won't (easily) get a windows service to have a system tray icon
since services aren't ment to have a UI. You have to write a seperate
program that goes to the systray and have that program intract with your
service in some way.

Chris
 
I disagree with that. I have several other programs that are single .exe
files and run as a service, but show an icon in the systemtray when the
console (or RDP client) is logged on. If using two seperate applications
(one service, one configuration tool), how can I parse realtime variables
from one to another? And there must be a way to detect that the shell is
started to start the systray application without using the Run-key in the
registry (like some virusscanners do).

Jeroen
 
Rotzooi,
Although you can have a service interact with the desktop, it is strongly
recommend by MS & others that you don't!!

Due largely to security reasons!
http://msdn.microsoft.com/library/d...y/en-us/dllproc/base/interactive_services.asp

Remember that most services run with heightened privileges, having it
interact with the desktop may allow non privileged users access to
privileged code.

For example SQL Server & its Service Manager. Two separate processes.

I use & promote using two separate applications. If the user wants to have
it (the config tool) run all the time, the user can add it to the startup
group (I don't automatically modify any of the Run keys in the registry).
Optionally my setup program could add the config tool to the users Startup
Group based on an option in the Install. With the Startup Group the user has
easy control over adding it or removing it at their leisure.

An added benefit from two separate applications is that you can install the
config tool on a separate machine & have it monitor the first machine...

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


|I disagree with that. I have several other programs that are single .exe
| files and run as a service, but show an icon in the systemtray when the
| console (or RDP client) is logged on. If using two seperate applications
| (one service, one configuration tool), how can I parse realtime variables
| from one to another? And there must be a way to detect that the shell is
| started to start the systray application without using the Run-key in the
| registry (like some virusscanners do).
|
| Jeroen
|
| "Chris" <[email protected]> schreef in bericht
| | > Rotzooi wrote:
| >> Hi,
| >>
| >> I have a VB.NET Service application that's running fine under the Local
| >> System account. But for configuration purposes I don't want to be
| >> dependent on modifying the registry manually or using an seperate
| >> application (like I do now).
| >>
| >> I want to implement a system tray icon when someone logs on to the
| >> console. I've tried several samples but I can't get the icon to the
| >> system tray. I know there are some issues because the logged on account
| >> differs from the Local System account, and if no one is logged on to
the
| >> console there is no shell with a system tray.
| >>
| >> How can I create a system tray icon when the shell is active? And one
| >> step more: how can I detect if the shell is started from a Terminal
| >> Server session or from the console?
| >>
| >>
| >> Jeroen.
| >
| > You won't (easily) get a windows service to have a system tray icon
since
| > services aren't ment to have a UI. You have to write a seperate program
| > that goes to the systray and have that program intract with your service
| > in some way.
| >
| > Chris
|
|
 
I was going by the email address.

|I disagree with that. I have several other programs that are single .exe
....
| Jeroen


I totally missed he signed it Jeroen...


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Jay,
|
| > Rotzooi,
|
| Means something as Rotten Heap in Dutch (Garbage), could not resist to
tell
| this to you.
|
| His subscription was Jeroen,
|
| :-)
|
| Cor
|
|
 
Thanks for the extra information about do's and don't's. Very usefull
(although the application doesn't do much accessing system resources).

How do I let the service and config tool communicate in real-time with
eachother? This is why I had to make this in one application. Any
suggestions or good links to such examples?


Jeroen a.k.a. Rotzooi a.k.a. Garbage
 
Jeroen,
Is the communication one way or two way?

You can use System.ServiceProcess.ServiceController in the config.exe to
monitor if the service is running, as well as start, stop & pause the
service. You can use ServiceBase.OnCustomCommand &
ServiceController.ExecuteCommand to send custom commands to the service,
unfortunately the custom commands are limited to 128 to 255. I would
recommend using an Enum to identify the custom commands.

I've used XmlDocument in the config.exe to modify the service's app.config
file to change configuration settings, then used a ServiceController to
restart the service, so it picks up the new settings.

For richer two way communication, you can use .NET remoting.

Both of the custom commands & remoting with a service are discussed in
Matthew MacDonalds book "Microsoft Visual Basic .NET Programmer's Cookbook"
from MS Press.

You can also use WMI (Windows Management
Instrumentation) via the classes in the System.Management namespace to
monitor your windows service.

Here is an MSDN article on WMI & .NET:

http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dnhcvs04/html/vs04d6a.asp

WMI also has events & custom objects for control. I consider Remoting over
WMI, unless I needed/wanted the WMI integration with Enterprise Management
tools.

In addition to/instead of WMI you can also simply use Performance Counters &
Event Logs to keep track of your service doing work. See
System.Diagnostics.EventLog & System.Diagnostics.PerformanceCounter.

Of course: You could also "roll your own" communication with Sockets or
TCP/IP clients, however .NET Remoting will safe you a lot of effort.


..NET 2.0 (VS 2005) introduces
System.Runtime.Remoting.Channels.Ipc.IpcChannel for .NET remoting that is
local machine only, plus System.Net.Security.NegotiateStream to secure
Remoting across machines.


FWIW: When restarting a service I use a routine similar to:

Public Shared Sub RestartService(ByVal controller As ServiceController)
controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.Stopped)
controller.Start()
End Sub


--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Thanks for the extra information about do's and don't's. Very usefull
| (although the application doesn't do much accessing system resources).
|
| How do I let the service and config tool communicate in real-time with
| eachother? This is why I had to make this in one application. Any
| suggestions or good links to such examples?
|
|
| Jeroen a.k.a. Rotzooi a.k.a. Garbage
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> schreef in
| bericht | >I was going by the email address.
| >
| > | > |I disagree with that. I have several other programs that are single
..exe
| > ...
| > | Jeroen
| >
| >
| > I totally missed he signed it Jeroen...
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Jay,
| > |
| > | > Rotzooi,
| > |
| > | Means something as Rotten Heap in Dutch (Garbage), could not resist to
| > tell
| > | this to you.
| > |
| > | His subscription was Jeroen,
| > |
| > | :-)
| > |
| > | Cor
| > |
| > |
| >
| >
|
|
 
Jay,

Thanks for the many suggestions. Since my service will use both values for
monitoring and configuring, I've decided to use the socket client way.
Although this option gives me some more overhead in programming, I can set
built-in features for quick command-line/telnet monitoring. Also I can be
more flexible in monitoring and configuring the service over the internet
when installed on remote locations (set IPs for both local and trusted
remote clients, DNS and Port). But I will have a look at Remoting for my
next project.


Jeroen



Jay B. Harlow said:
Jeroen,
Is the communication one way or two way?

You can use System.ServiceProcess.ServiceController in the config.exe to
monitor if the service is running, as well as start, stop & pause the
service. You can use ServiceBase.OnCustomCommand &
ServiceController.ExecuteCommand to send custom commands to the service,
unfortunately the custom commands are limited to 128 to 255. I would
recommend using an Enum to identify the custom commands.

I've used XmlDocument in the config.exe to modify the service's app.config
file to change configuration settings, then used a ServiceController to
restart the service, so it picks up the new settings.

For richer two way communication, you can use .NET remoting.

Both of the custom commands & remoting with a service are discussed in
Matthew MacDonalds book "Microsoft Visual Basic .NET Programmer's
Cookbook"
from MS Press.

You can also use WMI (Windows Management
Instrumentation) via the classes in the System.Management namespace to
monitor your windows service.

Here is an MSDN article on WMI & .NET:

http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dnhcvs04/html/vs04d6a.asp

WMI also has events & custom objects for control. I consider Remoting over
WMI, unless I needed/wanted the WMI integration with Enterprise Management
tools.

In addition to/instead of WMI you can also simply use Performance Counters
&
Event Logs to keep track of your service doing work. See
System.Diagnostics.EventLog & System.Diagnostics.PerformanceCounter.

Of course: You could also "roll your own" communication with Sockets or
TCP/IP clients, however .NET Remoting will safe you a lot of effort.


.NET 2.0 (VS 2005) introduces
System.Runtime.Remoting.Channels.Ipc.IpcChannel for .NET remoting that is
local machine only, plus System.Net.Security.NegotiateStream to secure
Remoting across machines.


FWIW: When restarting a service I use a routine similar to:

Public Shared Sub RestartService(ByVal controller As ServiceController)
controller.Stop()
controller.WaitForStatus(ServiceControllerStatus.Stopped)
controller.Start()
End Sub


--
Hope this helps
Jay [MVP - Outlook]
.NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


| Thanks for the extra information about do's and don't's. Very usefull
| (although the application doesn't do much accessing system resources).
|
| How do I let the service and config tool communicate in real-time with
| eachother? This is why I had to make this in one application. Any
| suggestions or good links to such examples?
|
|
| Jeroen a.k.a. Rotzooi a.k.a. Garbage
|
|
|
| "Jay B. Harlow [MVP - Outlook]" <[email protected]> schreef
in
| bericht | >I was going by the email address.
| >
| > | > |I disagree with that. I have several other programs that are single
.exe
| > ...
| > | Jeroen
| >
| >
| > I totally missed he signed it Jeroen...
| >
| >
| > --
| > Hope this helps
| > Jay [MVP - Outlook]
| > .NET Application Architect, Enthusiast, & Evangelist
| > T.S. Bradley - http://www.tsbradley.net
| >
| >
| > | > | Jay,
| > |
| > | > Rotzooi,
| > |
| > | Means something as Rotten Heap in Dutch (Garbage), could not resist
to
| > tell
| > | this to you.
| > |
| > | His subscription was Jeroen,
| > |
| > | :-)
| > |
| > | Cor
| > |
| > |
| >
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top