title: Set existing service to "Auto (Delayed start)"
body: <p>I'm trying to set an <strong>already installed</strong> windows service to automatic delayed start in C#. How do I set a windows service to </p>
  
  <pre><code>Automatic (Delayed Start) 
  </code></pre>
  
  <p>Can't find that value in the ServiceStartMode enum.</p>
  
  <p>Edit:1</p>
  
  <pre><code>public class ServiceAutoStartInfo
  {
      [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
      private struct SERVICE_DELAYED_AUTO_START_INFO
      {
  
          public bool fDelayedAutostart;
      }
  
      [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
      [return: MarshalAs(UnmanagedType.Bool)]
      private static extern bool ChangeServiceConfig2(IntPtr hService, int dwInfoLevel, IntPtr lpInfo);
  
      // Service configuration parameter 
      const int SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3;
  
      public bool ChangeDelayedAutoStart(IntPtr hService, bool delayed)
      {
  
  
          // Validate service handle
          if (hService != IntPtr.Zero)
          {
  
  
              // Create 
              SERVICE_DELAYED_AUTO_START_INFO info = new SERVICE_DELAYED_AUTO_START_INFO();
  
              // Set the DelayedAutostart property
              info.fDelayedAutostart = delayed;
  
              // Allocate necessary memory
              IntPtr hInfo = Marshal.AllocHGlobal(Marshal.SizeOf(
  
              typeof(SERVICE_DELAYED_AUTO_START_INFO)));
  
              // Convert structure to pointer
              Marshal.StructureToPtr(info, hInfo, true);
  
              // Change the configuration
              bool result = ChangeServiceConfig2(hService, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, hInfo);
  
              // Release memory
              Marshal.FreeHGlobal(hInfo);
  
              return result;
          }
  
          return false;
      }
  }
  </code></pre>
  
  <p>This is how I call it:</p>
  
  <pre><code>var controller = new ServiceController(s.ServiceName);
  var autoDelay = new ServiceAutoStartInfo();
  autoDelay.ChangeDelayedAutoStart(controller.ServiceHandle.DangerousGetHandle(), true);
  </code></pre>
  
  <p>But with no result.</p>