Exemplo n.º 1
0
        void Install(String ServiceName, String DisplayName, String Description,
                     System.ServiceProcess.ServiceAccount Account,
                     System.ServiceProcess.ServiceStartMode StartMode)
        {
            System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
            ProcessInstaller.Account = Account;

            System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

            System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
            string processPath = Process.GetCurrentProcess().MainModule.FileName;

            if (processPath != null && processPath.Length > 0)
            {
                System.IO.FileInfo fi = new System.IO.FileInfo(processPath);

                String   path    = String.Format("/assemblypath={0}", fi.FullName);
                String[] cmdline = { path };
                Context = new System.Configuration.Install.InstallContext("", cmdline);
            }

            SINST.Context     = Context;
            SINST.DisplayName = String.Format("{0}", DisplayName);
            SINST.Description = String.Format("{0}", Description);
            SINST.ServiceName = String.Format("{0}", ServiceName);
            SINST.StartType   = StartMode;
            SINST.Parent      = ProcessInstaller;

            //          SINST.ServicesDependedOn = new String[] { "Spooler", "Netlogon", "Netman" };
            SINST.ServicesDependedOn = null;

            System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
            SINST.Install(state);

            using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", ServiceName), true))
            {
                try
                {
                    //                  Object sValue = oKey.GetValue("ImagePath");
                    Object sValue = oKey.GetValue("ImagePath");
                    string str    = string.Format("{0} service", sValue.ToString());
                    oKey.SetValue("ImagePath", str);
                }
                catch (Exception Ex)
                {
                    // System.Windows.Forms.MessageBox.Show(Ex.Message);
                    System.Console.WriteLine("Failed to install {0}", Ex.Message);
                }
            }
        }
Exemplo n.º 2
0
            public static void ChangeStartMode(ServiceController svc, System.ServiceProcess.ServiceStartMode mode)
            {
                var scManagerHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);

                if (scManagerHandle == IntPtr.Zero)
                {
                    throw new ExternalException("Open Service Manager Error");
                }

                var serviceHandle = OpenService(
                    scManagerHandle,
                    svc.ServiceName,
                    SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);

                if (serviceHandle == IntPtr.Zero)
                {
                    throw new ExternalException("Open Service Error");
                }

                var result = ChangeServiceConfig(
                    serviceHandle,
                    SERVICE_NO_CHANGE,
                    (uint)mode,
                    SERVICE_NO_CHANGE,
                    null,
                    null,
                    IntPtr.Zero,
                    null,
                    null,
                    null,
                    null);

                if (result == false)
                {
                    int nError         = Marshal.GetLastWin32Error();
                    var win32Exception = new Win32Exception(nError);
                    throw new ExternalException("Could not change service start type: "
                                                + win32Exception.Message);
                }

                CloseServiceHandle(serviceHandle);
                CloseServiceHandle(scManagerHandle);
            }
Exemplo n.º 3
0
            public void Install(String ServiceName, String DisplayName, String Description,
                                System.ServiceProcess.ServiceAccount Account,
                                System.ServiceProcess.ServiceStartMode StartMode)
            {
                System.ServiceProcess.ServiceProcessInstaller ProcessInstaller = new System.ServiceProcess.ServiceProcessInstaller();
                ProcessInstaller.Account = Account;

                System.ServiceProcess.ServiceInstaller SINST = new System.ServiceProcess.ServiceInstaller();

                System.Configuration.Install.InstallContext Context = new System.Configuration.Install.InstallContext();
                string processPath = Process.GetCurrentProcess().MainModule.FileName;

                if (processPath != null && processPath.Length > 0)
                {
                    System.IO.FileInfo fi = new System.IO.FileInfo(processPath);

                    String   path    = String.Format("/assemblypath={0}", fi.FullName);
                    String[] cmdline = { path };
                    Context = new System.Configuration.Install.InstallContext("", cmdline);
                }

                SINST.Context     = Context;
                SINST.DisplayName = DisplayName;
                SINST.Description = Description;
                SINST.ServiceName = ServiceName;
                SINST.StartType   = StartMode;
                SINST.Parent      = ProcessInstaller;

                System.Collections.Specialized.ListDictionary state = new System.Collections.Specialized.ListDictionary();
                SINST.Install(state);

                using (RegistryKey oKey = Registry.LocalMachine.OpenSubKey(String.Format(@"SYSTEM\CurrentControlSet\Services\{0}", SINST.ServiceName), true))
                {
                    try
                    {
                        Object sValue = oKey.GetValue("ImagePath");
                        oKey.SetValue("ImagePath", sValue);
                    }
                    catch (Exception Ex)
                    {
                    }
                }
            }