Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    Visual Basic




Enable/Disable Or Start/stop Window Services Using VBA


How can I Enable/Disable or start/stop Window services using VB6?

Best regards


Michael Sakellis
Athens-Greece


Michael Sakellis
Athens-Greece




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
How To Start And Stop Window's Services
I need help on getting the following code to run!

I have been testing some code from the "MS 2000 Scripting Guide". Its link is:
http://www.microsoft.com/resources/documentation/w...

The web site code with its preceding comments is as follows:


Scripting Steps
The scripts for stopping and starting dependent services perform similar steps but in the opposite order
Stopping dependent services

Listing 15.16 contains a script that stops the IIS Admin Service and all its dependents. To carry out this task, the script must perform the following steps:

1. Create a variable to specify the computer name.
2. Use a GetObject call to connect to the WMI namespace rootcimv2, and set the impersonation level to "impersonate."
3. Use the ExecQuery method to query the Win32_Service class.
This query must use an Associators of query and specify the following information:
The instance of the service on which the query is performed (Win32_Service.Name = 'IISAdmin').
The name of the Association class (AssocClass = Win32_DependentService). If the class name is not specified, the query returns all associated classes and their instances.
The role played by the IISAdmin Service. In this case, IISAdmin is Antecedent to the services to be returned by the query.
The query returns a collection consisting of all the services dependent on the IIS Admin Service.
4. For each service in the collection, use the StopService method to stop the service.
5. After a stop control has been sent to each dependent service, pause for 60 seconds (60,000 milliseconds) to give the SCM time to stop each service.
6. Use a the ExecQuery method to retrieve the instance of the IISAdmin Service.
7. Use the StopService method to stop the IISAdmin Service.

Listing 15.16 Stopping a Service and Its Dependents

CODE:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" & strComputer & "
ootcimv2")
Set colServiceList = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Service.Name='iisadmin'} WHERE " _
& "AssocClass=Win32_DependentService Role=Antecedent" )
For Each objService in colServiceList
errReturn = objService.StopService()
Next
Wscript.Sleep 60000
Set colServiceList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Service WHERE Name='iisadmin'")
For Each objService in colServiceList
errReturn = objService.StopService()
Next

I did not include the Start Services proceedure.

The Problem I am having:
Since I do not have the service 'iisadmin' I switched it in my test code to 'DHCP Client' (which, by the way, has no Dependent Services but I tested it on others that did).
When I ran the code the first occurrance of the following code line
"For Each objService in colServiceList"

got the following error message:

Run-time error
'-2147217406(80041002)':
Automation error

On the error line, in debug, the "objService" was "empty" when hovered over by the mouse pointer.

In my debugging process I tried to comment out various portions of code and other such things to no avail. I also tried to read up on the problem at the link and other places without success.

I would greatly appreciate help on how to get this to run.

How Can I Make A Program Enable/disable Windows Services?
First, I would like to explain why I am trying to make this program.

I am using a laptop connected to an Actiontec gateway via wireless connection, and it frequently gets dropped. I found a way to fix it; I startup, go to Control Panel> Administrative tools> Services, and stop the "Wireless Zero Configuration" service. Once I do that, my connection is perfect, with no flaws. The issue with that is, I need that service to be enabled in order to get the innitial connection. Once it is connected, I no longer need the service, and the service cause more harm than good. While this method works great, it gets very old after a while, so I figured, "If im starting to program, why not make one that does it for me?".

Anyways, I need to make a program, that waits until the service starts, waits until the computer gets connected to the internet, and then stops the service.

How would I do this?

-Thanks in advance!

How Do I Make A Program Enable/disable Windows Services?
First, I would like to explain why I am trying to make this program.

I am using a laptop connected to an Actiontec gateway via wireless connection, and it frequently gets dropped. I found a way to fix it; I startup, go to Control Panel> Administrative tools> Services, and stop the "Wireless Zero Configuration" service. Once I do that, my connection is perfect, with no flaws. The issue with that is, I need that service to be enabled in order to get the innitial connection. Once it is connected, I no longer need the service, and the service cause more harm than good. While this method works great, it gets very old after a while, so I figured, "If im starting to program, why not make one that does it for me?".

Anyways, I need to make a program, that waits until the service starts, waits until the computer gets connected to the internet, and then stops the service.

How would I do this?

-Thanks in advance!

Stop/Start NT Services
Can anyone tell me how to stop/start a service in NT? Thanks!

How To Start/stop NT Services?
I wants to start & stop service using my app.
Expecting a detailed soln as i m a beginner

:-)

Start, Stop, Restart Services
How do I start, stop, or restart services in Microsoft Common Console in Windows 2000/XP ? I often turning on or off some services there and need to make a shorter way.

Start And Stop Windows Services
This is my first day with MS Visual Studio 2005 Beta 2. I need to create 4 buttons, that start and stop 2 Windows services, and have some indications that the services ares started or stopped. So far i did the for you can see on the image. thats all though.

How can i make the buttons work, the progres bars show when a service is starting or stopping, and some kind of indication that the service is started or stopped? The commnds the buttons should send would be "net stop" and "net start" i guess. I'm currently working with batch files and thats how i do it.

Thanks for your help, and pls remember. Day 1. No previous programming experience whatsoever.

How To Start And Stop Services In NT O/s Using Application?
I wants to stop and start the NT services using
my App.
Thanx for great response on my Prev. query.

:-)

Start/stop Services On Windows 2000
Hello.
Anybody knows how can I start/stop a windows 2000 service using vb? I'd like to receibe from the user the name of the service to control.
Thanks!

Stop/Start/Restart Sites And Services On IIS
How do I make it where I can stop/start/restart anything that has to deal with IIS, note I am using IIS 6.0

Isaiah - Microsoft Visual Basic MVP

Start Stop Services And Monitor Results
Hello,
I have a question relating to starting and stopping of services via a vba.

I would like to know how to perform an if then event to stop a service and check the status while the service is being stopped. If the service has not stopped after XX time it will perform a shell execute of an external application to do a force kill.

I have written the force PID kill application but now want to do the stop of the service and if the service does not stop within a given amount of time, fire off the custom pid kill application.

I have the following sample script so far:
Code:sComputer = "testcomputer"    'replace with name of your computer
sService    = "ApacheApplicationsManager"        'replace with name of the service to be started/stopped

Set oInstance = GetObject("winmgmts:{impersonationLevel=impersonate}//" & sComputer & _
    "/root/cimv2:Win32_Service=" & Chr(34) & sService & Chr(34))
Set oOutParam = oInstance.ExecMethod_("StartService")



If oOutParam.ReturnValue = 0 Then
    WScript.Echo oInstance.Name & " started successfully"
Else
    'WScript.Echo oInstance.Name & " failed to start"
    Select Case oOutParam.ReturnValue
        Case 1    WScript.Echo "The request is not supported."
        Case 2    WScript.Echo "The user did not have the necessary access."
        Case 3    WScript.Echo "The service cannot be started because it depends " & _
                    "on other services that are not running."
        Case 4    WScript.Echo "The requested control code is not valid, or " & _
                    "it is unacceptable to the service."
        Case 5    WScript.Echo "The requested control code cannot be sent to " & _
                    "the service because the state of the service."
        Case 6    WScript.Echo "The service has not been started."
        Case 7    WScript.Echo "The service did not respond to the start request " & _
                    "in a timely fashion."
        Case 8    WScript.Echo "Unknown failure when starting the service."
        Case 9    WScript.Echo "The directory path to the service executable was not found."
        'Case 10    WScript.Echo "The service is already running"
        Case 11    WScript.Echo "The database to add a new service is locked."
        Case 12    WScript.Echo "A dependency for which this service relies on " & _
                    "has been removed from the system."
        Case 13    WScript.Echo "The service failed to find the service needed " & _
                    "from a dependent service."
        Case 14    WScript.Echo "The service has been disabled from the system."
        Case 15    WScript.Echo "The service does not have the correct authentication " & _
                    "to run on the system."
        Case 16    WScript.Echo "This service is being removed from the system."
        Case 17    WScript.Echo "There is no execution thread for the service."
        Case 18    WScript.Echo "There are circular dependencies when starting the service."
        Case 19    WScript.Echo "There is a service running under the same name."
        Case 20    WScript.Echo "There are invalid characters in the name of the service."
        Case 21    WScript.Echo "Invalid parameters have been passed to the service."
        Case 22    WScript.Echo "The account, which this service is to run under is " & _
                    "either invalid or lacks the permissions to run the service."
        Case 23    WScript.Echo "The service exists in the database of services " & _
                    "available from the system."
        Case 24    WScript.Echo "The service is currently paused in the system."
    End Select
End If

If oOutParam.ReturnValue = 10 Then
    Set oOutParam = oInstance.ExecMethod_("StopService")
    WScript.Echo oInstance.Name & " stopped successfully"
    End If

As you can tell this just fires off the stop, but does not check the current status and performs any do while loop to do any if then statements to go to the next procedure if the service status cannot be stopped.



Edited by - microcyb on 9/22/2005 10:31:51 AM

How Do I Enable/disable The Start Button
Hey everbody have have a little problem whit the START button, i have the code to disable the mouse but the keyboard still work. Do some one have the a code there disable the mouse and the keyboard!!!

Disable Visual Effects & Disable Services
How in VB6 can you disable visual effects? (Right-click My Computer > Properties > Advaned tab, under "Performance" click the "Settings" button and click the circle "Adjust for best performance")

Also, is it possible in VB6 to DISABLE certain Services?(Start > Run > services.msc)(I have already done a Search and all I can find is how to STOP a service--not DISABLE)

Even if it's a batch(.bat) file to do it I'd be happy

How can such be done via VB6?

Disable Visual Effects & Disable Services
How in VB6 can you disable visual effects? (Right-click My Computer > Properties > Advaned tab, under "Performance" click the "Settings" button and click the circle "Adjust for best performance")

Also, is it possible in VB6 to DISABLE certain Services?(Start > Run > services.msc)(I have already done a Search and all I can find is how to STOP a service--not DISABLE)

Even if it's a batch(.bat) file to do it I'd be happy

How can such be done via VB6?

Start Services With VB
hello, i want to start service from VB, can anybody help me ? how can i write the code ? is it with API ? thanks

Anyone With An Api To Disable Start Menu? Not Start Button
please

anyone with an api to disable start menu? Not start Button

Remotely Start Services
Is there a way to start services remotely on a network?

LAN Enable/disable
Help,

I am looking for the programming code equivalent of disabling or enabling the LAN connection on my PC. I have created a routine that I want to be able to share with my fellow workers, but during a portion of the routine I want the connection to the LAN disabled and as I wrap up the routine I want to restore the LAN to normal operation. Thanks in advance for your help.

Curt

Disable And Enable Help.
Can someone please help! I have a playlist application that I am writing (for college) and I need to enable or disable some buttons depending on the state of the listbox. I have one listbox called playlist and six buttons - add, remove, up, down, all and none.

The add button is always enabled and can be ignored here. The other rules are as follows:

If listbox contains no items all buttons are disabled.
If listbox contains items with none selected then all are disable except all button.
If all the listboxes items are selected then remove and none are enabled the rest are disabled.
If only one of the listbox items are selected the all buttons are enabled unless the top item is selected (up disabled) or the bottom item is a selected (down disabled).
If more than item is selected but not all the up and down are disabled the rest are enabled.

I have posted the code I am using currently below. It almost completely works however there are three problems (I've spotted so far).

1. The code only starts when a selection is changed. How can I make it run when a new item is added to the list also.
2. If I popluate the list and then delete all entries the all button should disable but it doesn't it stays enabled.
3. If I select all entries in one go and then remove them all together I get an error pointing to the If PlayList.GetSelected(x) = True line.

The code I use for the remove button is


Code:
Private Sub RemoveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveButton.Click
Do While PlayList.SelectedIndex <> -1
PlayList.Items.RemoveAt(PlayList.SelectedIndex)
Loop
End Sub
This is my rules code


Code:
Private Sub PlayList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayList.SelectedIndexChanged
If PlayList.Items.Count = 0 Then
RemoveButton.Enabled = False
UpButton.Enabled = False
DownButton.Enabled = False
AllButton.Enabled = False
NoneButton.Enabled = False
ElseIf PlayList.Items.Count > 0 And PlayList.SelectedIndex = -1 Then
RemoveButton.Enabled = False
UpButton.Enabled = False
DownButton.Enabled = False
AllButton.Enabled = True
NoneButton.Enabled = False
Else
If CheckAllSelected(PlayList) Then
RemoveButton.Enabled = True
UpButton.Enabled = False
DownButton.Enabled = False
AllButton.Enabled = False
NoneButton.Enabled = True
ElseIf NumberSelected > 1 Then
RemoveButton.Enabled = True
UpButton.Enabled = False
DownButton.Enabled = False
AllButton.Enabled = True
NoneButton.Enabled = True
Else
If PlayList.Items.Count = 1 Then
RemoveButton.Enabled = True
UpButton.Enabled = False
DownButton.Enabled = True
AllButton.Enabled = True
NoneButton.Enabled = True
Else
Select Case PlayList.SelectedIndex
Case 0
RemoveButton.Enabled = True
UpButton.Enabled = False
DownButton.Enabled = True
AllButton.Enabled = True
NoneButton.Enabled = True
Case PlayList.Items.Count - 1
RemoveButton.Enabled = True
UpButton.Enabled = True
DownButton.Enabled = False
AllButton.Enabled = True
NoneButton.Enabled = True
Case Else
RemoveButton.Enabled = True
UpButton.Enabled = True
DownButton.Enabled = True
AllButton.Enabled = True
NoneButton.Enabled = True
End Select
End If
End If
End If
End Sub

Public Function CheckAllSelected(ByVal PlayList As ListBox)
Dim x As Integer
NumberSelected = 0
For x = 0 To PlayList.Items.Count - 1
If PlayList.GetSelected(x) = True Then
NumberSelected = NumberSelected + 1
Else
Exit Function
End If
Next
If NumberSelected = PlayList.Items.Count Then
CheckAllSelected = True
Else
CheckAllSelected = False
End If
End Function
I am sorry if this is a bit too long. Any help will be greatly appreciated. I have alos attached all of my code to this posting.

Thanks
Stephen

Disable/Enable X
Hi, I'd like to know how to undo this:


Code:
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long

Private Sub Form_Load()
Dim sysmen As Long
sysmen = GetSystemMenu(Me.hWnd, 0)
Call DeleteMenu(sysmen, &HF060&, &H0&)
End Sub
The code disables the X button and removes the close menu item. My question is... how do I put it back?

Disable/enable Alt Only
Hello Gurus

How to disable/enable Alt button ONLY on WinXP?

Thanks

Enable / Disable NIC
I want to know how to enable and disable the local area connection or the network card.

Basically, I am looking for an easy way to turn off my "always on" dsl without only clicking one button...I was thinking of having an app sit in the task tray that I can click to turn the connection on and off.

Any ideas are appreciated. And no, I don't want to turn the modem off since the connection is shared across more than one computer.

Enable/Disable NIC
Anyone have any code samples on how to enable/disable Network Adaptors?

Enable/disable
i'd like to secure my program. only me can use the other forms and commands. everytime they will logged as guest the they cannot use the other commands. how can i do it?

Disable/Enable LAN
Hello
I am new to VBCity
I need a help. Hope you guys can help me. I am using Windows XP and using LAN (Not wireless) we can disable/enable lan by right click on Network Places ---> Properties and right click to Local Area Network and click on Disable/Enable. I want to do this through Visual Basic 6. Please give the code of it.

Thanks



Edited by - jonybhi on 1/6/2006 10:54:36 PM

To List,start, Add &amp; Close Services?
Is it possible to list,start, add & Close services and them full path?

For exemple, when i execute in cmd net start
it gives me the services that are running but without path to the exe/dll program.

can this be solved?

Any exemple would be very usefull.
Thanks

Enable / Disable LAN Connections
Hi guys

someone could help me how to enable/disable a local area connection using some code or an api in VB?

Thank you

Tab Strip - Enable/disable
Hello there!

Im doing a project for college in visual basic 6.0. But im having trouble with the tab strip that can be found under "components/Microsoft Tabbed Dialog control 6.0" But the thing is i have a tab that i want to be disabled until the program executes a command. When i have tryed to do this the whole tab strip gets disabled and i only need the one...

can anyone give me a hand with what i would code?

- regards Tilston

Enable/disable Proxy. Anyone Know How?
Hi!

I'm looking for a way to create a tiny small app with two buttons to enable and disable proxy settings. I want this app to be on the desktop when it's run so that the users always see these two buttons.
Like this?

http://img78.imageshack.us/img78/4930/proxy4rf.jpg

Anyone know how to do this?

Enable/disable TextBox
Hi, I'm wondering how to enable a TextBox as soon as a letter, number or other character is typed in another TextBox.

TextBox1 = Enabled
TextBox2 = Disabled

How can I enable TextBox2 when something is typed in TextBox1 ?
And how to disable TextBox2 again when TextBox1 is cleared ?

Thanks.

Enable Or Disable IE Proxy
I am trying to figure out how to effectively change the IE Proxy Enable setting, and also make it take effect.

I have code similar to the following that correctly sets the registry value:

Code:
Registry.SetKeyValue HKEY_CURRENT_USER, _
"SoftwareMicrosoftWindowsCurrentVersionInternet Settings", "ProxyEnable", _
IIf(bolEnabled, "0", "1"), "Dword"
However, IE doesn't always recognize the change. As well, IE windows that are already open ignore any change of setting.

When you go into Internet Options, and enable/disable the proxy from there, I think it goes through some settings refresh procedure and then ALL browser windows will recognize the changed settings. This does not happen if I simply change the Registry key.

Does anyone know how I can make all the IE windows recognize the changed setting after I change it? I have searched PSC, Google, etc. and I have been unable to figure this out for a while now. Thanks

DHCP Enable/Disable
Would anyone know if there is a way to enable/disable DHCP in VB 6.0??? If so could you point me to the right direction. Thanks


Reason why : The reason why I would want to this for my laptop... My home network (LAN) is on DHCP and at work I use Static addressing. So to save mysef some time everyday I am writing a program to change the Enable/Disable DHCP and when DHCP is Disabled to assign my Static IP,Subnet,Gateway,DNS1,DNS2 and ESSID to my laptop. So far everything is going good(i think) on this (sense I am new to VB).

Enable/Disable Problems!
I have been trying to make it to where when:

check2.value = 1 = true then check1.enabled = false

and also

check2.value = 0 = true then check1.enabled = true

so i came up with this:

Private Sub Form_Activate()
If Check2.Value = 1 = True Then Check1.Enabled = False
If Check2.Value = 0 = True Then Check1.Enabled = True
End Sub

Which as I can tell is'nt right but I am not sure what to do, I have tryed many different things but nothing seems to work. Thanks ahead of time!

Enable/Disable An Index
I am trying to reset some tables in a database. When I tried to use Truncate ( Delete takes longer time and there is also transaction log issue which takes space on server), I realized that one of the tables has an Index, so naturally Truncate fails for that table.

So I wonder if there is a way to disable Indexes so I can Truncate such tables. Ofcourse I need to enable them after truncation.

BTW, The Database Server in question is MSDE.

Enable/disable Keyboard
I want to make a vb script which can enable or disable my keyboard.
which functions schould I use ?
thanks.

Enable/disable Hardware
hello, i have a question
is it possible to enable/disable a network adapter via programming?
10q for your time.

Enable/disable Monitor
If i got a system with 2 monitors (e.g. the default monitor and a tv-out), how could i using code enable the 2nd monitor? Been googling all evening, but i can't find a proper API to enable this.

Cheers,
Christopher

Enable Disable Frame VB6
Hi guys,
This is a shorty.
I have two opt buttons in array, two frames in an array.
if opt(0) is selected then only frame(0) should be enabled and vice versa. Somehow Iam not getting the code Can somebody post a snippet.

Disable Or Enable A Hardware
hi there,

is it any method to use VB6 to disable a hardware and after that enable it?


Regards.

Enable/Disable Devices
Hi!

Is there a way to enable/disable devices just like the Device Manager through code?? Either VB6 or VB.NET or API?

Thanx!

How To Disable And Enable A Lan Connection?
How To Disable and enable a Lan Connection?

Disable/Enable Keyboard
I know how to disable the keyboard and then be able to enable it again without reset?


Code:
Shell "rundll32.exe keyboard,disable", vbNormalFocus

i know that disables it but it wont enable it. Anyone know?

Disable And Enable Label
If anyone know, on how to disable and Enable a label on Data Report upon Datareport1.PrintReport ?

Form_Resize - Disable/enable
How can the resizing of a form be enable and disabled at run time?

Enable/Disable ScreenSaver...?
can anybody help me with an example of how to do that through code?

Disable And Enable Mouse
regarding to the subject, may i know how to use API to disable the mouse and enable it back.
Thanks

Enable / Disable Autorun
Hi,

Can anyone help me with a method to disable and enable the autorun of CD-Rom's via code?

I would also like to be able to check what
the user's current setting of Autorun is, and be able to restore it when exiting my prog.

Thanks

Enable / Disable Autorun
Hi,

Can anyone help me with a method to disable and enable the autorun of CD-Rom's via code?

I would also like to be able to check what
the user's current setting of Autorun is, and be able to restore it when exiting my prog.

Thanks

Disable And Enable Button
Hi All,

I have a button with the code...

Private Sub Send_Click()
Send.Enabled = False
ActiveWorkbook.SendMail "MYEMAIL ADDRESS", _
"Request"
End Sub

So, after one click, the button gets disabled and an e-mail is sent. What I would like to do is, if any of the following Cells, A1-A3 & C1 - C3 are changed, the button is enabled again. Any ideas?

Copyright 2005-08 www.BigResource.com, All rights reserved