none
How to change service type

    Question

  • I'm looking for the equivalent command to "sc.exe config [service name]  type= own|Share"

    I know with ServiceControler I can view the type with  service.ServiceType, but according to the documentation this is only a view command.

    Thursday, March 16, 2017 1:02 PM

Answers

  • Win32 APIs can be called in any language.

    A quick sample, just for testing =>

    	Dim sServiceName As String = "your_service_name"
    	Dim hSCM = OpenSCManager(Nothing, Nothing, SC_MANAGER_ALL_ACCESS)
            If hSCM <> IntPtr.Zero Then
                Dim hService = OpenService(hSCM, sServiceName, SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG)
                If hService <> IntPtr.Zero Then
                    Dim bReturn As Boolean = ChangeServiceConfig(hService, CUInt(SERVICE_WIN32_SHARE_PROCESS), SERVICE_NO_CHANGE, SERVICE_NO_CHANGE,
                                                     Nothing, Nothing, IntPtr.Zero, Nothing, Nothing, Nothing, Nothing)
                   If bReturn = False Then
                        Dim nError As Integer = Marshal.GetLastWin32Error()
                        Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                        Throw New ExternalException("ChangeServiceConfig error: " & Convert.ToString(win32Exception.Message))
                    Else
                        MsgBox("ChangeServiceConfig OK for service : " & sServiceName, MsgBoxStyle.Information)
                    End If
                    CloseServiceHandle(hService)
                Else
                    Dim nError As Integer = Marshal.GetLastWin32Error()
                    Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                    Throw New ExternalException("OpenService error: " & Convert.ToString(win32Exception.Message))
                End If
                CloseServiceHandle(hSCM)
            Else
                Dim nError As Integer = Marshal.GetLastWin32Error()
                Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                Throw New ExternalException("OpenSCManager error: " & Convert.ToString(win32Exception.Message))
            End If

    Declarations :

        Public Const SERVICE_QUERY_CONFIG As UInteger = &H1
        Public Const SERVICE_CHANGE_CONFIG As UInteger = &H2
        Public Const SC_MANAGER_ALL_ACCESS As UInteger = &HF003F
    
        Private Const SERVICE_NO_CHANGE As UInteger = &HFFFFFFFFUI
        Private Const SERVICE_KERNEL_DRIVER As UInteger = &H1
        Private Const SERVICE_FILE_SYSTEM_DRIVER As UInteger = &H2
        Private Const SERVICE_WIN32_OWN_PROCESS As UInteger = &H10
        Private Const SERVICE_WIN32_SHARE_PROCESS As UInteger = &H20
        Private Const SERVICE_INTERACTIVE_PROCESS As UInteger = &H100
    
        <DllImport("advapi32.dll", EntryPoint:="OpenSCManagerW", SetLastError:=True)>
        Public Shared Function OpenSCManager(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpMachineName As String,
                                                <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDatabaseName As String,
                                                ByVal dwDesiredAccess As UInteger) As IntPtr
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="ChangeServiceConfigW", SetLastError:=True)>
        Public Shared Function ChangeServiceConfig(<InAttribute()> ByVal hService As IntPtr, ByVal dwServiceType As UInteger, ByVal dwStartType As UInteger, ByVal dwErrorControl As UInteger,
                                                       <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpBinaryPathName As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpLoadOrderGroup As String,
                                                       ByVal lpdwTagId As IntPtr, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDependencies As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpServiceStartName As String,
                                                       <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpPassword As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDisplayName As String) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="OpenServiceW", SetLastError:=True)>
        Public Shared Function OpenService(<InAttribute()> ByVal hSCManager As IntPtr, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpServiceName As String,
                                                ByVal dwDesiredAccess As UInteger) As System.IntPtr
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="CloseServiceHandle", SetLastError:=True)>
        Public Shared Function CloseServiceHandle(ByVal hSCObject As IntPtr) As Integer
        End Function

    • Marked as answer by Inzane Thursday, March 16, 2017 5:03 PM
    Thursday, March 16, 2017 4:53 PM

All replies

  • Thursday, March 16, 2017 1:34 PM
  • Sorry but is there an example to do this in VB? Seems to be C##. Am I going to have to declare a new function referencing the advapi32.dll?
    Thursday, March 16, 2017 2:36 PM
  • Win32 APIs can be called in any language.

    A quick sample, just for testing =>

    	Dim sServiceName As String = "your_service_name"
    	Dim hSCM = OpenSCManager(Nothing, Nothing, SC_MANAGER_ALL_ACCESS)
            If hSCM <> IntPtr.Zero Then
                Dim hService = OpenService(hSCM, sServiceName, SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG)
                If hService <> IntPtr.Zero Then
                    Dim bReturn As Boolean = ChangeServiceConfig(hService, CUInt(SERVICE_WIN32_SHARE_PROCESS), SERVICE_NO_CHANGE, SERVICE_NO_CHANGE,
                                                     Nothing, Nothing, IntPtr.Zero, Nothing, Nothing, Nothing, Nothing)
                   If bReturn = False Then
                        Dim nError As Integer = Marshal.GetLastWin32Error()
                        Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                        Throw New ExternalException("ChangeServiceConfig error: " & Convert.ToString(win32Exception.Message))
                    Else
                        MsgBox("ChangeServiceConfig OK for service : " & sServiceName, MsgBoxStyle.Information)
                    End If
                    CloseServiceHandle(hService)
                Else
                    Dim nError As Integer = Marshal.GetLastWin32Error()
                    Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                    Throw New ExternalException("OpenService error: " & Convert.ToString(win32Exception.Message))
                End If
                CloseServiceHandle(hSCM)
            Else
                Dim nError As Integer = Marshal.GetLastWin32Error()
                Dim win32Exception = New System.ComponentModel.Win32Exception(nError)
                Throw New ExternalException("OpenSCManager error: " & Convert.ToString(win32Exception.Message))
            End If

    Declarations :

        Public Const SERVICE_QUERY_CONFIG As UInteger = &H1
        Public Const SERVICE_CHANGE_CONFIG As UInteger = &H2
        Public Const SC_MANAGER_ALL_ACCESS As UInteger = &HF003F
    
        Private Const SERVICE_NO_CHANGE As UInteger = &HFFFFFFFFUI
        Private Const SERVICE_KERNEL_DRIVER As UInteger = &H1
        Private Const SERVICE_FILE_SYSTEM_DRIVER As UInteger = &H2
        Private Const SERVICE_WIN32_OWN_PROCESS As UInteger = &H10
        Private Const SERVICE_WIN32_SHARE_PROCESS As UInteger = &H20
        Private Const SERVICE_INTERACTIVE_PROCESS As UInteger = &H100
    
        <DllImport("advapi32.dll", EntryPoint:="OpenSCManagerW", SetLastError:=True)>
        Public Shared Function OpenSCManager(<InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpMachineName As String,
                                                <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDatabaseName As String,
                                                ByVal dwDesiredAccess As UInteger) As IntPtr
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="ChangeServiceConfigW", SetLastError:=True)>
        Public Shared Function ChangeServiceConfig(<InAttribute()> ByVal hService As IntPtr, ByVal dwServiceType As UInteger, ByVal dwStartType As UInteger, ByVal dwErrorControl As UInteger,
                                                       <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpBinaryPathName As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpLoadOrderGroup As String,
                                                       ByVal lpdwTagId As IntPtr, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDependencies As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpServiceStartName As String,
                                                       <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpPassword As String, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpDisplayName As String) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="OpenServiceW", SetLastError:=True)>
        Public Shared Function OpenService(<InAttribute()> ByVal hSCManager As IntPtr, <InAttribute(), MarshalAsAttribute(UnmanagedType.LPWStr)> ByVal lpServiceName As String,
                                                ByVal dwDesiredAccess As UInteger) As System.IntPtr
        End Function
    
        <DllImport("advapi32.dll", EntryPoint:="CloseServiceHandle", SetLastError:=True)>
        Public Shared Function CloseServiceHandle(ByVal hSCObject As IntPtr) As Integer
        End Function

    • Marked as answer by Inzane Thursday, March 16, 2017 5:03 PM
    Thursday, March 16, 2017 4:53 PM
  • Thanks for the example, and your help.
    Thursday, March 16, 2017 5:03 PM