Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than advapi32, prefix the name with the module name and a period.
See warning and usage below. [hi7tech.com/blog/microsoft-corporation/]
VB Signature:
<DllImport("AdvApi32", CharSet:=CharSet.Auto, entrypoint:="ChangeServiceConfigA")> _
Public Function ChangeServiceConfig(ByVal hService As Integer, ByVal dwServiceType As ServiceType, ByVal dwStartType As ServiceStartType, ByVal dwErrorControl As ServiceErrorControl, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, ByVal lpdwTagId As Integer, ByVal lpDependencies As String, ByVal lpServiceStartName As String, ByVal lpPassword As String, ByVal lpDisplayName As String) As Boolean
End Function
If the configuration is changed for a service that is running, with the exception of lpDisplayName, the changes do not take effect until the service is stopped.
Security Remarks:
Setting the lpServiceStartName parameter changes the logon account of the service. This can cause problems. If you have registered a service principal name (SPN), it would now be registered on the wrong account. Similarly, if you have used an ACE to grant access to a service, it would now grant access to the wrong account.
Tips & Tricks:
Remember that the cofiguration settings will only be in effect the next time the service is started.
Visual Basic Example:
For those who are tired about searching and not found code running. This code snipet is functional, you only need to copy paste.
''' <summary>
''' Required to connect to the service control manager.
''' </summary>
SC_MANAGER_CONNECT = &H1
''' <summary>
''' Required to call the CreateService function to create a service
''' object and add it to the database.
''' </summary>
SC_MANAGER_CREATE_SERVICE = &H2
''' <summary>
''' Required to call the EnumServicesStatusEx function to list the
''' services that are in the database.
''' </summary>
SC_MANAGER_ENUMERATE_SERVICE = &H4
''' <summary>
''' Required to call the LockServiceDatabase function to acquire a
''' lock on the database.
''' </summary>
SC_MANAGER_LOCK = &H8
''' <summary>
''' Required to call the QueryServiceLockStatus function to retrieve
''' the lock status information for the database.
''' </summary>
SC_MANAGER_QUERY_LOCK_STATUS = &H10
''' <summary>
''' Required to call the NotifyBootConfigStatus function.
''' </summary>
SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
''' <summary>
''' Includes STANDARD_RIGHTS_REQUIRED, in addition to all access
''' rights in this table.
''' </summary>
SC_MANAGER_ALL_ACCESS = ACCESS_MASK.STANDARD_RIGHTS_REQUIRED Or _
SC_MANAGER_CONNECT Or _
SC_MANAGER_CREATE_SERVICE Or _
SC_MANAGER_ENUMERATE_SERVICE Or _
SC_MANAGER_LOCK Or _
SC_MANAGER_QUERY_LOCK_STATUS Or _
SC_MANAGER_MODIFY_BOOT_CONFIG
GENERIC_READ = ACCESS_MASK.STANDARD_RIGHTS_READ Or _
SC_MANAGER_ENUMERATE_SERVICE Or _
SC_MANAGER_QUERY_LOCK_STATUS
GENERIC_WRITE = ACCESS_MASK.STANDARD_RIGHTS_WRITE Or _
SC_MANAGER_CREATE_SERVICE Or _
SC_MANAGER_MODIFY_BOOT_CONFIG
GENERIC_EXECUTE = ACCESS_MASK.STANDARD_RIGHTS_EXECUTE Or _
SC_MANAGER_CONNECT Or SC_MANAGER_LOCK
GENERIC_ALL = SC_MANAGER_ALL_ACCESS
End Enum
<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function OpenSCManager(ByVal machineName As String, ByVal databaseName As String, ByVal desiredAccess As Int32) As IntPtr
End Function
<DllImport("advapi32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function OpenService(ByVal hSCManager As IntPtr, ByVal lpServiceName As String, ByVal dwDesiredAccess As Int32) As IntPtr
End Function
<DllImport("AdvApi32", CharSet:=CharSet.Auto, entrypoint:="ChangeServiceConfigA")> _
Private Shared Function ChangeServiceConfig(ByVal hService As Integer, ByVal dwServiceType As UInt32, ByVal dwStartType As UInt32, ByVal dwErrorControl As UInt32, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As String, ByVal lpdwTagId As Integer, ByVal lpDependencies As String, ByVal lpServiceStartName As String, ByVal lpPassword As String, ByVal lpDisplayName As String) As Boolean
End Function
Private Const SERVICE_NO_CHANGE As UInteger = &HFFFFFFFFUI
Private Const SERVICE_QUERY_CONFIG As UInteger = &H1UI
Private Const SERVICE_CHANGE_CONFIG As UInteger = &H2UI
''' <summary>
''' Changes the service startup method
''' </summary>
''' <param name="ServiceName">Indicates the name of the service, as it is shown in the service manager</param>
''' <param name="Mode">Indicates the mode you wish to stablish as startup</param>
Private Sub ChangeStartUp(ByVal ServiceName As String, ByVal Mode As System.ServiceProcess.ServiceStartMode)
Dim databaseHandle As IntPtr = OpenSCManager(Nothing, Nothing, SCM_ACCESS.SC_MANAGER_ALL_ACCESS)
If databaseHandle = IntPtr.Zero Then Throw New System.Runtime.InteropServices.ExternalException("Open Service Manager Error")
Dim m_pServiceHandle As IntPtr = OpenService(databaseHandle, ServiceName, SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG)
If m_pServiceHandle = IntPtr.Zero Then Throw New System.Runtime.InteropServices.ExternalException("Open Service Error")
If Not ChangeServiceConfig(m_pServiceHandle, SERVICE_NO_CHANGE, CUInt(Mode), SERVICE_NO_CHANGE, Nothing, Nothing, IntPtr.Zero, Nothing, Nothing, Nothing, Nothing) Then
Throw New System.Runtime.InteropServices.ExternalException("Could not change password ")
End If
End Sub
Warning:
The lpDependencies parameters requires "a pointer to a double null-terminated array of null-separated names of services or load ordering groups", this means a TCHAR array in the following format:
This is different from a normal null-terminated string. First I tried to give a string from C# code which looked like this: "Service1\0Service2\0" or like this: "Service1\0Service2\0\0", and what happened was my process calling ChangeServiceConfig hung and even worse, my Windows XP system got corrupted in a way I had to reboot in Safe Mode to be able to run System Restore.
I don't know what the Marshaler is internally doing with these strings containing null characters, but ChangeServiceConfig was happy with the adapted prototype and an array given in this way:
(In case of doubt, check rawArray in the debugger first!)
Sample Code:
IntPtr databaseHandle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
if(databaseHandle == IntPtr.Zero)
throw new System.Runtime.InteropServices.ExternalException("Open Service Manager Error");
m_pServiceHandle = OpenService(databaseHandle, strServiceName, SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);
if(m_pServiceHandle == IntPtr.Zero)
throw new System.Runtime.InteropServices.ExternalException("Open Service Error");
//This code is changing the password for the service.
if (!ChangeServiceConfig(m_pServiceHandle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, null, null,
IntPtr.Zero, null, null, value, null))
{
int nError = Marshal.GetLastWin32Error();
Win32Exception win32Exception = new Win32Exception(nError);
throw new System.Runtime.InteropServices.ExternalException("Could not change password : " + win32Exception.Message);
}
Changes the optional configuration parameters of a service.
9/29/2021 2:48:28 AM - -90.242.135.188
Changes the optional configuration parameters of a service.
8/8/2011 7:49:05 AM - -87.205.225.145
Changes the optional configuration parameters of a service.
9/29/2021 2:48:28 AM - -90.242.135.188
The QueryServiceConfig2 function retrieves the optional configuration parameters of the specified service. At present, these are the service description and the failure actions.
2/6/2011 11:47:36 PM - Smoke-190.64.103.228
The OpenService function opens an existing service.
5/6/2020 6:55:40 AM - maxbgn-178.74.224.122
The OpenService function opens an existing service.
5/6/2020 6:55:40 AM - maxbgn-178.74.224.122
The QueryServiceConfig2 function retrieves the optional configuration parameters of the specified service. At present, these are the service description and the failure actions.
2/6/2011 11:47:36 PM - Smoke-190.64.103.228
Changes the optional configuration parameters of a service.
9/29/2021 2:48:28 AM - -90.242.135.188
Changes the optional configuration parameters of a service.
9/29/2021 2:48:28 AM - -90.242.135.188
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).