question

osvbnet avatar image
0 Votes"
osvbnet asked RLWA32-6355 commented

Set Service to Delayed Auto Start

Hello,
Using this code to set StartType:
https://docs.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 :)

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered RLWA32-6355 commented

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


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello, it's a Windows pre-installed service! Not using C++ though, using the mentioned .NET sample need to set to Delayed Auto! :)

0 Votes 0 ·

Wow. There was no suggestion about using c++. Had you read my answer a little more carefully you would have noticed that I said to use p/invoke with the windows API function which is the same methodology used in the referenced sample code in your question.

0 Votes 0 ·