示例#1
0
 public Service1()
 {
     InitializeComponent();
     CanShutdown = true;
     tim = new Timer();
     tim.Interval = 5000;
     tim.Elapsed += tim_Elapsed;
     FieldInfo acceptedCommandsFieldInfo = typeof(ServiceBase).GetField("acceptedCommands", BindingFlags.Instance | BindingFlags.NonPublic);
     int value = (int)acceptedCommandsFieldInfo.GetValue(this);
     acceptedCommandsFieldInfo.SetValue(this, value | SERVICE_ACCEPT_PRESHUTDOWN);
     StreamWriter writer = new StreamWriter("D:\\LogConst.txt", true);
     try
     {
         IntPtr hMngr = OpenSCManager("localhost", null, (uint)SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
         IntPtr hSvc = OpenService(hMngr, "WindowsService1", (uint)SCM_ACCESS.SC_MANAGER_ALL_ACCESS);
         SERVICE_PRESHUTDOWN_INFO spi = new SERVICE_PRESHUTDOWN_INFO();
         spi.dwPreshutdownTimeout = 5000;
         IntPtr lpInfo = Marshal.AllocHGlobal(Marshal.SizeOf(spi));
         if (lpInfo == IntPtr.Zero)
         {
             writer.WriteLine(String.Format("Unable to allocate memory for service action, error was: 0x{0:X} -- {1}", Marshal.GetLastWin32Error(), DateTime.Now.ToLongTimeString()));
         }
         Marshal.StructureToPtr(spi, lpInfo, false);
         // apply the new timeout value
         if (!ChangeServiceConfig2(hSvc, (int)INFO_LEVEL.SERVICE_CONFIG_PRESHUTDOWN_INFO, lpInfo))
             writer.WriteLine(DateTime.Now.ToLongTimeString() + " Failed to change service timeout");
         else
             writer.WriteLine(DateTime.Now.ToLongTimeString() + " change service timeout : " + spi.dwPreshutdownTimeout);
     }
     catch (Exception ex)
     {
         writer.WriteLine(DateTime.Now.ToLongTimeString() + " " + ex.Message);
     }
     writer.Close();
 }
示例#2
0
    public override void Install(System.Collections.IDictionary stateSaver)
    {
        base.Install(stateSaver);
        Version versionWinVistaSp1 = new Version(6, 0, 6001);

        if (Environment.OSVersion.Platform != PlatformID.Win32NT || Environment.OSVersion.Version < versionWinVistaSp1)
        {
            //Preshutdown is not supported
            return;
        }
        Context.LogMessage(string.Format("Setting preshutdown timeout {0}ms to service {1}", PreshutdownTimeout, ServiceName));
        IntPtr service   = IntPtr.Zero;
        IntPtr sCManager = IntPtr.Zero;

        try
        {
            // Open the service control manager
            sCManager = OpenSCManager(null, null, ServiceControlAccessRights.SC_MANAGER_CONNECT);
            if (sCManager == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to open Service Control Manager.");
            }
            // Open the service
            service = OpenService(sCManager, ServiceName, ServiceAccessRights.SERVICE_CHANGE_CONFIG);
            if (service == IntPtr.Zero)
            {
                throw new Win32Exception();
            }
            // Set up the preshutdown timeout structure
            SERVICE_PRESHUTDOWN_INFO preshutdownInfo = new SERVICE_PRESHUTDOWN_INFO();
            preshutdownInfo.dwPreshutdownTimeout = (uint)_preshutdownTimeout;
            // Make the change
            int changeResult = ChangeServiceConfig2(
                service,
                ServiceConfig2InfoLevel.SERVICE_CONFIG_PRESHUTDOWN_INFO,
                ref preshutdownInfo);
            // Check that the change occurred
            if (changeResult == 0)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Unable to change the Service configuration.");
            }
            Context.LogMessage(string.Format("Preshutdown timeout {0}ms set to service {1}", PreshutdownTimeout, ServiceName));
        }
        finally
        {
            // Clean up
            if (service != IntPtr.Zero)
            {
                CloseServiceHandle(service);
            }
            if (sCManager != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(sCManager);
            }
        }
    }
示例#3
0
 public static extern int ChangeServiceConfig2(
     IntPtr hService,
     ServiceConfig2InfoLevel dwInfoLevel,
     ref SERVICE_PRESHUTDOWN_INFO lpInfo);
 public static extern bool ChangeServiceConfig2(
     SafeServiceHandle hService,
     int dwInfoLevel,
     ref SERVICE_PRESHUTDOWN_INFO lpInfo);
示例#5
0
 public static extern bool ChangeServiceConfig2(
     SafeServiceHandle hService,
     int dwInfoLevel,
     ref SERVICE_PRESHUTDOWN_INFO lpInfo);