Set Service to Delayed Auto Start

OSVBNET 1,386 Reputation points
2022-06-22T00:14:55.11+00:00

Hello,
Using this code to set StartType:
https://learn.microsoft.com/en-us/answers/questions/798043/stop-and-disable-service.html

It sets the StartType = Automatic but can't set to Automatic (Delayed Start)
Tried all these with no luck:
SERVICE_AUTO_START
SERVICE_BOOT_START
SERVICE_DEMAND_START
SERVICE_DISABLED
SERVICE_NO_CHANGE
SERVICE_SYSTEM_START
Any idea?
Best :)

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,567 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,481 Reputation points
    2022-06-22T01:40:07.78+00:00

    This works for me on a random service :

                    Dim bRet As Boolean = ChangeServiceConfig(scTemp.ServiceHandle, CUInt(SERVICE_NO_CHANGE), SERVICE_AUTO_START,  
                                   SERVICE_NO_CHANGE, Nothing, Nothing, IntPtr.Zero, Nothing, Nothing, Nothing, Nothing)  
                    If (bRet) Then  
                        Dim sdasi As SERVICE_DELAYED_AUTO_START_INFO = New SERVICE_DELAYED_AUTO_START_INFO()  
                        sdasi.fDelayedAutostart = True  
                        Dim pSDASI As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(SERVICE_DELAYED_AUTO_START_INFO)))  
                        Marshal.StructureToPtr(sdasi, pSDASI, False)  
                        bRet = ChangeServiceConfig2(scTemp.ServiceHandle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, pSDASI)  
                        Marshal.FreeHGlobal(pSDASI)  
                    End If  
    

    with

      <DllImport("Advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
        Public Shared Function ChangeServiceConfig(<[In]()> ByVal hService As SafeHandle, ByVal dwServiceType As UInteger, ByVal dwStartType As UInteger, ByVal dwErrorControl As UInteger,  
                                                   <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpBinaryPathName As String, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpLoadOrderGroup As String,  
                                                   ByVal lpdwTagId As IntPtr, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpDependencies As String, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpServiceStartName As String,  
                                                   <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpPassword As String, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpDisplayName As String) As <MarshalAs(UnmanagedType.Bool)> Boolean  
        End Function  
      
        <DllImport("Advapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)>  
        Public Shared Function ChangeServiceConfig2(hService As SafeHandle, dwInfoLevel As UInteger, lpInfo As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean  
        End Function  
      
      
        Public Const SERVICE_NO_CHANGE As UInteger = &HFFFFFFFFUI  
        Public Const SERVICE_BOOT_START = &H0  
        Public Const SERVICE_SYSTEM_START = &H1  
        Public Const SERVICE_AUTO_START = &H2  
        Public Const SERVICE_DEMAND_START = &H3  
        Public Const SERVICE_DISABLED = &H4  
      
        Public Const SERVICE_CONFIG_DESCRIPTION = 1  
        Public Const SERVICE_CONFIG_FAILURE_ACTIONS = 2  
        Public Const SERVICE_CONFIG_DELAYED_AUTO_START_INFO = 3  
        Public Const SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 4  
        Public Const SERVICE_CONFIG_SERVICE_SID_INFO = 5  
        Public Const SERVICE_CONFIG_REQUIRED_PRIVILEGES_INFO = 6  
        Public Const SERVICE_CONFIG_PRESHUTDOWN_INFO = 7  
        Public Const SERVICE_CONFIG_TRIGGER_INFO = 8  
        Public Const SERVICE_CONFIG_PREFERRED_NODE = 9  
        ' reserved                                     10  
        ' reserved                                     11  
        Public Const SERVICE_CONFIG_LAUNCH_PROTECTED = 12  
      
        <StructLayout(LayoutKind.Sequential)>  
        Public Structure SERVICE_DELAYED_AUTO_START_INFO  
            Public fDelayedAutostart As Boolean  
        End Structure  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RLWA32 40,276 Reputation points
    2022-06-22T01:13:26.707+00:00

    You can set the service start type as desired after the service is installed by using p/invoke and nf-winsvc-changeserviceconfig2w