home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD174683232001.psc / frmMain.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2001-03-23  |  18.3 KB  |  467 lines

  1. VERSION 5.00
  2. Object = "{0BA686C6-F7D3-101A-993E-0000C0EF6F5E}#1.0#0"; "threed32.ocx"
  3. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  4. Begin VB.Form frmMain 
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Services"
  7.    ClientHeight    =   3990
  8.    ClientLeft      =   2250
  9.    ClientTop       =   4095
  10.    ClientWidth     =   7635
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   3990
  15.    ScaleWidth      =   7635
  16.    Begin MSComctlLib.ListView lvServices 
  17.       Height          =   3795
  18.       Left            =   0
  19.       TabIndex        =   6
  20.       Top             =   195
  21.       Width           =   6015
  22.       _ExtentX        =   10610
  23.       _ExtentY        =   6694
  24.       LabelWrap       =   -1  'True
  25.       HideSelection   =   -1  'True
  26.       _Version        =   393217
  27.       ForeColor       =   -2147483640
  28.       BackColor       =   -2147483643
  29.       BorderStyle     =   1
  30.       Appearance      =   1
  31.       NumItems        =   0
  32.    End
  33.    Begin Threed.SSCommand cmdClose 
  34.       Cancel          =   -1  'True
  35.       Default         =   -1  'True
  36.       Height          =   450
  37.       Left            =   6060
  38.       TabIndex        =   0
  39.       Top             =   0
  40.       Width           =   1560
  41.       _Version        =   65536
  42.       _ExtentX        =   2752
  43.       _ExtentY        =   794
  44.       _StockProps     =   78
  45.       Caption         =   "Close"
  46.    End
  47.    Begin Threed.SSCommand cmdStart 
  48.       Height          =   450
  49.       Left            =   6060
  50.       TabIndex        =   1
  51.       Top             =   705
  52.       Width           =   1560
  53.       _Version        =   65536
  54.       _ExtentX        =   2752
  55.       _ExtentY        =   794
  56.       _StockProps     =   78
  57.       Caption         =   "&Start"
  58.    End
  59.    Begin Threed.SSCommand cmdStop 
  60.       Height          =   450
  61.       Left            =   6060
  62.       TabIndex        =   2
  63.       Top             =   1425
  64.       Width           =   1560
  65.       _Version        =   65536
  66.       _ExtentX        =   2752
  67.       _ExtentY        =   794
  68.       _StockProps     =   78
  69.       Caption         =   "S&top"
  70.    End
  71.    Begin Threed.SSCommand cmdPause 
  72.       Height          =   450
  73.       Left            =   6060
  74.       TabIndex        =   3
  75.       Top             =   2130
  76.       Width           =   1560
  77.       _Version        =   65536
  78.       _ExtentX        =   2752
  79.       _ExtentY        =   794
  80.       _StockProps     =   78
  81.       Caption         =   "&Pause"
  82.    End
  83.    Begin Threed.SSCommand cmdContinue 
  84.       Height          =   450
  85.       Left            =   6060
  86.       TabIndex        =   4
  87.       Top             =   2835
  88.       Width           =   1560
  89.       _Version        =   65536
  90.       _ExtentX        =   2752
  91.       _ExtentY        =   794
  92.       _StockProps     =   78
  93.       Caption         =   "&Continue"
  94.    End
  95.    Begin Threed.SSCommand cmdStartup 
  96.       Height          =   450
  97.       Left            =   6060
  98.       TabIndex        =   5
  99.       Top             =   3540
  100.       Width           =   1560
  101.       _Version        =   65536
  102.       _ExtentX        =   2752
  103.       _ExtentY        =   794
  104.       _StockProps     =   78
  105.       Caption         =   "Sta&rtup"
  106.    End
  107.    Begin VB.Label Label3 
  108.       Alignment       =   2  'Center
  109.       Caption         =   "Startup"
  110.       Height          =   270
  111.       Left            =   4575
  112.       TabIndex        =   9
  113.       Top             =   15
  114.       Width           =   1140
  115.    End
  116.    Begin VB.Label Label2 
  117.       Alignment       =   2  'Center
  118.       Caption         =   "Status"
  119.       Height          =   270
  120.       Left            =   3150
  121.       TabIndex        =   8
  122.       Top             =   15
  123.       Width           =   1425
  124.    End
  125.    Begin VB.Label Label1 
  126.       Caption         =   "Name"
  127.       Height          =   270
  128.       Left            =   15
  129.       TabIndex        =   7
  130.       Top             =   15
  131.       Width           =   3135
  132.    End
  133. Attribute VB_Name = "frmMain"
  134. Attribute VB_GlobalNameSpace = False
  135. Attribute VB_Creatable = False
  136. Attribute VB_PredeclaredId = True
  137. Attribute VB_Exposed = False
  138. 'Public colServices As New Collection
  139. Public liPreviousItem As ListItem
  140. Public bDoOnce As Boolean
  141. Private Sub Form_Load()
  142.     Call SetupServiceListView
  143.     Call GetServiceList
  144.     Call StoreServicesInListView
  145.     bDoOnce = True
  146. End Sub
  147. Private Sub Form_Activate()
  148.     If bDoOnce Then
  149.         'Select the first item in the list view
  150.         Set lvServices.SelectedItem = lvServices.ListItems(1)
  151.         Call lvServices_ItemClick(lvServices.SelectedItem)
  152.         Set liPreviousItem = lvServices.SelectedItem
  153.         lvServices.SetFocus
  154.         bDoOnce = False
  155.     End If
  156. End Sub
  157. Private Sub cmdStart_Click()
  158.     Dim liCurrentItem As ListItem
  159.     Dim sDependencies As String
  160.     Dim sSrvcName As String
  161.     Dim sServiceList() As String
  162.     Dim iCount As Integer
  163.     Dim i As Integer
  164.     Set liCurrentItem = lvServices.SelectedItem
  165.     iCount = 0
  166.     'This can get a little tricky.  We are going to attempt to duplicate a feature
  167.     'found in Microsoft's Service Manager.  We are going to travel up the
  168.     'dependency tree, starting each service we come to.
  169.     'Check if the service is dependent on another service, don't worry about groups
  170.     'at the moment.  For those of you that don't know, certain services can be
  171.     'part of a defined group (sLoadOrderGroup in our class).  A service might very
  172.     'well depend on an entire group of services to load first.  In this version
  173.     'I am not dealing with groups at all.
  174.     sDependencies = colServices(liCurrentItem.Key).Dependencies
  175.     sSrvcName = colServices(liCurrentItem.Key).ServiceName
  176.     Do While Not sDependencies = "" And InStr(1, sDependencies, SC_GROUP_IDENTIFIER) = 0
  177.         'Lets store the current item in the array as the very first item
  178.         If iCount = 0 Then
  179.             ReDim sServiceList(iCount) As String
  180.             sServiceList(iCount) = sSrvcName
  181.         End If
  182.         
  183.         iCount = iCount + 1
  184.         ReDim Preserve sServiceList(iCount) As String
  185.         sServiceList(iCount) = sDependencies
  186.         sDependencies = colServices(sDependencies).Dependencies
  187.     Loop
  188.     If iCount > 0 Then
  189.         'Now we need to start each service going backwards through the array
  190.         For i = iCount To 0 Step -1
  191.             Call StartServiceByName(UCase(sServiceList(i)))
  192.         Next i
  193.     Else
  194.         Call StartServiceByName(UCase(sSrvcName))
  195.     End If
  196.     Set liCurrentItem = Nothing
  197. End Sub
  198. Private Sub cmdStop_Click()
  199.     Dim liCurrentItem As ListItem
  200.     Dim lServiceStatus As Long
  201.     Dim sHoldText As String
  202.     Dim tStartTime As Date
  203.     Dim bResult As Boolean
  204.     Dim service As CServices
  205.     Dim sMessage As String
  206.     Dim iCount As Integer
  207.     Dim lResult As Long
  208.     Set liCurrentItem = lvServices.SelectedItem
  209.     'Check if the service in question has any dependent services
  210.     bResult = GetDependentInfo("", colServices(liCurrentItem.Key).ServiceName)
  211.     If bResult Then
  212.         'Check if the user is sure they want to stop all services
  213.         sMessage = colServices(liCurrentItem.Key).ServiceName & " has the following dependent service(s) that also need to be stopped:  " & vbCrLf & vbCrLf
  214.         
  215.         iCount = 0
  216.         For Each service In colDepServices
  217.             iCount = iCount + 1
  218.             If iCount = colDepServices.Count Then
  219.                 'We are on the last one
  220.                 sMessage = sMessage & service.DisplayName & vbCrLf
  221.             Else
  222.                 sMessage = sMessage & service.DisplayName & vbCrLf
  223.             End If
  224.         Next
  225.         
  226.         sMessage = sMessage & vbCrLf & "Are you sure you wish to stop all the above services?"
  227.         lResult = MsgBox(sMessage, vbYesNo + vbQuestion, "Stopping")
  228.         If lResult = vbYes Then
  229.             'Go ahead and stop services
  230.                 
  231.             'The colDepServices should be filled with dependent service info now.
  232.             For Each service In colDepServices
  233.                 'Is the service running?
  234.                 If colServices(UCase(service.ServiceName)).CurrentStatus = SERVICE_RUNNING Then
  235.                     'The service is running, lets stop it
  236.                     Call StopServiceByName(UCase(service.ServiceName))
  237.                 End If
  238.             Next
  239.             
  240.             'Now stop the main service
  241.             Call StopServiceByName(UCase(colServices(liCurrentItem.Key).ServiceName))
  242.         Else
  243.             'Don't stop services.. exit
  244.             Exit Sub
  245.         End If
  246.     Else
  247.         'Check if the user is sure they want to stop all services
  248.         sMessage = "Are you sure you want to stop the " & colServices(liCurrentItem.Key).ServiceName & " service?"
  249.         
  250.         lResult = MsgBox(sMessage, vbYesNo + vbQuestion, "Services")
  251.         If lResult = vbYes Then
  252.             Call StopServiceByName(UCase(colServices(liCurrentItem.Key).ServiceName))
  253.         End If
  254.     End If
  255.     Set liCurrentItem = Nothing
  256. End Sub
  257. Private Sub cmdPause_Click()
  258.     Dim liCurrentItem As ListItem
  259.     Dim sMessage As String
  260.     Dim lResult As Long
  261.     Set liCurrentItem = lvServices.SelectedItem
  262.     sMessage = "Are you sure you want to pause the " & colServices(liCurrentItem.Key).ServiceName & " service?"
  263.     lResult = MsgBox(sMessage, vbYesNo, "Services")
  264.     If lResult = vbYes Then
  265.         Call PauseServiceByName(UCase(colServices(liCurrentItem.Key).ServiceName))
  266.     End If
  267.     Set liCurrentItem = Nothing
  268. End Sub
  269. Private Sub cmdContinue_Click()
  270.     Dim liCurrentItem As ListItem
  271.     Set liCurrentItem = lvServices.SelectedItem
  272.     Call ContinueServiceByName(UCase(colServices(liCurrentItem.Key).ServiceName))
  273. End Sub
  274. Private Sub cmdStartup_Click()
  275.     frmStartup.Show vbModal
  276.     lvServices.SetFocus
  277. End Sub
  278. Private Sub lvServices_ItemClick(ByVal Item As MSComctlLib.ListItem)
  279.     Call subSetupButtons(Item.Key)
  280. End Sub
  281. Private Sub lvServices_KeyDown(KeyCode As Integer, Shift As Integer)
  282.     If KeyCode = vbKeyDown Then
  283.         If Not lvServices.SelectedItem.Index = lvServices.ListItems.Count Then
  284.             'If we are at the end of the list we should not move
  285.             Call subSetupButtons(lvServices.ListItems(lvServices.SelectedItem.Index + 1).Key)
  286.         End If
  287.     End If
  288.     If KeyCode = vbKeyUp Then
  289.         If Not lvServices.SelectedItem.Index = 1 Then
  290.             'If we are at the beginning of the list we should not move
  291.             Call subSetupButtons(lvServices.ListItems(lvServices.SelectedItem.Index - 1).Key)
  292.         End If
  293.     End If
  294. End Sub
  295. Private Sub cmdClose_Click()
  296.     Unload Me
  297. End Sub
  298. Private Sub SetupServiceListView()
  299.     'This routine configures the column headings for the Report view mode
  300.     Dim lLVWidth As Long
  301.     Dim LVHeader As ColumnHeader
  302.     lLVWidth = lvServices.Width - 5 * Screen.TwipsPerPixelX
  303.     Set LVHeader = lvServices.ColumnHeaders.Add(1, , "Name", lLVWidth * 0.55)
  304.     Set LVHeader = lvServices.ColumnHeaders.Add(2, , "Status", lLVWidth * 0.2, vbCenter)
  305.     Set LVHeader = lvServices.ColumnHeaders.Add(3, , "Startup", lLVWidth * 0.2, vbCenter)
  306.     lvServices.Sorted = True
  307.     lvServices.View = lvwReport
  308.     lvServices.FullRowSelect = True
  309.     lvServices.HideColumnHeaders = True
  310.     lvServices.LabelEdit = lvwManual
  311.     Set LVHeader = Nothing
  312. End Sub
  313. Private Sub GetServiceList()
  314.     Dim bResult As Boolean
  315.     bResult = EnumerateServices("", SERVICE_STATE_ALL, SERVICE_WIN32)
  316.     If Not bResult Then
  317.         'Something went wrong enumerating services
  318.         End
  319.     End If
  320. End Sub
  321. Private Sub StoreServicesInListView()
  322.     Dim CurrentService As CServices
  323.     Dim liCurrentItem As ListItem
  324.     'Loop through the service collection and add each item to the list view
  325.     For Each CurrentService In colServices
  326.         'SubItems stores data that will be displayed when in Report view
  327.         Set liCurrentItem = lvServices.ListItems.Add(, UCase(CurrentService.ServiceName), CurrentService.DisplayName)
  328.         
  329.         'The following two case statements fill in the subitem information and
  330.         'change the forecolor of the service line depending on it's state.
  331.         Select Case CurrentService.CurrentStatus
  332.             Case SERVICE_STOPPED
  333.                 liCurrentItem.SubItems(1) = ""
  334.                 liCurrentItem.ForeColor = vbRed
  335.             Case SERVICE_RUNNING
  336.                 liCurrentItem.SubItems(1) = "Started"
  337.                 liCurrentItem.ForeColor = vbBlack
  338.             Case SERVICE_PAUSED
  339.                 liCurrentItem.SubItems(1) = "Paused"
  340.                 liCurrentItem.ForeColor = vbGreen
  341.         End Select
  342.         
  343.         Select Case CurrentService.StartType
  344.             Case SERVICE_BOOT_START
  345.                 liCurrentItem.SubItems(2) = "Boot"
  346.             Case SERVICE_SYSTEM_START
  347.                 liCurrentItem.SubItems(2) = "System"
  348.             Case SERVICE_AUTO_START
  349.                 liCurrentItem.SubItems(2) = "Automatic"
  350.             Case SERVICE_DEMAND_START
  351.                 liCurrentItem.SubItems(2) = "Manual"
  352.             Case SERVICE_DISABLED
  353.                 liCurrentItem.SubItems(2) = "Disabled"
  354.                 liCurrentItem.ForeColor = vbGrayText
  355.         End Select
  356.     Next
  357. End Sub
  358. Private Sub subSetupButtons(sKey As String)
  359.     'This sub routine sets up the buttons depending on the currently selected
  360.     'servies status.
  361.     cmdStart.Enabled = Not colServices(sKey).Running And Not CBool((colServices(sKey).StartType And SERVICE_DISABLED))
  362.     cmdStop.Enabled = colServices(sKey).AcceptStop And colServices(sKey).Running
  363.     cmdPause.Enabled = colServices(sKey).AcceptPause And colServices(sKey).Running And Not colServices(sKey).Paused
  364.     cmdContinue.Enabled = colServices(sKey).Paused
  365. End Sub
  366. Private Sub StartServiceByName(sServiceName As String)
  367.     Dim liCurrentItem As ListItem
  368.     Set liCurrentItem = lvServices.ListItems(sServiceName)
  369.     'Don't try and start the service if it's already started
  370.     If Not colServices(sServiceName).Running Then
  371.         'sHoldText = sbmain.Panels("MAIN").Text
  372.         Me.MousePointer = vbHourglass
  373.         
  374.         'Start the selected service
  375.         ServiceStart "", colServices(liCurrentItem.Key).ServiceName
  376.         
  377.         'Set global variables for Service Control dialog box
  378.         SERVICE_CONTROL_MSGBOX_MESSAGE = "Attempting to start the " & colServices(liCurrentItem.Key).DisplayName & " service."
  379.         SERVICE_CONTROL_WAITTIME = 3000
  380.         SERVICE_CONTROL_MSGBOX_TITLE = "Service Control"
  381.         SERVICE_CONTROL_MACHINENAME = ""
  382.         SERVICE_CONTROL_SERVICENAME = colServices(liCurrentItem.Key).ServiceName
  383.         SERVICE_CONTROL_STATUS_WANTED = SERVICE_RUNNING
  384.         frmSCMsgBox.Show vbModal
  385.         
  386.         colServices(liCurrentItem.Key).CurrentStatus = SERVICE_RUNNING
  387.         Call subSetupButtons(liCurrentItem.Key)
  388.         Me.MousePointer = vbDefault
  389.         
  390.         lvServices.SetFocus
  391.     End If
  392.     Set liCurrentItem = Nothing
  393. End Sub
  394. Private Sub StopServiceByName(sServiceName As String)
  395.     Dim liCurrentItem As ListItem
  396.     Set liCurrentItem = lvServices.ListItems(sServiceName)
  397.     'Don't try and start the service if it's already started
  398.     If Not colServices(sServiceName).CurrentStatus = SERVICE_STOPPED Then
  399.         Me.MousePointer = vbHourglass
  400.         ServiceStop "", sServiceName
  401.             
  402.         'Set global variables for Service Control dialog box
  403.         SERVICE_CONTROL_MSGBOX_MESSAGE = "Attempting to stop the " & colServices(liCurrentItem.Key).DisplayName & " service."
  404.         SERVICE_CONTROL_WAITTIME = 3000
  405.         SERVICE_CONTROL_MSGBOX_TITLE = "Service Control"
  406.         SERVICE_CONTROL_MACHINENAME = ""
  407.         SERVICE_CONTROL_SERVICENAME = sServiceName
  408.         SERVICE_CONTROL_STATUS_WANTED = SERVICE_STOPPED
  409.         frmSCMsgBox.Show vbModal
  410.         
  411.         'colServices(liCurrentItem.Key).CurrentStatus = SERVICE_RUNNING
  412.         Call subSetupButtons(liCurrentItem.Key)
  413.         Me.MousePointer = vbDefault
  414.         
  415.         lvServices.SetFocus
  416.     End If
  417.     Set liCurrentItem = Nothing
  418. End Sub
  419. Private Sub PauseServiceByName(sServiceName As String)
  420.     Dim liCurrentItem As ListItem
  421.     Set liCurrentItem = lvServices.ListItems(sServiceName)
  422.     'The service must be running to pause it
  423.     If colServices(sServiceName).CurrentStatus = SERVICE_RUNNING Then
  424.         Me.MousePointer = vbHourglass
  425.         ServicePause "", sServiceName
  426.             
  427.         'Set global variables for Service Control dialog box
  428.         SERVICE_CONTROL_MSGBOX_MESSAGE = "Attempting to pause the " & colServices(liCurrentItem.Key).DisplayName & " service."
  429.         SERVICE_CONTROL_WAITTIME = 3000
  430.         SERVICE_CONTROL_MSGBOX_TITLE = "Service Control"
  431.         SERVICE_CONTROL_MACHINENAME = ""
  432.         SERVICE_CONTROL_SERVICENAME = sServiceName
  433.         SERVICE_CONTROL_STATUS_WANTED = SERVICE_PAUSED
  434.         frmSCMsgBox.Show vbModal
  435.         
  436.         Call subSetupButtons(liCurrentItem.Key)
  437.         Me.MousePointer = vbDefault
  438.         
  439.         lvServices.SetFocus
  440.     End If
  441.     Set liCurrentItem = Nothing
  442. End Sub
  443. Private Sub ContinueServiceByName(sServiceName As String)
  444.     Dim liCurrentItem As ListItem
  445.     Set liCurrentItem = lvServices.ListItems(sServiceName)
  446.     'The service must be paused to continue it
  447.     If colServices(sServiceName).CurrentStatus = SERVICE_PAUSED Then
  448.         Me.MousePointer = vbHourglass
  449.         ServiceContinue "", sServiceName
  450.             
  451.         'Set global variables for Service Control dialog box
  452.         SERVICE_CONTROL_MSGBOX_MESSAGE = "Attempting to continue the " & colServices(liCurrentItem.Key).DisplayName & " service."
  453.         SERVICE_CONTROL_WAITTIME = 3000
  454.         SERVICE_CONTROL_MSGBOX_TITLE = "Service Control"
  455.         SERVICE_CONTROL_MACHINENAME = ""
  456.         SERVICE_CONTROL_SERVICENAME = sServiceName
  457.         SERVICE_CONTROL_STATUS_WANTED = SERVICE_RUNNING
  458.         frmSCMsgBox.Show vbModal
  459.         
  460.         Call subSetupButtons(liCurrentItem.Key)
  461.         Me.MousePointer = vbDefault
  462.         
  463.         lvServices.SetFocus
  464.     End If
  465.     Set liCurrentItem = Nothing
  466. End Sub
  467.