Managed Services Launcher

Dec 16, 2008 at 8:13 PM
Hi, thanks for your windows service it's a great idea.

I understand almost all your code execpt the launcher. I have an HTC Diamond and each time I try to run the launcher the application run and stop whitout doing something...
I think my problem is about the installation of the manager can you give me some help about where and how install the manager and the launcher correctly.

Thanks in advance,

MadeByHisto
Coordinator
Mar 24, 2009 at 8:10 AM
Hi,

sorry for not replying earlier.

Your created service has to be registered. This writes the necessary entries to the registry. See more on registering a Service here.
The Launcher himself goes through these registry entries and looks for the services, which have the starttype of 1, which means "AutoStart".
For each found service the Executable is started.

Here is the code for this, how it works:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Diagnostics;

namespace ManagedServicesLauncher
{
    class Program
    {
        [MTAThread]
        static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                StartServices();
                SignalStarted(uint.Parse(args[0]));
            }
        }

        private static void StartServices()
        {
            RegistryKey mainkey = Registry.LocalMachine.OpenSubKey("Software\\ManagedServices", true);
            if (mainkey == null)
            {
                return;
            }
            RegistryKey servicekey;
            Process serviceStarter;
            //Checks for registered services
            foreach (string keyname in mainkey.GetSubKeyNames())
            {
                try
                {
                    servicekey = Registry.LocalMachine.OpenSubKey("Software\\ManagedServices\\" + keyname);
                    //Checks if registered Service has set StartType = 1(StartType.Automatic).
                    if ((int)servicekey.GetValue("StartType", 0) == 1)
                    {
                        serviceStarter = new Process();
                        serviceStarter.StartInfo = new ProcessStartInfo((string)servicekey.GetValue("Executable"), "");
                        serviceStarter.StartInfo.UseShellExecute = true;
                        serviceStarter.Start();
                    }
                }
                catch
                {
                    //Nothing in here as the next service shall be tried to be started                    
                }
            }
        }
        /// <summary>
        /// On system startup this method has to be called to return the DWORD, which has been provided by the system as command line parameter.
        /// The method is called, when the application has loaded the services correctly and is ready.
        /// </summary>
        /// <param name="dword"></param>
        [DllImport("coredll")]
        public extern static void SignalStarted(uint dword);
    }

}