0

使用我的代码,我可以启动和停止服务,基本上在我的应用程序中,我正在刷新 WIA 窗口服务。因此,在停止服务之前,我想知道状态..据我所知QueryServiceStatus,这样做但在我的代码中它返回 0(失败)。

' start/stop/pause/continue a service
' SERVICENAME is the
' COMMAND can be   0=Start, 1=Stop, 2=Pause, 3=Continue
'
' returns True if successful, False otherwise
' if any error, call Err.LastDLLError for more information

Function ServiceCommand(ByVal ServiceName As String, ByVal command As Long) As _
    Boolean
    Dim hSCM As Long
    Dim hService As Long
    Dim res As Long

    Dim query As Long
    Dim lpServiceStatus As SERVICE_STATUS

    ' first, check the command
    If command < 0 Or command > 3 Then Err.Raise 5

    ' open the connection to Service Control Manager, exit if error
    hSCM = OpenSCManager(vbNullString, vbNullString, GENERIC_EXECUTE)
    If hSCM = 0 Then Exit Function



    ' open the given service, exit if error
    hService = OpenService(hSCM, ServiceName, GENERIC_EXECUTE)
    If hService = 0 Then GoTo CleanUp

    'fetch the status
    query = QueryServiceStatus(hService, lpServiceStatus)

    ' start the service
    Select Case command
        Case 0
            ' to start a service you must use StartService
            res = StartService(hService, 0, 0)
        Case SERVICE_CONTROL_STOP, SERVICE_CONTROL_PAUSE, _
            SERVICE_CONTROL_CONTINUE
            ' these commands use ControlService API
            ' (pass a NULL pointer because no result is expected)
            res = ControlService(hService, command, lpServiceStatus)
    End Select
    If res = 0 Then GoTo CleanUp

    ' return success
    ServiceCommand = True

    CleanUp:
        If hService Then CloseServiceHandle hService
        ' close the SCM
        CloseServiceHandle hSCM

End Function

另外,如果有人也可以告诉我对窗口服务的一些疑问:

  1. 同一个系统可以有 2 个不同版本的窗口服务(WIA 1.0 和 WIA 2.0)吗?
  2. 上述服务是否具有不同的服务名称(WIA2.0 名称 =StiSvc)或相同?
4

1 回答 1

1

此答案来自 freevbcode.com 上的示例。

' Service State - for CurrentState
Public Const SERVICE_STOPPED = &H1
Public Const SERVICE_START_PENDING = &H2
Public Const SERVICE_STOP_PENDING = &H3
Public Const SERVICE_RUNNING = &H4
Public Const SERVICE_CONTINUE_PENDING = &H5
Public Const SERVICE_PAUSE_PENDING = &H6
Public Const SERVICE_PAUSED = &H7

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

Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal  dwControl As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, lpServiceStatus As SERVICE_STATUS) As Long
Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long


Public Function ServiceStatus(ComputerName As String, ServiceName As String) As String
    Dim ServiceStat As SERVICE_STATUS
    Dim hSManager As Long
    Dim hService As Long
    Dim hServiceStatus As Long

    ServiceStatus = ""
    hSManager = OpenSCManager(ComputerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS)
    If hSManager <> 0 Then
        hService = OpenService(hSManager, ServiceName, SERVICE_ALL_ACCESS)
        If hService <> 0 Then
            hServiceStatus = QueryServiceStatus(hService, ServiceStat)
            If hServiceStatus <> 0 Then
                Select Case ServiceStat.dwCurrentState
                Case SERVICE_STOPPED
                    ServiceStatus = "Stopped"
                Case SERVICE_START_PENDING
                    ServiceStatus = "Start Pending"
                Case SERVICE_STOP_PENDING
                    ServiceStatus = "Stop Pending"
                Case SERVICE_RUNNING
                    ServiceStatus = "Running"
                Case SERVICE_CONTINUE_PENDING
                    ServiceStatus = "Coninue Pending"
                Case SERVICE_PAUSE_PENDING
                    ServiceStatus = "Pause Pending"
                Case SERVICE_PAUSED
                    ServiceStatus = "Paused"
                End Select
            End If
            CloseServiceHandle hService
        End If
        CloseServiceHandle hSManager
    End If
End Function

完整的示例可以在http://www.freevbcode.com/ShowCode.asp?ID=6829 找到。我不知道你其他问题的答案。

于 2013-05-30T14:33:10.037 回答