示例#1
0
        private void BindCustomServices()
        {
            List <WinSvc> winServices = null;

            try
            {
                string[] svcs;
                if ((WinSvcStr == null) || (WinSvcStr.Length <= 0))
                {
                    svcs = WinServices;
                }
                else
                {
                    svcs = WinSvcStr;
                }
                foreach (string svcName in svcs)
                {
                    using (ServiceController sc = new ServiceController(svcName, Environment.MachineName))
                    {
                        if (winServices == null)
                        {
                            winServices = new List <WinSvc>();
                        }
                        WinSvc svc = new WinSvc();
                        svc.ServiceName    = sc.ServiceName;
                        svc.DisplayName    = sc.DisplayName;
                        svc.MachineName    = sc.MachineName;
                        svc.CanStop        = sc.CanStop;
                        svc.CanShutdown    = sc.CanShutdown;
                        svc.CurrentSvcType = sc.ServiceType;
                        svc.ServiceHandle  = sc.ServiceHandle;
                        svc.Status         = sc.Status;
                        svc.LastUpdated    = DateTime.Now;
                        winServices.Add(svc);
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                Log.Error(ex);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            GridProcesses.DataSource = winServices;
            // Add the context menu to the grid columns
            foreach (DataGridViewColumn col in GridProcesses.Columns)
            {
                col.ContextMenuStrip = GridContextMenu;
                col.ContextMenuStrip.Items.Add(stopToolStripMenuItem);
                col.ContextMenuStrip.Items.Add(startToolStripMenuItem);
                col.ContextMenuStrip.Items.Add(restartToolStripMenuItem);
            }
        }
示例#2
0
        private void BindWinServices()
        {
            List <WinSvc> winServices = null;

            ServiceController[] scs = null;
            try
            {
                using (ServiceController sc = new ServiceController("W3SVC", Environment.MachineName))
                {
                    scs    = new ServiceController[1];
                    scs[0] = sc;
                }
            }
            catch (InvalidOperationException ex)
            {
                Log.Error(ex);
            }
            for (int i = 0; i < scs.Length; i++)
            {
                winServices = new List <WinSvc>(scs.Length);
                WinSvc svc = new WinSvc();
                svc.ServiceName    = scs[i].ServiceName;
                svc.DisplayName    = scs[i].DisplayName;
                svc.MachineName    = scs[i].MachineName;
                svc.CanStop        = scs[i].CanStop;
                svc.CanShutdown    = scs[i].CanShutdown;
                svc.CurrentSvcType = scs[i].ServiceType;
                svc.ServiceHandle  = scs[i].ServiceHandle;
                svc.Status         = scs[i].Status;
                svc.LastUpdated    = DateTime.Now;
                winServices.Add(svc);
            }
            scs = null;
            GridProcesses.DataSource = winServices;
            // Add the context menu to the grid columns
            foreach (DataGridViewColumn col in GridProcesses.Columns)
            {
                col.ContextMenuStrip = GridContextMenu;
                col.ContextMenuStrip.Items.Add(stopToolStripMenuItem);
                col.ContextMenuStrip.Items.Add(startToolStripMenuItem);
                col.ContextMenuStrip.Items.Add(restartToolStripMenuItem);
            }
        }
示例#3
0
        /// <summary>
        /// Changes the user account used by the named native Windows service.
        /// </summary>
        /// <param name="serviceName">The service name.</param>
        /// <param name="account">Specifies the account type.</param>
        /// <param name="userID">The user ID (or <c>null</c> if <b>account!=ServiceAccount.User)</b>.</param>
        /// <param name="password">The password (or <c>null</c> if <b>account!=ServiceAccount.User)</b>.</param>
        /// <exception cref="ServiceException">Thrown if there's a problem completing the operation.</exception>
        public static void ChangeServiceAccount(string serviceName, ServiceAccount account, string userID, string password)
        {
            var hScManager = IntPtr.Zero;
            var hScLock    = IntPtr.Zero;
            var hService   = IntPtr.Zero;

            try
            {
                hScManager = WinSvc.OpenSCManager(null, null, WinSvc.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS);

                if (hScManager.ToInt64() <= 0)
                {
                    throw new ServiceException("Error [{0}] opening the service manager.", Marshal.GetLastWin32Error());
                }

                hScLock = WinSvc.LockServiceDatabase(hScManager);

                if (hScLock.ToInt64() <= 0)
                {
                    throw new ServiceException("Error [{0}] locking the service manager.", Marshal.GetLastWin32Error());
                }

                hService = WinSvc.OpenService(hScManager, serviceName, WinSvc.ACCESS_TYPE.SERVICE_ALL_ACCESS);

                if (hService.ToInt64() <= 0)
                {
                    throw new ServiceException("Error [{0}] opening service [{1}].", Marshal.GetLastWin32Error(), serviceName);
                }

                switch (account)
                {
                case ServiceAccount.LocalService:

                    userID   = @"NT Authority\LocalService";
                    password = null;
                    break;

                case ServiceAccount.LocalSystem:

                    userID   = @".\LocalSystem";
                    password = null;
                    break;

                case ServiceAccount.NetworkService:

                    userID   = @"NT Authority\NetworkService";
                    password = null;
                    break;

                case ServiceAccount.User:

                    break;

                default:

                    throw new NotImplementedException();
                }

                if (!WinSvc.ChangeServiceConfig(hService,
                                                WinSvc.ServiceType.SERVICE_WIN32_OWN_PROCESS,
                                                WinSvc.SERVICE_NO_CHANGE,
                                                WinSvc.SERVICE_NO_CHANGE,
                                                null,
                                                null,
                                                IntPtr.Zero,
                                                null,
                                                userID,
                                                password,
                                                null))
                {
                    throw new ServiceException("Error [{0}] updating the [{1}] service configuration.", Marshal.GetLastWin32Error(), serviceName);
                }
            }
            finally
            {
                if (hService.ToInt64() < 0)
                {
                    WinSvc.CloseServiceHandle(hService);
                }

                if (hScLock.ToInt64() > 0)
                {
                    WinSvc.UnlockServiceDatabase(hScLock);
                }

                if (hScManager.ToInt64() > 0)
                {
                    WinSvc.CloseServiceHandle(hScManager);
                }
            }
        }