Here is some VB6 code to set the startup mode of a service in either Windows XP or Vista. Here is the code for disabling a service:
Private Function DisableService(strServiceName)
Dim ErrorArray(24)
Dim strComputer As String
Dim oInstance
ErrorArray(1) = "The request is not supported."
ErrorArray(2) = "The user did not have the necessary access."
ErrorArray(3) = "The service cannot be stopped because other services that are running are dependent on it."
ErrorArray(4) = "The requested control code is not valid, or it is unacceptable to the service."
ErrorArray(5) = "The requested control code cannot be sent to the service because the state of the service."
ErrorArray(6) = "The service has not been started."
ErrorArray(7) = "The service did not respond to the stop request in a timely fashion."
ErrorArray(8) = "Unknown failure when stopping the service."
ErrorArray(9) = "The directory path to the service executable was not found."
ErrorArray(10) = "The service is already stopped"
ErrorArray(11) = "The service database is locked."
ErrorArray(12) = "A dependency which this service relies on has been removed from the system."
ErrorArray(13) = "The service failed to find the service needed from a dependent service."
ErrorArray(14) = "The service has been disabled from the system."
ErrorArray(15) = "The service does not have the correct authentication to run on the system."
ErrorArray(16) = "This service is being removed from the system."
ErrorArray(17) = "There is no execution thread for the service."
ErrorArray(18) = "There are circular dependencies when stopping the service."
ErrorArray(19) = "There is a service running under the same name."
ErrorArray(20) = "There are invalid characters in the name of the service."
ErrorArray(21) = "Invalid parameters have been passed to the service."
ErrorArray(22) = "The account, which this service is to run under is either invalid or lacks the permissions to run the service."
ErrorArray(23) = "The service exists in the database of services available from the system."
ErrorArray(24) = "The service is currently paused in the system."
strComputer = "."
Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2:Win32_Service.Name=" & Chr(34) & strServiceName & Chr(34))
Set oMethod = oInstance.Methods_("ChangeStartMode")
Set oInParam = oMethod.inParameters.SpawnInstance_()
oInParam.StartMode = "Disabled"
Set oOutParam = oInstance.ExecMethod_("ChangeStartMode", oInParam)
If oOutParam.returnValue <> 0 Then
MsgBox "Change of startup mode of " & oInstance.DisplayName & " to disable failed. Reason: " & ErrorArray(oOutParam.returnValue)
End If
End Function
Currently, the code is written as two different functions, DisableService and ManualService, with the code only being different on two lines, one functional and one for a message box, so technically it could be combined into one function, which accepts two strings: the service name and what to do. However, on my installation of VB6, it doesn’t want to call the function with parenthesis, which looks wrong and, well, I don’t trust it as a result. This could technically be a standard for VB6, but I’ve seen plenty of other code samples online that use parenthesis around functions accepting multiple parameters, so it could just be an issue with my machine, or me just not understanding VB6. Anyway, the chunk of code that would change would be:
oInParam.StartMode = strSomeValue
and
MsgBox "Change of startup mode of " & oInstance.DisplayName & " to " & strSomeValue & " failed. Reason: " & ErrorArray(oOutParam.returnValue)
The value of strSomeValue could be any of the following:
- Boot
- System
- Automatic
- Manual
- Disabled
With that said, if you had two functions, DisableService and ManualService, one way to call them could be:
Private Sub SetServices()
On Error Resume Next
' Computer Browser
DisableService ("Browser")
' Messenger
DisableService ("Messenger")
' NetMeeting Remote Desktop Sharing
DisableService ("mnmsrvc")
' Remote Access Auto Connection Manager"
DisableService ("RasAuto")
' Remote Desktop Help Session Manager
DisableService ("RDSessMgr")
' Remote Registry
DisableService ("RemoteRegistry")
' Server
DisableService ("lanmanserver")
' SSDP Discovery
DisableService ("SSDPSRV")
' Telnet
DisableService ("TlntSvr")
' Universal Plug and Play Device Host
DisableService ("upnphost")
' Volume Shadow Copy
ManualService ("VSS")
' Windows Image Acquisiton
ManualService ("stisvc")
MsgBox "Computer services have been analyzed and configured."
End Sub
To find out how to call a service, just open the Services window, my personal favorite method is Start->Run->services.msc, and just double-click on a given service. You’ll want to call a service via its service name, which, in Windows XP, is the first item on the General tab. At the moment, I’m not sure what the display looks like on Windows Vista, but I believe it to be very close to the same. If all else fails, you can also easily find a list of Windows services by searching the web, and the site will typically have suggested settings for each one and common uses.