⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 电子书“Visual Basic 6 网络编程实例教程.rar”
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "枚举本机的服务"
   ClientHeight    =   6135
   ClientLeft      =   45
   ClientTop       =   330
   ClientWidth     =   8955
   Icon            =   "Form1.frx":0000
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   6135
   ScaleWidth      =   8955
   StartUpPosition =   2  '屏幕中心
   Begin VB.ListBox List1 
      Height          =   4740
      Left            =   240
      TabIndex        =   1
      Top             =   600
      Width           =   8415
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   240
      TabIndex        =   0
      Top             =   5520
      Width           =   1215
   End
   Begin VB.Label Label3 
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "状态"
      Height          =   180
      Left            =   7680
      TabIndex        =   4
      Top             =   360
      Width           =   360
   End
   Begin VB.Label Label2 
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "服务名"
      Height          =   180
      Left            =   5280
      TabIndex        =   3
      Top             =   360
      Width           =   540
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      BackStyle       =   0  'Transparent
      Caption         =   "显示名"
      Height          =   180
      Left            =   480
      TabIndex        =   2
      Top             =   360
      Width           =   540
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Sub Command1_Click()
    Call EnumSystemServices(List1)
End Sub

Private Sub Form_Load()
    ReDim TabArray(0 To 1) As Long
   TabArray(0) = 230
   TabArray(1) = 320
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 2&, TabArray(0))
   List1.Refresh
   Command1.Caption = "运行(&R)"
End Sub

Private Function EnumSystemServices(ctl As Control) As Long
   Dim hSCManager As Long
   Dim pntr() As ENUM_SERVICE_STATUS
   Dim cbBuffSize As Long
   Dim cbRequired As Long
   Dim dwReturned As Long
   Dim hEnumResume As Long
   Dim cbBuffer As Long
   Dim success As Long
   Dim i As Long
   Dim sSvcName As String
   Dim sDispName As String
   Dim dwState As Long
  '建立与本地计算机的服务控制管理器的连接,
  '并打开本地服务控制管理器数据库。
   hSCManager = OpenSCManager(vbNullString, _
                              vbNullString, _
                              SC_MANAGER_ENUMERATE_SERVICE)
   If hSCManager <> 0 Then
     '调用EnumServicesStatus获取缓存的大小
     '在调用EnumServicesStatus时,将参数cbBuffer和hEnumResume设置为0,
     '可获取存放结构数组所需的缓存的大小。该值将由参数cbRequired带回。
     '此时,EnumServicesStatus调用失败(返回0),
     '且Err.LastDLLError的值为ERROR_MORE_DATA。
      success = EnumServicesStatus(hSCManager, _
                                   SERVICE_WIN32, _
                                   SERVICE_STATE_ALL, _
                                   ByVal &H0, _
                                   &H0, _
                                   cbRequired, _
                                   dwReturned, _
                                   hEnumResume)
     '如success为0,且LastDllError值为ERROR_MORE_DATA,
     '则可使用返回信息创建所需的缓存
      If success = 0 And Err.LastDllError = ERROR_MORE_DATA Then
        '计算结构的数量,并重定义结构数组
         cbBuffer = (cbRequired \ SIZEOF_SERVICE_STATUS) + 1
         ReDim pntr(0 To cbBuffer)
        '设置cbBuffSize为缓存的大小
         cbBuffSize = cbBuffer * SIZEOF_SERVICE_STATUS
        '枚举服务。如古成功,则返回非0值,否则返回0。
        'hEnumResume必须比设置为0。
         hEnumResume = 0
         If EnumServicesStatus(hSCManager, _
                               SERVICE_WIN32, _
                               SERVICE_STATE_ALL, _
                               pntr(0), _
                               cbBuffSize, _
                               cbRequired, _
                               dwReturned, _
                               hEnumResume) Then
           '从pntr()数组中提取所需的信息
            With ctl
               .Clear
               For i = 0 To dwReturned - 1
                  sDispName = GetStrFromPtrA(ByVal pntr(i).lpDisplayName)
                  sSvcName = GetStrFromPtrA(ByVal pntr(i).lpServiceName)
                  dwState = pntr(i).ServiceStatus.dwCurrentState
                  .AddItem sDispName & vbTab & _
                           sSvcName & vbTab & _
                           GetServiceState(dwState)
               Next
            End With
         Else: MsgBox "EnumServicesStatus; error " & _
                      CStr(Err.LastDllError)
         End If  'If EnumServicesStatus
      Else: MsgBox "ERROR_MORE_DATA not returned; error " & _
                    CStr(Err.LastDllError)
      End If  'If success = 0 And Err.LastDllError
   Else: MsgBox "OpenSCManager调用失败; error = " & _
                CStr(Err.LastDllError)
   End If  'If hSCManager <> 0
  '关闭SCM数据库句柄
   Call CloseServiceHandle(hSCManager)
  '返回服务的数量
   EnumSystemServices = dwReturned
End Function

Private Function GetStrFromPtrA(ByVal lpszA As Long) As String
   GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
   Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function

Private Function GetServiceState(dwState As Long) As String
   Select Case dwState
      Case SERVICE_STOPPED:          GetServiceState = "停止"
      Case SERVICE_START_PENDING:    GetServiceState = "启动期间"
      Case SERVICE_STOP_PENDING:     GetServiceState = "停止期间"
      Case SERVICE_RUNNING:          GetServiceState = "正运行"
      Case SERVICE_CONTINUE_PENDING: GetServiceState = "继续"
      Case SERVICE_PAUSE_PENDING:    GetServiceState = "暂停期间"
      Case SERVICE_PAUSED:           GetServiceState = "暂停"
      Case Else:                     GetServiceState = ""
   End Select
End Function

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -