Controlling services with VB6  
Author Message
ddcaron





PostPosted: Fri Oct 12 06:07:03 PDT 2007 Top

Visual Basic >> Controlling services with VB6

I thought I had seen this before, but today Google isn't turning up
anything useful on this.

I need to be able to control (start, stop, enable, disable) NT services
on XP with a VB 6 program. I am NOT looking to write a service with VB,
just control already-existing ones.

Can somebody please point me to some sample code to get me started, or
to the API calls to use for this? (I'm not real proficient with the
windows API, but can usually manage to struggle through it if somebody
can give me a starting point).

TIA!

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).

Visual Studio173  
 
 
Phill





PostPosted: Fri Oct 12 06:07:03 PDT 2007 Top

Visual Basic >> Controlling services with VB6

> I need to be able to control (start, stop, enable, disable) NT services
> on XP with a VB 6 program.

> Can somebody please point me to some sample code to get me started, or
> to the API calls to use for this?

The quick and dirty way is to Shell() out the "sc" command, with the
necessary arguments:

sc start <servicename>
sc stop <servicename>
sc config start= <mode> <servicename>

If you need to redirect the output (into a file) remember to Shell
"cmd"; redirection is a function of the shell, not of the commands that
the shell kicks off.

Shell( "cmd /C 'sc stop Service1 > %APPDATA%\sc.log ' " )

Have another Google around for "Shell-and-Wait".

HTH,
Phill W.
 
 
David





PostPosted: Fri Oct 12 06:52:46 PDT 2007 Top

Visual Basic >> Controlling services with VB6
n-.-a-c-.-u-k says...

>
> > I need to be able to control (start, stop, enable, disable) NT services
> > on XP with a VB 6 program.
>
> > Can somebody please point me to some sample code to get me started, or
> > to the API calls to use for this?
>
> The quick and dirty way is to Shell() out the "sc" command, with the
> necessary arguments:

Quick and dirty is just fine for this purpose, and your suggestion is
actually better than a compiled .exe; we're setting up a bunch of oem
systems for our specific needs, and need to disable a bunch of the
standard services. We're already running a set of scripts to do some
other stuff to these systems, so adding lines to them is better than a
separate program.

Thanks!!


>
> sc start <servicename>
> sc stop <servicename>
> sc config start= <mode> <servicename>
>
> If you need to redirect the output (into a file) remember to Shell
> "cmd"; redirection is a function of the shell, not of the commands that
> the shell kicks off.
>
> Shell( "cmd /C 'sc stop Service1 > %APPDATA%\sc.log ' " )
>
> Have another Google around for "Shell-and-Wait".

....

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
 
 
David





PostPosted: Fri Oct 12 06:55:43 PDT 2007 Top

Visual Basic >> Controlling services with VB6
n-.-a-c-.-u-k says...

>
> > I need to be able to control (start, stop, enable, disable) NT services
> > on XP with a VB 6 program.
>
> > Can somebody please point me to some sample code to get me started, or
> > to the API calls to use for this?
>
> The quick and dirty way is to Shell() out the "sc" command, with the
> necessary arguments:
>
> sc start <servicename>
> sc stop <servicename>
> sc config start= <mode> <servicename>

Also, is the sc command new with XP? I've never heard of it before, and
I was pretty familiar with NT's management tools.

....

--
Remove the ns_ from if replying by e-mail (but keep posts in the
newsgroups if possible).
 
 
Bob





PostPosted: Fri Oct 12 07:12:17 PDT 2007 Top

Visual Basic >> Controlling services with VB6

>I thought I had seen this before, but today Google isn't turning up
> anything useful on this.
>
> I need to be able to control (start, stop, enable, disable) NT services
> on XP with a VB 6 program. I am NOT looking to write a service with VB,
> just control already-existing ones.
>
> Can somebody please point me to some sample code to get me started, or
> to the API calls to use for this? (I'm not real proficient with the
> windows API, but can usually manage to struggle through it if somebody
> can give me a starting point).

set s=getobject("WinNT://./<servicename>,Service")
s.stop
msgbox s.status
s.start
msgbox s.status

http://support.microsoft.com/kb/234001

Const ADS_SERVICE_STOPPED = 1
Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_STOP_PENDING = 3
Const ADS_SERVICE_RUNNING = 4
Const ADS_SERVICE_CONTINUE_PENDING = 5
Const ADS_SERVICE_PAUSE_PENDING = 6
Const ADS_SERVICE_PAUSED = 7
Const ADS_SERVICE_ERROR = 8

 
 
Ralph





PostPosted: Fri Oct 12 08:27:29 PDT 2007 Top

Visual Basic >> Controlling services with VB6



> n-.-a-c-.-u-k says...

> >
> > > I need to be able to control (start, stop, enable, disable) NT
services
> > > on XP with a VB 6 program.
> >
> > > Can somebody please point me to some sample code to get me started, or
> > > to the API calls to use for this?
> >
> > The quick and dirty way is to Shell() out the "sc" command, with the
> > necessary arguments:
> >
> > sc start <servicename>
> > sc stop <servicename>
> > sc config start= <mode> <servicename>
>
> Also, is the sc command new with XP? I've never heard of it before, and
> I was pretty familiar with NT's management tools.
>

http://www.networkclue.com/os/Windows/commands/index.aspx



 
 
Jeff





PostPosted: Fri Oct 12 08:53:00 PDT 2007 Top

Visual Basic >> Controlling services with VB6


> I need to be able to control (start, stop, enable, disable) NT services
> on XP with a VB 6 program. I am NOT looking to write a service with VB,
> just control already-existing ones.

Not my code (L.J. Johnson perhaps?), but I don't remember the link, so just
paste this into a .BAS module:

Option Explicit

' Service API stuff

Public Declare Function OpenSCManager Lib "advapi32.dll" Alias
"OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As
String, ByVal dwDesiredAccess As Long) As Long
Public Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal
hSCObject As Long) As Long
Public Declare Function SetServiceStatus Lib "advapi32.dll" (ByVal
ServiceStatus As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal
hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Public Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA"
(ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal
dwDesiredAccess As Long) As Long
Public Declare Function ControlService Lib "advapi32.dll" (ByVal hService As
Long, ByVal controlcommand As ServiceControl, lpServiceStatus As
SERVICE_STATUS) As Long
Public Declare Function StartService Lib "advapi32.dll" Alias
"StartServiceA" (ByVal hService As Long, ByVal ArgCount As Long, ByVal
lpArgVectors As Long) As Long
Public Declare Function GetServiceKeyName Lib "advapi32.dll" Alias
"GetServiceKeyNameA" (ByVal hSCManager As Long, ByVal DisplayName As String,
ByVal ServiceName As String, BuffSize As Long) As Long
Public Declare Function GetServiceDisplayName Lib "advapi32.dll" Alias
"GetServiceDisplayNameA" (ByVal hSCManager As Long, ByVal ServiceName As
String, ByVal DisplayName As String, BuffSize As Long) As Long
Public Declare Function ChangeServiceConfig Lib "advapi32.dll" Alias
"ChangeServiceConfigA" (ByVal hService As Long, ByVal dwServiceType As Long,
ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal
lpBinaryPathName As Long, ByVal lpLoadOrderGroup As Long, ByVal lpdwTagId As
Long, ByVal lpDependencies As Long, ByVal lpServiceStartName As Long, ByVal
lpPassword As Long, ByVal lpDisplayName As Long) As Long
Public Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As
Long) As Long

' Control manager access
Public Const SC_MANAGER_CONNECT = &H1
Public Const SC_MANAGER_CREATE_SERVICE = &H2
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const SC_MANAGER_LOCK = &H8
Public Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Public Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20
Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
Public Const SC_MANAGER_ALL_ACCESS = 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 Or STANDARD_RIGHTS_REQUIRED
' Service access
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public Const SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or _
SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or _
SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or _
SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL

Public Const SERVICE_NO_CHANGE = &HFFFFFFFF

Public Enum ServiceStartModes
stmAtBoot = 0
stmOnSystemInit = 1
stmAutomatic = 2
stmManual = 3
stmDisabled = 4
End Enum

Public Enum ServiceStates
sstUnknown = 0 ' not a real status code
sstStopped = 1
sstStartPending = 2
sstStopPending = 3
sstRunning = 4
sstContinuePending = 5
sstPausePending = 6
sstPaused = 7
End Enum

Public Enum ServiceControl
scStop = 1
scPause = 2
scContinue = 3
scInterrogate = 4
scShutdown = 5
End Enum

Public Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As ServiceStates
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type

Public Function ChangeServiceStartMode(ByVal ServiceName As String, ByVal
StartMode As ServiceStartModes)
Dim hSCManager As Long, hService As Long, lResult As Long
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName,
SERVICE_CHANGE_CONFIG)
If hService <> 0 Then
lResult = ChangeServiceConfig(hService, SERVICE_NO_CHANGE,
StartMode, SERVICE_NO_CHANGE, _
0&, 0&, 0&, 0&, 0&, 0&, 0&)
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function GetDisplayName(ByVal ServiceName As String) As String
' Function: Get the name that displays in the Services applet based
' on the registry name
Dim strOut As String, hSCManager As Long, x As Long
GetDisplayName = ServiceName ' default
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
strOut = Space$(256)
x = Len(strOut)
If GetServiceDisplayName(hSCManager, ByVal ServiceName, strOut, x) = 1
Then
x = InStr(strOut, vbNullChar)
If x > 0 Then GetDisplayName = Left$(strOut, x - 1)
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function GetServiceName(ByVal DisplayName As String) As String
' Function: Get the registry name of the service based on the name that
' displays in the Services applet
Dim strOut As String, hSCManager As Long, x As Long
GetServiceName = DisplayName ' default
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
strOut = Space$(256)
x = Len(strOut)
If GetServiceKeyName(hSCManager, ByVal DisplayName, strOut, x) = 1
Then
x = InStr(strOut, vbNullChar)
If x > 0 Then GetServiceName = Left$(strOut, x - 1)
End If
CloseServiceHandle hSCManager
End If
End Function


Public Function GetServiceState(ByVal ServiceName As String) As
ServiceStates
Dim x As Long, hSCManager As Long, hService As Long
Dim ssStatus As SERVICE_STATUS
GetServiceState = sstUnknown
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName,
SERVICE_QUERY_STATUS)
If hService <> 0 Then
If QueryServiceStatus(hService, ssStatus) = 1 Then
GetServiceState = ssStatus.dwCurrentState
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function SetServiceContinued(ByVal ServiceName As String) As
ServiceStates
Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
SetServiceContinued = sstUnknown
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName,
SERVICE_PAUSE_CONTINUE)
If hService <> 0 Then
If ControlService(hService, scContinue, ssStatus) = 1 Then
SetServiceContinued = ssStatus.dwCurrentState
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function SetServicePaused(ByVal ServiceName As String) As
ServiceStates
Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
SetServicePaused = sstUnknown
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName,
SERVICE_PAUSE_CONTINUE)
If hService <> 0 Then
If ControlService(hService, scPause, ssStatus) = 1 Then
SetServicePaused = ssStatus.dwCurrentState
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function SetServiceStarted(ByVal ServiceName As String) As
ServiceStates
Dim hSCManager As Long, hService As Long
SetServiceStarted = sstUnknown
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_ALL_ACCESS)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName,
SERVICE_ALL_ACCESS)
If hService <> 0 Then
Err.Clear
If StartService(hService, 0, 0) = 1 Then
SetServiceStarted = sstRunning
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function

Public Function SetServiceStopped(ByVal ServiceName As String) As
ServiceStates
Dim ssStatus As SERVICE_STATUS, hSCManager As Long, hService As Long
SetServiceStopped = sstUnknown
hSCManager = OpenSCManager(vbNullString, vbNullString,
SC_MANAGER_CONNECT)
If hSCManager <> 0 Then
hService = OpenService(hSCManager, ByVal ServiceName, SERVICE_STOP)
If hService <> 0 Then
If ControlService(hService, scStop, ssStatus) = 1 Then
SetServiceStopped = ssStatus.dwCurrentState
End If
CloseServiceHandle hService
End If
CloseServiceHandle hSCManager
End If
End Function



 
 
Bob





PostPosted: Fri Oct 12 08:57:13 PDT 2007 Top

Visual Basic >> Controlling services with VB6



>
>> I need to be able to control (start, stop, enable, disable) NT services
>> on XP with a VB 6 program. I am NOT looking to write a service with VB,
>> just control already-existing ones.
>
> Not my code (L.J. Johnson perhaps?),

Nope, very old code of mine actually. No problem though as I'm not
concerned about attribution especially since I would probably not use that
any more. The ADSI model is just so much cleaner to work with than the API.


 
 
Jeff





PostPosted: Fri Oct 12 11:12:03 PDT 2007 Top

Visual Basic >> Controlling services with VB6


>> Not my code (L.J. Johnson perhaps?),
>
> Nope, very old code of mine actually.

Wow. I'm pretty sure I got it off of his site (whatever it was called). So
now I know its ancestry....

For reference, I've used it successfully many time in the past and am very
happy with it. I prefer (cover your ears, Rick) directly using the API to a
middle-ware library anyway.


 
 
Bob





PostPosted: Fri Oct 12 12:54:30 PDT 2007 Top

Visual Basic >> Controlling services with VB6



>
>>> Not my code (L.J. Johnson perhaps?),
>>
>> Nope, very old code of mine actually.
>
> Wow. I'm pretty sure I got it off of his site (whatever it was called). So
> now I know its ancestry....

I posted it long ago and I didn't go through that line-by-line to see if
it's exactly what I wrote but I recognize the procedure names and general
syntax.

> For reference, I've used it successfully many time in the past and am very
> happy with it. I prefer (cover your ears, Rick) directly using the API to
> a middle-ware library anyway.

I still have versions of that in a few apps that have survived and I tend to
like the API as well but in newer apps it just makes sense to me to use the
simpler approach, especially since I know I can always swap in the API if I
need to. I do a lot of stuff that has to be used in VB, VBA and VBScript so
the non-API lets me be consistent without resorting to DLLs all the time.


 
 
Karl





PostPosted: Thu Oct 18 17:33:10 PDT 2007 Top

Visual Basic >> Controlling services with VB6
>>>> Not my code (L.J. Johnson perhaps?),
>>>
>>> Nope, very old code of mine actually.
>>
>> Wow. I'm pretty sure I got it off of his site (whatever it was called). So
>> now I know its ancestry....
>
> I posted it long ago and I didn't go through that line-by-line to see if
> it's exactly what I wrote but I recognize the procedure names and general
> syntax.

Definitely not L.J.'s! He had the *ghoulash* just dripping off of everything! <g>
--
.NET: It's About Trust!
http://vfred.mvps.org