2013-05-30 123 views
-1

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

' 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)还是相同?
+0

你不显示必要的声明和类型定义 - 没有办法告诉你的代码是否工作。 –

+0

如果您尝试安装与现有服务具有相同名称的服务,则Windows服务管理器将引发异常。因此,您必须拥有不同的服务名称才能同时运行多个版本的服务。 – BobRodes

+0

谢谢bobRodes ...如果WIA 1.0和WIA 2.0有相同的名字(StiSvc),你有什么想法吗? – user1702421

回答

0

这个答案来自于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.发现我不知道答案,你的其他问题。

+0

感谢jac ..帮助:) – user1702421