Solved

Run-time error -2147467259 System Error &H80004005

Posted on 2006-11-01
3
777 Views
Last Modified: 2012-05-05
Hi folks, I'm using the code below in a bas module to retrieve the current status of windows services.  This works fine on my local machine (Windows XP SP2) however when I move the files (and any required DLLs) to another machine (also Windows XP SP2) the QueryService() function returns the above error.

Any help on this would be greatly appreciated as I'm at my whits end, the code below shows the contents of the bas module as well as an example of how I'm using it.  The code itself is from another source, most likely here to be honest so I take no credit for it at all, but unfortunately that makes it hard for me to debug :\

Cheers

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

Const STANDARD_RIGHTS_REQUIRED = &HF0000
Const STANDARD_RIGHTS_ALL = &H1F0000
Const SPECIFIC_RIGHTS_ALL = &HFFFF

' Service database names
Const SERVICES_ACTIVE_DATABASE = "ServicesActive"
Const SERVICES_FAILED_DATABASE = "ServicesFailed"

' Value to indicate no change to an optional parameter
Const SERVICE_NO_CHANGE = &HFFFF

' Service State -- for Enum Requests (Bit Mask)
Const SERVICE_ACTIVE = &H1
Const SERVICE_INACTIVE = &H2
Const SERVICE_STATE_ALL = (SERVICE_ACTIVE Or SERVICE_INACTIVE)

' Controls
Const SERVICE_CONTROL_STOP = &H1
Const SERVICE_CONTROL_PAUSE = &H2
Const SERVICE_CONTROL_CONTINUE = &H3
Const SERVICE_CONTROL_INTERROGATE = &H4
Const SERVICE_CONTROL_SHUTDOWN = &H5

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

' Controls Accepted  (Bit Mask)
Const SERVICE_ACCEPT_STOP = &H1
Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
Const SERVICE_ACCEPT_SHUTDOWN = &H4

' Service Control Manager object specific access types
Const SC_MANAGER_CONNECT = &H1
Const SC_MANAGER_CREATE_SERVICE = &H2
Const SC_MANAGER_ENUMERATE_SERVICE = &H4
Const SC_MANAGER_LOCK = &H8
Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20

Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SC_MANAGER_CONNECT Or SC_MANAGER_CREATE_SERVICE Or SC_MANAGER_ENUMERATE_SERVICE Or SC_MANAGER_LOCK Or SC_MANAGER_QUERY_LOCK_STATUS Or SC_MANAGER_MODIFY_BOOT_CONFIG)

' Service object specific access type
Const SERVICE_QUERY_CONFIG = &H1
Const SERVICE_CHANGE_CONFIG = &H2
Const SERVICE_QUERY_STATUS = &H4
Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Const SERVICE_START = &H10
Const SERVICE_STOP = &H20
Const SERVICE_PAUSE_CONTINUE = &H40
Const SERVICE_INTERROGATE = &H80
Const SERVICE_USER_DEFINED_CONTROL = &H100
Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or SERVICE_QUERY_CONFIG Or SERVICE_CHANGE_CONFIG Or SERVICE_QUERY_STATUS Or SERVICE_ENUMERATE_DEPENDENTS Or SERVICE_START Or SERVICE_STOP Or SERVICE_PAUSE_CONTINUE Or SERVICE_INTERROGATE Or SERVICE_USER_DEFINED_CONTROL)

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 Function QueryService(strServiceName As String, strServerName As String) As String
    On Error GoTo errH
   
    App.LogEvent "QueryService : " & strServiceName
    Dim lngSMHandle As Long
    Dim lngSvcHandle As Long
    Dim ssStatus As SERVICE_STATUS
    lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
    lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
    ret = QueryServiceStatus(lngSvcHandle, ssStatus)
    If lngSvcHandle = 0& Then QueryService = "Service Not Found"
    Select Case ssStatus.dwCurrentState
    Case SERVICE_STOPPED
        QueryService = "Service Is Stopped"
    Case SERVICE_START_PENDING
        QueryService = "Service Start Pending"
    Case SERVICE_STOP_PENDING
        QueryService = "Service Stop Pending"
    Case SERVICE_RUNNING
        QueryService = "Service Is Running"
    Case SERVICE_CONTINUE_PENDING
        QueryService = "Service Continue Pending"
    Case SERVICE_PAUSE_PENDING
        QueryService = "Service Pause Pending"
    Case SERVICE_PAUSED
        QueryService = "Service Paused"
    End Select
    CloseServiceHandle lngSvcHandle
    CloseServiceHandle lngSMHandle
   
    Exit Function
   
errH:
    CloseServiceHandle lngSvcHandle
    CloseServiceHandle lngSMHandle
   
    QueryService = Err.Number & " " & Err.Description
End Function

Public Sub StopSvc(strServiceName As String, strServerName As String)
    App.LogEvent "Stop Service : " & strServiceName
    Dim lngSMHandle As Long
    Dim lngSvcHandle As Long
    Dim ssStatus As SERVICE_STATUS
    lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
    lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
    ret = QueryServiceStatus(lngSvcHandle, ssStatus)
    If lngSvcHandle <> 0& Then
        ControlService lngSvcHandle, SERVICE_CONTROL_STOP, ssStatus
    End If
    CloseServiceHandle lngSvcHandle
    CloseServiceHandle lngSMHandle
End Sub

Public Sub StartSvc(strServiceName As String, strServerName As String)
    App.LogEvent "Start Service : " & strServiceName
    Dim lngSMHandle As Long
    Dim lngSvcHandle As Long
    Dim ssStatus As SERVICE_STATUS
    lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
    lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
    ret = QueryServiceStatus(lngSvcHandle, ssStatus)
    If lngSvcHandle <> 0& Then
        StartService lngSvcHandle, 0&, 0&
    End If
    CloseServiceHandle lngSvcHandle
    CloseServiceHandle lngSMHandle
End Sub
============ end of bas module =======




Example of how I'm using it:

dim sReturn as string
sReturn = QueryService("Service Name Here", "")


The ,"") tells it to use the local machine, however I've tried using the machine name there too.. e.g.

sReturn = QueryService("Service Name Here", "Computer Name Here")

and still get the same error.

Thanks in advance
0
Comment
Question by:ShelfieldCollege
  • 2
3 Comments
 
LVL 4

Author Comment

by:ShelfieldCollege
ID: 17848745
Hi, I've just done some research and found an alternative method of controlling windows services, as seen here:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20730045.html?query=windows+services&topics=93

I've incorporated this into my code and will try it on the remote machine, if this works then problem solved :)
0
 
LVL 4

Author Comment

by:ShelfieldCollege
ID: 17848811
Hi, the solution mentioned above works fine and has solved the issue using the alternative method of checking services status.

I will request in community support that this question be closed as I have managed to answer it myself, cheers.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 0 total points
ID: 17848847
question PAQed with points refunded
angelIII, paged editor
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now