window service 恢复选项卡设置

[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, IntPtr lpInfo);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr OpenSCManager(string MachineName, string DatabaseName, uint DesiredAcces);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr OpenService(IntPtr scHandle, string serviceName, int DesiredAccess
);
        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern bool CloseServiceHandle(IntPtr hSCObject);

        public struct SC_ACTION
        {
            public int Type;
            public int Delay;
        }

        public struct SERVICE_FAILURE_ACTIONS
        {
            public int dwResetPeriod;
            public string lpRebootMsg;
            public string lpCommand;
            public int cActions;
            public IntPtr lpsaActions;
        }
        static void ChangeFailureActions(string ServiceName)
        {
            IntPtr scHandle = IntPtr.Zero;
            IntPtr svcHandle = IntPtr.Zero;
            string serviceName = ServiceName;
            try
            {
                scHandle = OpenSCManager(null, null, (uint)0xF003F);
                if (scHandle == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Error connecting to Service Control Manager. Error provided was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                svcHandle = OpenService(scHandle, serviceName, (int)0xF01FF);
                if (svcHandle == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Error opening service for modifying. Error returned was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                SC_ACTION action = new SC_ACTION();
                action.Type = 1;
                action.Delay = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;

                IntPtr lpsaActions = Marshal.AllocHGlobal(Marshal.SizeOf(action)*2);
                if (lpsaActions == IntPtr.Zero)
                {
                    throw new Exception(String.Format("Unable to allocate memory for service action, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.StructureToPtr(action, lpsaActions, false);

                IntPtr nextAction = (IntPtr)(lpsaActions.ToInt64() + Marshal.SizeOf(action));
                action.Type = 1;

                Marshal.StructureToPtr(action, nextAction, false);

                SERVICE_FAILURE_ACTIONS failureActions = new SERVICE_FAILURE_ACTIONS();
                failureActions.dwResetPeriod = (int)TimeSpan.FromDays(1).TotalSeconds;
                failureActions.lpRebootMsg = "";
                failureActions.lpCommand = "";
                failureActions.cActions = 2;
                failureActions.lpsaActions = lpsaActions;

                IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(failureActions));
                if (lpInfo == IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(lpsaActions);
                    throw new Exception(String.Format("Unable to allocate memory, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.StructureToPtr(failureActions, lpInfo, false);

                if (!ChangeServiceConfig2(svcHandle, 2, lpInfo))
                {
                    Marshal.FreeHGlobal(lpInfo);
                    Marshal.FreeHGlobal(lpsaActions);
                    throw new Exception(String.Format("Error setting service config, error was: 0x{0:X}", Marshal.GetLastWin32Error()));
                }

                Marshal.FreeHGlobal(lpInfo);
                Marshal.FreeHGlobal(lpsaActions);
            }
            catch (Exception ex)
            {
            }
            if (svcHandle != IntPtr.Zero)
            {
                CloseServiceHandle(svcHandle);
            }

            if (scHandle != IntPtr.Zero)
            {
                CloseServiceHandle(scHandle);
            }
        }

posted @ 2011-06-27 16:13  驢騎士  阅读(690)  评论(0编辑  收藏  举报