[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

How do I start/stop a system service

GS

3/9/2012 7:36:00 PM

My apps rely on WMI for gathering hardware info. How would I start WMI
if not running, then stop it when I'm done using it?

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


35 Answers

MikeD

3/9/2012 8:46:00 PM

0



"GS" <gs@somewhere.net> wrote in message news:jjdm45$kv9$1@dont-email.me...
> My apps rely on WMI for gathering hardware info. How would I start WMI if
> not running, then stop it when I'm done using it?
>


The simplest way I can think of would be to use "net start" from the command
line. You can run cmd.exe with the /c parameter followed by the net start
command. For example (syntax may not be exactly right):

cmd.exe /c net start "Windows Management Instrumentation"

Similarly, to stop the service, use "net stop".

Mike


unknown

3/9/2012 9:02:00 PM

0

GS wrote:
> My apps rely on WMI for gathering hardware info. How would I start WMI
> if not running, then stop it when I'm done using it?

One obvious VB6 source site(Vbnet) shows how to call API functions to
accomplish that, but it seems that you didn't search the web or newsgroups.

But forget it. You can't start or stop services if your app is running in
the context of the limited "Users" group, and perhaps unelevated Admins in
Vista+, but you can query if the service is running or not.


GS

3/9/2012 10:36:00 PM

0

Farnsworth submitted this idea :
> GS wrote:
>> My apps rely on WMI for gathering hardware info. How would I start WMI
>> if not running, then stop it when I'm done using it?
>
> One obvious VB6 source site(Vbnet) shows how to call API functions to
> accomplish that, but it seems that you didn't search the web or newsgroups.

I did search (Google: -always my first course of action) but only found
stuff relating to a VB6 ActiveX.exe, indicating I should have tried
different keywords.

I don't get your inference to Vbnet being an 'obvious source' for
Classic VB!!!
>
> But forget it. You can't start or stop services if your app is running in the
> context of the limited "Users" group, and perhaps unelevated Admins in
> Vista+, but you can query if the service is running or not.

I can query by attempting to use a ref to it. That part isn't an issue.
Though, you do raise a good point about the permissions issue!

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


GS

3/9/2012 10:39:00 PM

0

MikeD wrote on 3/9/2012 :
>
> "GS" <gs@somewhere.net> wrote in message news:jjdm45$kv9$1@dont-email.me...
>> My apps rely on WMI for gathering hardware info. How would I start WMI if
>> not running, then stop it when I'm done using it?
>>
>
>
> The simplest way I can think of would be to use "net start" from the command
> line. You can run cmd.exe with the /c parameter followed by the net start
> command. For example (syntax may not be exactly right):
>
> cmd.exe /c net start "Windows Management Instrumentation"
>
> Similarly, to stop the service, use "net stop".
>
> Mike

Thanks, Mike. I assume you mean to do this via Shell or ShellExecute? I
was thinking there'd be API[s] involved but if it's that simple...

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


Jeff Johnson [MVP: VB]

3/9/2012 10:42:00 PM

0

"GS" <gs@somewhere.net> wrote in message news:jjdm45$kv9$1@dont-email.me...

> My apps rely on WMI for gathering hardware info. How would I start WMI if
> not running, then stop it when I'm done using it?

Wow, I tried finding this with a Google search and I got nothin'. Here's the
contents of a module I have. Copy and paste what is below into a text editor
and save it as "Services.bas" (or "basServices.bas" if you still do that
goofy Hungarian type-dangler thing). It will allow you to deal with the SCM.
(If you don't know what the SCM is, you have no business fiddling with
services...!)

--- Copy from the next line onwards ---
Attribute VB_Name = "basServices"
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



GS

3/9/2012 11:23:00 PM

0

Wow! Thanks, Jeff...
I'll definitely spend the time to digest this. Very much appreciated...

Also, thanks for confirming there's not much out there in Googleland.

BTW, I do use a fairly simple naming convention:

Attribute VB_Name = "mSCM_Services"

SaveAs = "SCM_Services.bas"

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


unknown

3/9/2012 11:56:00 PM

0

GS wrote:
> I don't get your inference to Vbnet being an 'obvious source' for
> Classic VB!!!

It's for classic VB despite the name, which was in use before VB.Net was
created, and perhaps where you got the WMI source code examples:

http://vbnet...

http://vbnet...code/network/enumservices.htm



GS

3/10/2012 2:30:00 AM

0

Farnsworth wrote on 3/9/2012 :
> GS wrote:
>> I don't get your inference to Vbnet being an 'obvious source' for
>> Classic VB!!!
>
> It's for classic VB despite the name, which was in use before VB.Net was
> created, and perhaps where you got the WMI source code examples:
>
> http://vbnet...
>
> http://vbnet...code/network/enumservices.htm

Thanks! I'm going to check these out...

Actually, I got the WMI code from scouring Excel forums for ways to
identify computers for app licensing. To enhance my use of it I got
Scriptomatic and WMICodeCreator to use for resources.

I got started with classic VB as a result of expanding my VBA to
COMAddins. My resource for getting started was F. Balena's "Programming
Visual Basic 6.0". There was mention of a website called "VB2TheMax"
which kinda disappeared into dotnet land. I also spent a lot of time
studying Brad Martinez's examples, and Karl Petersen's stuff.
Otherwise, I owe a lot of gratitude to many folks from these forums who
have contributed greatly to my growth in my quest to become a VB
programmer. I think I'm still a newbie in most respects and so it seems
like my journey is a very long one. Also, I have Lou Gehrig's and so
there's a limited amount of energy to invest each day, but I push
myself onward as best I can. That said, I'm extremely grateful when
people step up to assist my learning by any measure.

--
Garry

Free usenet access at http://www.eternal-sep...
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


MikeD

3/10/2012 12:27:00 PM

0



"GS" <gs@somewhere.net> wrote in message news:jje0pr$n5o$1@dont-email.me...
> MikeD wrote on 3/9/2012 :
>>
>> "GS" <gs@somewhere.net> wrote in message
>> news:jjdm45$kv9$1@dont-email.me...
>>> My apps rely on WMI for gathering hardware info. How would I start WMI
>>> if not running, then stop it when I'm done using it?
>>>
>>
>>
>> The simplest way I can think of would be to use "net start" from the
>> command line. You can run cmd.exe with the /c parameter followed by the
>> net start command. For example (syntax may not be exactly right):
>>
>> cmd.exe /c net start "Windows Management Instrumentation"
>>
>> Similarly, to stop the service, use "net stop".
>>
>> Mike
>
> Thanks, Mike. I assume you mean to do this via Shell or ShellExecute? I
> was thinking there'd be API[s] involved but if it's that simple...
>
> --

Yes. Use Shell.

There are API calls you can make (as per Jeff's reply), but I think this is
simpler. The only caveat, if you want to even call it that, is that a
command prompt window will open and close, but you could hide that.

And Farnsworth made the excellent point that I neglected to, which is that
the user must have permissions to start and stop services.

Mike


Mayayana

3/10/2012 2:24:00 PM

0

Also note:

WMI requires DCOM Server Process Launcher running.
There's also a WMI Driver Extensions service. DCOM
Server Process Launcher is not listed as a dependency.
I actually don't know the story on that. All I know is
that I usually disable as many services as possible, and
one time I found that when I re-enabled WMI for something
it wouldn't work. By process of elimination I found that
I had to also enable DCOM Server Process Launcher.

WMI can work remotely, but it doesn't need to, and I
wasn't using it that way. But it seems to be set up to
"consider itself hobbled" if it's not prepared to work
remotely. That's the only reason I can think of for it
needing DCOM Server Process Launcher.

It might be better to just show a message if WMI is
not enabled, for the following reasons:

1) WMI seems to be enmeshed with other services.

2) WMI *not* running is likely to be a rare occurence.

3) In any instance where WMI has been disabled it was
probably done deliberately for security reasons, so you
really have no right to re-enable it.