[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Enabling/Disable a service on MSCONFIG using VB 6.0 , 500 Points!

Posted on 2007-10-07
7
Medium Priority
?
1,996 Views
Last Modified: 2013-12-26
Hi, take a moment to my example , --> Click the Start button , then click "run" and open the file "msconfig" , then click on Services, there you should see many applications , now what i need to do is ... to find a way so i can Disable/Enable a service using visual basic 6.0 .

Thanks
0
Comment
Question by:KingSencat
7 Comments
 
LVL 8

Expert Comment

by:Anthony2000
ID: 20030133
Question was asked before, here is an answer:

http://www.experts-exchange.com/Programming/Misc/Q_21050127.html
0
 
LVL 6

Expert Comment

by:PhilAI
ID: 20030144
I'd start looking at the ControlService API:
http://msdn2.microsoft.com/en-us/library/ms682108.aspx

Not used this myself, but I've heard it should allow you to stop and start services and this should help you:
http://www.techbytes.ca/techbyte16.html
0
 

Author Comment

by:KingSencat
ID: 20030153
Anthony2000: Doesn't help too much , can you paste me here a sample code ?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KingSencat
ID: 20030211
I just need a code so i can STOP A SERVICE and RUN A SERVICE , this is all what i need , please help me
0
 
LVL 8

Expert Comment

by:Anthony2000
ID: 20030249
KingSencat, I found an example on the web that uses the service control manager api:
I would test if for you, but I don't have VB6 on this machine. Its on my notebook at work.

In summary:


this is the function that you will call (from below):

dim result as boolean

result = ServiceCommand("service name",  SERVICE_CONTROL_STOP) ' disable service or stop it

result = ServiceCommand("service name",  SERVICE_CONTROL_START) ' enable service or start it

Let me know if this worked.








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

Private Declare Function OpenSCManager Lib "advapi32.dll" Alias _
"OpenSCManagerA" (ByVal lpMachineName As String, _
ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject _
As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" _
(ByVal hSCManager As Long, ByVal lpServiceName As String, _
ByVal dwDesiredAccess As Long) As Long
Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" _
(ByVal hService As Long, ByVal dwNumServiceArgs As Long, _
ByVal lpServiceArgVectors As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As _
Long, ByVal dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long

Const GENERIC_EXECUTE = &H20000000
Const SERVICE_CONTROL_STOP = 1
Const SERVICE_CONTROL_PAUSE = 2
Const SERVICE_CONTROL_CONTINUE = 3

' start/stop/pause/continue a service
' SERVICENAME is the internal name of the service
' COMMAND can be 0=Start, 1=Stop, 2=Pause, 3=Continue
'
' returns True if successful, False otherwise
' if any error, call Err.LastDLLError for more information

Function ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _
Boolean
Dim hSCM As Long
Dim hService As Long
Dim res As Long
Dim lpServiceStatus As SERVICE_STATUS

' first, check the command
If command < 0 Or command > 3 Then Err.Raise 5

' open the connection to Service Control Manager, exit if error
hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_EXECUTE)
If hSCM = 0 Then Exit Function



' open the given service, exit if error
hService = OpenService(hSCM, ServiceName, GENERIC_EXECUTE)
If hService = 0 Then GoTo CleanUp

' start the service
Select Case command
Case 0
' to start a service you must use StartService
res = StartService(hService, 0, 0)
Case SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, _
SERVICE_CONTROL_CONTINUE
' these commands use ControlService API
' (pass a NULL pointer because no result is expected)
res = ControlService(hService, command, lpServiceStatus)
End Select
If res = 0 Then GoTo CleanUp

' return success
ServiceCommand = True

CleanUp:
If hService Then CloseServiceHandle hService
' close the SCM
CloseServiceHandle hSCM

End Function
0
 
LVL 33

Accepted Solution

by:
Jeroen Rosink earned 2000 total points
ID: 20030415
perhaps this works also in VB

http://msdn2.microsoft.com/en-us/library/aa394602.aspx

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where Name ='Alerter'") 'Enter here the service Name
For Each objService in colListOfServices
    objService.StartService()
    Wscript.Echo "Started Alerter service"
Next


In addition to stop a service use instead of StartService()
StopService()

I hope this works for you
regards,
Jeroen
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 20031134
@roos01 and KingSencat,

that code will work (and is probably the simplest), as long as you have a project reference to the Microsoft Scripting Runtime Library added.

Regards,

Rob.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question