|
|
|
This article will show you sample code to use ADSI to manage services.
Step-By-Step Example
- Create a new standard exe project - Add reference to "Active Ds Type Library" (Project->Add Reference) - Add windows common control 6.0 (Project->Controls) - Add one listview, five command button and one textbox control on the form - Add the following code in form1 |
Click here to copy the following block | Option Explicit
Const ADS_SERVICE_STOPPED = &H1 Const ADS_SERVICE_START_PENDING = &H2 Const ADS_SERVICE_STOP_PENDING = &H3 Const ADS_SERVICE_RUNNING = &H4 Const ADS_SERVICE_CONTINUE_PENDING = &H5 Const ADS_SERVICE_PAUSE_PENDING = &H6 Const ADS_SERVICE_PAUSED = &H7 Const ADS_SERVICE_ERROR = &H8
Const ADS_SERVICE_OWN_PROCESS = &H10 Const ADS_SERVICE_SHARE_PROCESS = &H20 Const ADS_SERVICE_KERNEL_DRIVER = &H1 Const ADS_SERVICE_FILE_SYSTEM_DRIVER = &H2
Const ADS_SERVICE_BOOT_START = &H0 Const ADS_SERVICE_SYSTEM_START = &H1 Const ADS_SERVICE_AUTO_START = &H2 Const ADS_SERVICE_DEMAND_START = &H3 Const ADS_SERVICE_DISABLED = &H4
Private Sub Command1_Click() Dim objAdsCont Dim objAdsService Dim objAdsServiceO Dim itmX As ListItem
Command1.Enabled = False Me.MousePointer = 11
ListView1.ListItems.Clear Set objAdsCont = GetObject("WinNT://" & Text1.Text & ",computer") If (IsEmpty(objAdsCont) = False) Then objAdsCont.Filter = Array("Service") For Each objAdsService In objAdsCont Set objAdsServiceO = objAdsService With objAdsService Set itmX = ListView1.ListItems.Add(, , .Name) itmX.SubItems(1) = .DisplayName itmX.SubItems(2) = GetStatusString(objAdsServiceO.Status) itmX.SubItems(3) = GetStartTypeString(.StartType) itmX.SubItems(4) = .ServiceAccountName End With Next objAdsService End If
Command1.Enabled = True Me.MousePointer = 0 End Sub
Private Sub Form_Load() Dim objNtSys As New WinNTSystemInfo
Command1.Caption = "Show Services" Command2.Caption = "Start" Command3.Caption = "Stop" Command4.Caption = "Pause" Command5.Caption = "Continue"
Text1.Text = objNtSys.ComputerName
With ListView1 .ListItems.Clear .LabelEdit = lvwManual With .ColumnHeaders .Clear .Add , , "Service Name", Me.TextWidth("Name" & Space(26)), lvwColumnLeft .Add , , "Description", Me.TextWidth("Description" & Space(50)), lvwColumnLeft .Add , , "Startup Type", Me.TextWidth("Startup" & Space(8)), lvwColumnLeft .Add , , "Status", Me.TextWidth("Status" & Space(4)), lvwColumnLeft .Add , , "Service Account", Me.TextWidth("Service Account" & Space(10)), lvwColumnLeft End With .View = lvwReport .FullRowSelect = True End With
Call SetActionButton(0, 0) End Sub
Private Sub SetActionButton(ByVal lngStatus As Long, ByVal lngStartType As Long)
Command2.Enabled = False Command3.Enabled = False Command4.Enabled = False Command5.Enabled = False
If lngStatus = ADS_SERVICE_RUNNING Then Command3.Enabled = True If lngStartType = ADS_SERVICE_AUTO_START Then Command4.Enabled = True End If ElseIf lngStatus = ADS_SERVICE_STOPPED Then Command2.Enabled = True ElseIf lngStatus = ADS_SERVICE_PAUSED Then Command5.Enabled = True End If End Sub
Private Function GetStartTypeString(ByVal lngStartType As Long) As String Select Case lngStartType Case ADS_SERVICE_BOOT_START GetStartTypeString = "Boot Start" Case ADS_SERVICE_SYSTEM_START GetStartTypeString = "System Start" Case ADS_SERVICE_AUTO_START GetStartTypeString = "Automatic" Case ADS_SERVICE_DEMAND_START GetStartTypeString = "Manual" Case ADS_SERVICE_DISABLED GetStartTypeString = "Disabled" Case Else GetStartTypeString = lngStartType & ": UNKNOWN" End Select End Function
Private Function GetStatusString(ByVal lngStatus As Long) As String Select Case lngStatus Case ADS_SERVICE_STOPPED GetStatusString = "SERVICE STOPPED" Case ADS_SERVICE_START_PENDING GetStatusString = "START PENDING" Case ADS_SERVICE_STOP_PENDING GetStatusString = "STOP PENDING" Case ADS_SERVICE_RUNNING GetStatusString = "SERVICE RUNNING" Case ADS_SERVICE_CONTINUE_PENDING GetStatusString = "CONTINUE PENDING" Case ADS_SERVICE_PAUSE_PENDING GetStatusString = "PAUSE PENDING" Case ADS_SERVICE_PAUSED GetStatusString = "ˆSERVICE PUSED" Case ADS_SERVICE_ERROR GetStatusString = "SERVICE ERROR" Case Else GetStatusString = lngStatus & ": UNKNOWN" End Select End Function
Private Sub ListView1_ItemClick(ByVal Item As MSComctlLib.ListItem) Dim objAdsService As IADsService Dim objAdsServiceO As IADsServiceOperations
Set objAdsService = GetObject("WinNT://" & Text1.Text & "/" & _ Item & ",Service") Set objAdsServiceO = objAdsService
Call SetActionButton(objAdsServiceO.Status, objAdsService.StartType)
Set objAdsService = Nothing Set objAdsServiceO = Nothing End Sub |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|