Windows Service and Application interaction

991 views Asked by At

TL;DR: I have a service, a gui, and a tray icon. I want to know how to get the tray icon to run on user login/start-up and be able to stop the service and start the gui

Some Background Info:

I have a windows service I've made that uses a xml file to collect files from other computers on my network and store them on the local pc (running the service). the xml has some structures called 'profiles' which have info like FileDestination, LocationToTakeFrom, FileTypeToTake, and IsProfileActive...

The service basically takes all the active profiles and every hour scans the location for files created within a 1hr window of the current date/time on local PC.

My GUI allows the user to make profiles / modify profiles, as well as determine which profile(s) should be active for collection. I dont want this gui running at all times, so I plan to have a systemTrayIcon to allow this GUI to be opened and shutdown.

I'm using Visual Studio 2010 .NET 4.0 everything is in C#, I have 1 solution with separate projects(gui and service)

I'm wondering about the following things as far as the System Tray Icon goes:

1) how do I have the icon start on user login (note that this will be distributed via an installer, not just my personal use. so It has to be done via code)

2) Stop a service via sysTrayIcon

3) where to place the SysTrayIcon... do I make a 3rd project? add it in the GUI project? not quite sure here.

4) if SysTrayIcon IS in a seperate project how can I have it create instances of the GUI? ie how can I start the GUI application from code in a different project

the project requirements are:

-upon installation the GUI must start, after that the Gui should only be accessed through the tray icon.

-user should be able to stop the service any time via system tray icon

1

There are 1 answers

2
JensG On

Start the client

There are plenty of ways to start an application on Login under Windows. Just grab SysInternals AutoRuns to get an idea. The obvious ones are (a) the good ol' Startup group and (b) one of the

\SOFTWARE\Microsoft\Windows\CurrentVersion\Run* 

keys under HKCU and/or HKLM. That's the typical task of an setup utility, which makes sense since you have to install the service anyway. To do it in code:

  • Registry: Open the reg key, add the entry, close the reg key.

  • Startup: determine the value of CSIDL_STARTUP or CSIDL_COMMON_STARTUP using SHGetFolderPath, then create a Shell Link in that folder.

Service communication & control

The GUI part implements the TrayIcon and communicates through some channnel with your service. Again, there are plenty of possibilities how to do this, like disk files or memory mapped files, named pipes, even sockets. It would be too broad to list all the ways here, so I'd suggest to decide for one way and ask again if you have specific questions on that one.

To stop your service from code, use the ControlService() function and pass SERVICE_CONTROL_STOP as the dwControl parameter. To start a service, there's another function named (big surprise) StartService() to achieve that. Note that you may need to start an elevated copy of your app to control services. A quick & dirty way is to simply launch net start/stop MyService elevated with the necessary args.