[Ray] How to Modify Windows Services Status in Legacy VisualBasic6 ?

Create a new VB6 project, then add Five controls into the Form1. 3 buttons , 1 listbox, 1  text box. all use default name, copy the code below , then press F5 to run the app. then you will see the result. user can modify the name then control the status of one services. 

tested in Windows XP sp2, works fine. 

Notes: If you want to transfer this article to another place, plz refer the original URL, 
http://nickong.cnblogs.com . Thanks. :)
  
Private Sub Command1_Click()
    
Dim ServiceSet
    
Dim Service
    
    List1.Clear
    
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_Service")
    
    
For Each Service In ServiceSet
        List1.AddItem Service.Name
    
Next
End Sub



Private Sub Command2_Click()
    
Dim ServiceSet
    
Dim Service

    
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Service where Name='" & Text1.Text & "'")
    
    
For Each Service In ServiceSet
       Service.StopService
       Service.ChangeStartMode (
"Manual")
    
Next
    
    
If ServiceSet.Count = 0 Then MsgBox "Sorry, No services found meeting query criteria."
        
End Sub



Private Sub Command3_Click()
    
Dim ServiceSet
    
Dim Service
    
    
Set ServiceSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select * from Win32_Service where Name='" & Text1.Text & "'")
    
    
For Each Service In ServiceSet
       Service.StartService
       Service.ChangeStartMode (
"Automatic")
    
Next
    
    
If ServiceSet.Count = 0 Then MsgBox "Sorry, No services found meeting query criteria."
End Sub



Private Sub List1_Click()
    Text1.Text 
= List1.Text  '(List1.ListIndex)
End Sub

posted @ 2007-07-23 12:15  RayG  阅读(266)  评论(0编辑  收藏  举报