Advertisement
4_2005-2006 Windows System Services #150946

Enumertate Windows NT Services

Populates a collection of installed Windows NT services, based upon type of service requested. Can be used to enumerate the services on a workstation or server. Requires Windows NT and administrator rights.

AI

สรุปโดย AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

ซอร์สโค้ด
original-source
Public Function EnumerateServices(colSVC As Collection, bolDisplayName As Boolean, Optional lngServiceType As Variant, Optional lngServiceState As Variant, Optional strMachineName As Variant) As Long
 
 '// lngServiceType = 0 (win32 services)
 '// lngServiceType = 1 (driver services)
 '// lngServiceState = 0 (active and inactive services)
 '// lngServiceState = 1 (active services)
 '// lngServiceState = 2 (inactive services)
 Dim hSCM As Long
 Dim lngBytesNeeded As Long
 Dim lngResumeHandle As Long
 Dim lngServicesReturned As Long
 Dim lngStructsNeeded As Long
 Dim lngServiceStatusInfoBuffer As Long
 Dim lngSVCReturnCode As Long
 Dim lngI As Long
 Dim strSVCName As String * 250
 Dim lpEnumServiceStatus() As ENUM_SERVICE_STATUS
 
 On Error Resume Next
 If IsMissing(lngServiceType) = True Then lngServiceType = 0 Else lngServiceType = CLng(lngServiceType)
 If IsMissing(lngServiceState) = True Then lngServiceState = 0 Else lngServiceState = CLng(lngServiceState)
 If IsMissing(strMachineName) = True Then strMachineName = vbNullString Else strMachineName = CStr(strMachineName)
 If lngServiceType = 0 Then lngServiceType = 30
 If lngServiceType = 1 Then lngServiceType = 11
 If lngServiceState = 0 Then lngServiceState = 3
 If lngServiceState = 1 Then lngServiceState = &H1
 If lngServiceState = 2 Then lngServiceState = &H2
 '// Open the service manager
 hSCM = OpenSCManager(strMachineName, vbNullString, &H4)
 If hSCM = 0 Then Exit Function '// error opening
 
 '// Get buffer size (bytes) without passing a buffer
 Call EnumServicesStatus(hSCM, lngServiceType, lngServiceState, ByVal &H0, &H0, lngBytesNeeded, lngServicesReturned, lngResumeHandle)
 
 '// We should receive MORE_DATA error
 If Not Err.LastDllError = 234 Then
 Call CloseServiceHandle(hSCM)
 Exit Function
 End If
 
 '// Calculate the number of structures needed and redimention array
 lngStructsNeeded = lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1
 ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
 
 '// Get buffer size in bytes
 lngServiceStatusInfoBuffer = lngStructsNeeded * Len(lpEnumServiceStatus(0))
 
 '// Get services information starting entry 0
 lngResumeHandle = 0
 lngSVCReturnCode = EnumServicesStatus(hSCM, lngServiceType, lngServiceState, lpEnumServiceStatus(0), lngServiceStatusInfoBuffer, lngBytesNeeded, lngServicesReturned, lngResumeHandle)
 If lngSVCReturnCode <> 0 Then
 For lngI = 0 To lngServicesReturned - 1
  If bolDisplayName = True Then
  Call lstrcpy(ByVal strSVCName, ByVal lpEnumServiceStatus(lngI).lpDisplayName)
  Else
  Call lstrcpy(ByVal strSVCName, ByVal lpEnumServiceStatus(lngI).lpServiceName)
  End If
  colSVC.Add StripTerminator(strSVCName)
 Next
 End If
 
 Call CloseServiceHandle(hSCM)
 
 EnumerateServices = colSVC.Count
 
End Function
Private Function StripTerminator(ByVal strString As String) As String
 
 If InStr(strString, Chr(0)) > 0 Then StripTerminator = Left(strString, InStr(strString, Chr(0)) - 1) Else StripTerminator = strString
 
End Function
ความคิดเห็นดั้งเดิม (3)
กู้คืนจาก Wayback Machine