怎么样才能得到当前系统的服务和它们的运行情况啊!!!

wangfs111222 2006-07-16 10:41:26
rt!!!


分不够可以+的,急需!
...全文
156 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsftest 2006-07-16
  • 打赏
  • 举报
回复
'用wmi:

Private Sub Command4_Click()
Dim strComputer As String
Dim objWMIService As Object
Dim colProcessList
Dim objProcess As Object
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("select * from win32_service")
For Each objProcess In colProcessList
debug.print cstr(objProcess.name),cstr(objProcess.state),cstr(objProcess.status)
Next
End Sub

一笑拔剑 2006-07-16
  • 打赏
  • 举报
回复
来迟了
boywang 2006-07-16
  • 打赏
  • 举报
回复

Public 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

'just help to keep the code lines
'below from becoming too long for
'html display
Dim sSvcName As String
Dim sDispName As String
Dim dwState As Long

'establish a connection to the service control
'manager on the local computer and open
'the local service control manager database.
hSCManager = OpenSCManager(vbNullString, _
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_STATE_ALL, _
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_STATE_ALL, _
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

.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 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


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 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

boywang 2006-07-16
  • 打赏
  • 举报
回复
BAS Module Code

Place the following code into the general declarations area of a bas module:

--------------------------------------------------------------------------------

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2004 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type

Public Type ENUM_SERVICE_STATUS
lpServiceName As Long
lpDisplayName As Long
ServiceStatus As SERVICE_STATUS
End Type

'our own constant
Public Const SIZEOF_SERVICE_STATUS As Long = 36

'windows constants
Public Const ERROR_MORE_DATA = 234
Public Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Public Const LB_SETTABSTOPS As Long = &H192
Public Const SERVICE_STATE_ALL = &H3

'Service Types (Bit Mask)
'corresponds to SERVICE_STATUS.dwServiceType
Public Const SERVICE_KERNEL_DRIVER As Long = &H1
Public Const SERVICE_FILE_SYSTEM_DRIVER As Long = &H2
Public Const SERVICE_ADAPTER As Long = &H4
Public Const SERVICE_RECOGNIZER_DRIVER As Long = &H8
Public Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Public Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
Public Const SERVICE_INTERACTIVE_PROCESS As Long = &H100

Public Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS Or _
SERVICE_WIN32_SHARE_PROCESS

Public Const SERVICE_DRIVER As Long = SERVICE_KERNEL_DRIVER Or _
SERVICE_FILE_SYSTEM_DRIVER Or _
SERVICE_RECOGNIZER_DRIVER

Public Const SERVICE_TYPE_ALL As Long = SERVICE_WIN32 Or _
SERVICE_ADAPTER Or _
SERVICE_DRIVER Or _
SERVICE_INTERACTIVE_PROCESS

'Service State
'corresponds to SERVICE_STATUS.dwCurrentState
Public Const SERVICE_STOPPED As Long = &H1
Public Const SERVICE_START_PENDING As Long = &H2
Public Const SERVICE_STOP_PENDING As Long = &H3
Public Const SERVICE_RUNNING As Long = &H4
Public Const SERVICE_CONTINUE_PENDING As Long = &H5
Public Const SERVICE_PAUSE_PENDING As Long = &H6
Public Const SERVICE_PAUSED As Long = &H7

Public Declare Function OpenSCManager Lib "advapi32" _
Alias "OpenSCManagerA" _
(ByVal lpMachineName As String, _
ByVal lpDatabaseName As String, _
ByVal dwDesiredAccess As Long) As Long

Public Declare Function EnumServicesStatus Lib "advapi32" _
Alias "EnumServicesStatusA" _
(ByVal hSCManager As Long, _
ByVal dwServiceType As Long, _
ByVal dwServiceState As Long, _
lpServices As Any, _
ByVal cbBufSize As Long, _
pcbBytesNeeded As Long, _
lpServicesReturned As Long, _
lpResumeHandle As Long) As Long

Public Declare Function CloseServiceHandle Lib "advapi32" _
(ByVal hSCObject As Long) As Long

Public Declare Function lstrcpyA Lib "kernel32" _
(ByVal RetVal As String, ByVal Ptr As Long) As Long

Public Declare Function lstrlenA Lib "kernel32" _
(ByVal Ptr As Any) As Long

Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
To a form add a command button (Command1) and a list box (List1). Add the following code:

--------------------------------------------------------------------------------

Option Explicit

Private Sub Form_Load()

ReDim TabArray(0 To 1) As Long

TabArray(0) = 150
TabArray(1) = 220
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&)
Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 2&, TabArray(0))
List1.Refresh

Command1.Caption = "Enum Services"

End Sub


Private Sub Command1_Click()

Call EnumSystemServices(List1)

End Sub

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧