1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
Option Explicit
Private lastIndex As Long
Private Sub Combo1_Click()
If Combo1.ListIndex > -1 Then
Command1(lastIndex).Value = True
End If
End Sub
Private Sub Command1_Click(Index As Integer)
Dim sMachine As String
If Combo1.ListIndex > -1 Then
sMachine = Combo1.List(Combo1.ListIndex)
Select Case Index
Case 0:
'active services
EnumSystemServices SERVICE_ACTIVE, sMachine, List1
Case 1:
'inactive services
EnumSystemServices SERVICE_INACTIVE, sMachine, List1
Case 2:
'all services
EnumSystemServices SERVICE_STATE_ALL, sMachine, List1
End Select
'save the current option to allow the list
'to refresh with the same type of data as
'different machines are selected.
lastIndex = Index
End If
End Sub
Private Sub Form_Load()
ReDim TabArray(0 To 4) As Long
TabArray(0) = 150
TabArray(1) = 220
TabArray(2) = 270
TabArray(3) = 342
TabArray(4) = 390
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 5&, TabArray(0))
List1.Refresh
Command1(0).Caption = "Active Services"
Command1(1).Caption = "Inactive Services"
Command1(2).Caption = "All Services"
'if you have a large list of machines, you may
'want to move this call to a command button to
'speed loading.
Call GetServers(vbNullString)
End Sub
Public Function EnumSystemServices(SERVICE_TYPE As Long, _
sMachine As String, _
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
'these five just help keep the
'code lines from becoming too long
'for html display
Dim sSvcName As String
Dim sDispName As String
Dim dwState As Long
Dim dwType As Long
Dim dwCtrls As Long
'update label to show what we're trying to view
Label1.Caption = "Viewing " & Command1(SERVICE_TYPE - 1).Caption & " on " & sMachine
'establish a connection to the service control
'manager on the local computer and open
'the local service control manager database.
hSCManager = OpenSCManager("\\" & sMachine, _
vbNullString, _
SC_MANAGER_ENUMERATE_SERVICE)
If hSCManager <> 0 Then
'Get buffer size by calling EnumServicesStatus.
'To determine the required buffer size, call EnumServicesStatus
'with cbBuffer and hEnumResume set to zero. EnumServicesStatus
'fails (returns 0), and Err.LastDLLError returns ERROR_MORE_DATA,
'filling cbRequired with the size, in bytes, of the buffer
'required to hold the array of structures and their data.
success = EnumServicesStatus(hSCManager, _
SERVICE_WIN32, _
SERVICE_TYPE, _
ByVal &H0, _
&H0, _
cbRequired, _
dwReturned, _
hEnumResume)
'If success is 0 and the LastDllError is
'ERROR_MORE_DATA, use returned info to create
'the required data buffer
If success = 0 And Err.LastDllError = ERROR_MORE_DATA Then
'Calculate number of structures needed
'and redimension the array
cbBuffer = (cbRequired \ SIZEOF_SERVICE_STATUS) + 1
ReDim pntr(0 To cbBuffer)
'Set cbBuffSize equal to the size of the buffer
cbBuffSize = cbBuffer * SIZEOF_SERVICE_STATUS
'Enumerate the services. If the function succeeds,
'the return value is nonzero. If the function fails,
'the return value is zero. In addition, hEnumResume
'must be set to 0.
hEnumResume = 0
If EnumServicesStatus(hSCManager, _
SERVICE_WIN32, _
SERVICE_TYPE, _
pntr(0), _
cbBuffSize, _
cbRequired, _
dwReturned, _
hEnumResume) Then
'pntr() array is now filled with service data,
'so it is a simple matter of extracting the
'required information.
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
dwType = pntr(i).ServiceStatus.dwServiceType
dwCtrls = pntr(i).ServiceStatus.dwControlsAccepted
.AddItem sDispName & vbTab & _
sSvcName & vbTab & _
GetServiceState(dwState) & vbTab & _
GetServiceType(dwType) & vbTab & _
GetServiceControl(dwCtrls)
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 failed; error = " & CStr(Err.LastDllError)
End If 'If hSCManager <> 0
'Clean up
Call CloseServiceHandle(hSCManager)
'return the number of services
'returned as a sign of success
EnumSystemServices = dwReturned
End Function
Private Function GetServers(sDomain As String) As Long
'lists all servers of the specified type
'that are visible in a domain.
Dim bufptr As Long
Dim dwEntriesread As Long
Dim dwTotalentries As Long
Dim dwResumehandle As Long
Dim se100 As SERVER_INFO_100
Dim success As Long
Dim nStructSize As Long
Dim cnt As Long
nStructSize = LenB(se100)
'Call passing MAX_PREFERRED_LENGTH to have the
'API allocate required memory for the return values.
'
'The call is enumerating all machines on the
'network (SV_TYPE_ALL); however, by Or'ing
'specific bit masks for defined types you can
'customize the returned data. For example, a
'value of 0x00000003 combines the bit masks for
'SV_TYPE_WORKSTATION (0x00000001) and
'SV_TYPE_SERVER (0x00000002).
'
'dwServerName must be Null. The level parameter
'(100 here) specifies the data structure being
'used (in this case a SERVER_INFO_100 structure).
'
'The domain member is passed as Null, indicating
'machines on the primary domain are to be retrieved.
'If you decide to use this member, pass
'StrPtr("domain name"), not the string itself.
success = NetServerEnum(0&, _
100, _
bufptr, _
MAX_PREFERRED_LENGTH, _
dwEntriesread, _
dwTotalentries, _
SV_TYPE_WORKSTATION Or SV_TYPE_SERVER, _
0&, _
dwResumehandle)
'if all goes well
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
'loop through the returned data, adding each
'machine to the list
For cnt = 0 To dwEntriesread - 1
'get one chunk of data and cast
'into an LOCALGROUP_INFO_1 type
'in order to add the name to a list
CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize
Combo1.AddItem GetPointerToByteStringW(se100.sv100_name)
Next
End If
'clean up, regardless of success
Call NetApiBufferFree(bufptr)
End Function
Public Function GetStrFromPtrA(ByVal lpszA As Long) As String
GetStrFromPtrA = String$(lstrlenA(ByVal lpszA), 0)
Call lstrcpyA(ByVal GetStrFromPtrA, ByVal lpszA)
End Function
Private Function GetServiceControl(dwControl As Long) As String
Dim tmp As String
If dwControl Then
If (dwControl And SERVICE_ACCEPT_STOP) Then tmp = tmp & "stop, "
If (dwControl And SERVICE_ACCEPT_PAUSE_CONTINUE) Then tmp = tmp & "pause, "
If (dwControl And SERVICE_ACCEPT_SHUTDOWN) Then tmp = tmp & "shutdown"
'Else: tmp = vbTab
End If
GetServiceControl = tmp
End Function
Private Function GetServiceType(dwType As Long) As String
Dim sType As String
If (dwType And SERVICE_WIN32_OWN_PROCESS) Then sType = sType & "own proc, "
If (dwType And SERVICE_WIN32_SHARE_PROCESS) Then sType = sType & "share, "
If (dwType And SERVICE_KERNEL_DRIVER) Then sType = sType & "kernel, "
If (dwType And SERVICE_FILE_SYSTEM_DRIVER) Then sType = sType & "filesys, "
If (dwType And SERVICE_INTERACTIVE_PROCESS) Then sType = sType & "interactive"
GetServiceType = sType
End Function
Private Function GetServiceState(dwState As Long) As String
Select Case dwState
Case SERVICE_STOPPED: GetServiceState = "stopped"
Case SERVICE_START_PENDING: GetServiceState = "startpend"
Case SERVICE_STOP_PENDING: GetServiceState = "stoppend"
Case SERVICE_RUNNING: GetServiceState = "running"
Case SERVICE_CONTINUE_PENDING: GetServiceState = "contpend"
Case SERVICE_PAUSE_PENDING: GetServiceState = "pausepend"
Case SERVICE_PAUSED: GetServiceState = "paused"
End Select
End Function
'--end block--' |
Partager