Solved

Visual basic Ntsvc.ocx unable to start service!!

Posted on 2004-08-29
5
590 Views
Last Modified: 2012-06-22
Hello,

I am getting the following error when trying to start my app from the service control manager:

"The Testing App service on local computer started and then stopped.  some services stop automaticly if they have no work to do, for example, the performance logs and alerts service."


I just simply want to know how to tell the SCM that yes my app is running, I have NTService.StartService on form_load.

HELP!!!!!! URGENT
0
Comment
Question by:bluedragon99
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
This is VERY nice but service will not start because form1 is not telling SCM hey I'm started





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
Public Declare Function CreateService Lib "advapi32" Alias "CreateServiceA" _
      (ByVal hSCManager As Long, ByVal lpServiceName As String, _
      ByVal lpDisplayName As String, ByVal dwDesiredAccess As Long, _
      ByVal dwServiceType As Long, ByVal dwStartType As Long, _
      ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, _
      ByVal lpLoadOrderGroup As String, ByVal lpdwTagId As String, _
      ByVal lpDependencies As String, ByVal lp As String, _
      ByVal lpPassword As String) As Long
Public Declare Function DeleteService Lib "advapi32" (ByVal hService As Long) As Long

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

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

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

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

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

' 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

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

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

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

Public Const SERVICE_DEMAND_START As Long = 3
Public Const SERVICE_ERROR_NORMAL As Long = 1

' Service object specific access type
Public Const SERVICE_QUERY_CONFIG = &H1
Public Const SERVICE_CHANGE_CONFIG = &H2
Public Const SERVICE_QUERY_STATUS = &H4
Public Const SERVICE_ENUMERATE_DEPENDENTS = &H8
Public Const SERVICE_START = &H10
Public Const SERVICE_STOP = &H20
Public Const SERVICE_PAUSE_CONTINUE = &H40
Public Const SERVICE_INTERROGATE = &H80
Public Const SERVICE_USER_DEFINED_CONTROL = &H100
Public 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 Const SERVICE_WIN32_OWN_PROCESS As Long = &H10
Public Const SERVICE_WIN32_SHARE_PROCESS As Long = &H20
Public Const SERVICE_WIN32 As Long = SERVICE_WIN32_OWN_PROCESS + SERVICE_WIN32_SHARE_PROCESS

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

Public Const ERROR_MORE_DATA = 234

Public Declare Function EnumServicesStatus Lib "advapi32.dll" 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 lstrcpy Lib "Kernel32.dll" Alias "lstrcpyA" (szDest As String, szcSource As Long) As Long

Public Function GetServiceStatus(strServiceName As String, strServerName As String) As String
   'Left strServerName = "" for local service
   App.LogEvent "GetServiceStatus : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   Dim ret As Long
   
   lngSMHandle = OpenSCManager(strServerName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)
   lngSvcHandle = OpenService(lngSMHandle, strServiceName, SERVICE_ALL_ACCESS)
   ret = QueryServiceStatus(lngSvcHandle, ssStatus)
   If lngSvcHandle = 0& Then GetServiceStatus = "Service Not Found"
   Select Case ssStatus.dwCurrentState
   Case SERVICE_STOPPED
       GetServiceStatus = "Service Is Stopped"
   Case SERVICE_START_PENDING
       GetServiceStatus = "Service Start Pending"
   Case SERVICE_STOP_PENDING
       GetServiceStatus = "Service Stop Pending"
   Case SERVICE_RUNNING
       GetServiceStatus = "Service Is Running"
   Case SERVICE_CONTINUE_PENDING
       GetServiceStatus = "Service Continue Pending"
   Case SERVICE_PAUSE_PENDING
       GetServiceStatus = "Service Pause Pending"
   Case SERVICE_PAUSED
       GetServiceStatus = "Service Paused"
   End Select
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Function

Public Function GetServiceStatusWithStatusNumber(ByVal StatusNumber As Long) As String
    Select Case StatusNumber
    Case SERVICE_STOPPED
        GetServiceStatusWithStatusNumber = "Service Is Stopped"
    Case SERVICE_START_PENDING
        GetServiceStatusWithStatusNumber = "Service Start Pending"
    Case SERVICE_STOP_PENDING
        GetServiceStatusWithStatusNumber = "Service Stop Pending"
    Case SERVICE_RUNNING
        GetServiceStatusWithStatusNumber = "Service Is Running"
    Case SERVICE_CONTINUE_PENDING
        GetServiceStatusWithStatusNumber = "Service Continue Pending"
    Case SERVICE_PAUSE_PENDING
        GetServiceStatusWithStatusNumber = "Service Pause Pending"
    Case SERVICE_PAUSED
        GetServiceStatusWithStatusNumber = "Service Paused"
    End Select
End Function

Public Function InstallSvc(strServiceName As String, strDisplayName As String, strServiceExeFilePath As String, Optional strAccount As String = "LocalSystem", Optional strPassword As String = "") As Long
    Dim hSCManager As Long
    Dim hService As Long, DomainName As String
   
'    If frmServiceControl.txtAccount <> "LocalSystem" Then
'    ' Add domain name to account string
'        If InStr(1, frmServiceControl.txtAccount, "\") = 0 Then
'            DomainName = GetDomainName()
'            If DomainName = "" Then DomainName = "."
'            frmServiceControl.txtAccount.Text = DomainName & "\" & frmServiceControl.txtAccount.Text
'        End If
'    End If
    hSCManager = OpenSCManager(vbNullString, vbNullString, _
                           SC_MANAGER_CREATE_SERVICE)
    If hSCManager <> 0 Then
    ' Install service to manual start. To set service to autostart
    ' replace SERVICE_DEMAND_START to SERVICE_AUTO_START
        hService = CreateService(hSCManager, strServiceName, _
                           strDisplayName, SERVICE_ALL_ACCESS, _
                           SERVICE_WIN32_OWN_PROCESS, _
                           SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _
                           strServiceExeFilePath, vbNullString, _
                           vbNullString, vbNullString, strAccount, _
                           strPassword)
        '<Note>:
        '1. strAccount can be like .\Administrator
        '2. Can use vbNullString instead of strAccount (strAccount = "LocalSystem")
       
        If hService <> 0 Then
            CloseServiceHandle hService
        Else
            InstallSvc = Err.LastDllError
        End If
        CloseServiceHandle hSCManager
    Else
        InstallSvc = Err.LastDllError
    End If
End Function

' This function uninstalls service
' It returns nonzero value on error
Public Function UnInstallSvc(strServiceName As String) As Long
    Dim hSCManager As Long
    Dim hService As Long, Status As SERVICE_STATUS
   
    hSCManager = OpenSCManager(vbNullString, vbNullString, _
                           SC_MANAGER_CONNECT)
    If hSCManager <> 0 Then
        hService = OpenService(hSCManager, strServiceName, _
                           SERVICE_ALL_ACCESS)
        If hService <> 0 Then
    ' Stop service if it is running
            ControlService hService, SERVICE_CONTROL_STOP, Status
            If DeleteService(hService) = 0 Then
                UnInstallSvc = Err.LastDllError
            End If
            CloseServiceHandle hService
        Else
            UnInstallSvc = Err.LastDllError
        End If
        CloseServiceHandle hSCManager
    Else
        UnInstallSvc = Err.LastDllError
    End If
End Function

Public Sub StopSvc(strServiceName As String, strServerName As String)
   'Left strServerName = "" for local service
   App.LogEvent "Stop Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   Dim ret As Long
   
   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 PauseSvc(strServiceName As String, strServerName As String)
   'Left strServerName = "" for local service
   App.LogEvent "Stop Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   Dim ret As Long
   
   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_PAUSE, ssStatus
   End If
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Sub

Public Sub ContinueSvc(strServiceName As String, strServerName As String)
   'Left strServerName = "" for local service
   App.LogEvent "Stop Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   Dim ret As Long
   
   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_CONTINUE, ssStatus
   End If
   CloseServiceHandle lngSvcHandle
   CloseServiceHandle lngSMHandle
End Sub

Public Sub StartSvc(strServiceName As String, strServerName As String)
   'Left strServerName = "" for local service
   App.LogEvent "Start Service : " & strServiceName
   Dim lngSMHandle As Long
   Dim lngSvcHandle As Long
   Dim ssStatus As SERVICE_STATUS
   Dim ret As Long
   
   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

Public Sub LoadService(ByRef lst As ListBox)
    Dim hSCM As Long, lpEnumServiceStatus() As ENUM_SERVICE_STATUS, lngServiceStatusInfoBuffer As Long
    Dim strServiceName As String * 250, lngBytesNeeded As Long, lngServicesReturned As Long
    Dim hNextUnreadEntry As Long, lngStructsNeeded As Long, lngResult As Long, i As Long
    'Open connection to Service Control Manager.
    hSCM = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ENUMERATE_SERVICE)
    If hSCM = 0 Then
        MsgBox "OpenSCManager failed. LastDllError = " & CStr(Err.LastDllError)
        Exit Sub
    End If
    'Get buffer size (bytes) without passing a buffer
    'and make sure starts at 0th entry.
    hNextUnreadEntry = 0
    lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, ByVal &H0, &H0, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
    'We should receive MORE_DATA error.
    If Not Err.LastDllError = ERROR_MORE_DATA Then
        MsgBox "LastDLLError = " & CStr(Err.LastDllError)
        Exit Sub
    End If
    'Calculate the number of structures needed.
    lngStructsNeeded = lngBytesNeeded / Len(lpEnumServiceStatus(0)) + 1
    'Redimension the array according to our calculation.
    ReDim lpEnumServiceStatus(lngStructsNeeded - 1)
    'Get buffer size in bytes.
    lngServiceStatusInfoBuffer = lngStructsNeeded * Len(lpEnumServiceStatus(0))
    'Get services information starting entry 0.
    hNextUnreadEntry = 0
    lngResult = EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_ACTIVE Or SERVICE_INACTIVE, lpEnumServiceStatus(0), lngServiceStatusInfoBuffer, lngBytesNeeded, lngServicesReturned, hNextUnreadEntry)
    If lngResult = 0 Then
        MsgBox "EnumServicesStatus failed. LastDllError = " & CStr(Err.LastDllError)
        Exit Sub
    End If
    'Get the strings and display them.
    lst.Clear
    For i = 0 To lngServicesReturned - 1
        lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpServiceName)
        'lst.AddItem StripTerminator(strServiceName) + " - " & lpEnumServiceStatus(i).ServiceStatus.dwCurrentState
        lst.AddItem Left$(strServiceName, InStr(1, strServiceName, Chr$(0)) - 1) & " - " & GetServiceStatusWithStatusNumber(lpEnumServiceStatus(i).ServiceStatus.dwCurrentState)
        'lngResult = lstrcpy(ByVal strServiceName, ByVal lpEnumServiceStatus(i).lpDisplayName)
        'Me.Print StripTerminator(strServiceName)
        'Me.Print lpEnumServiceStatus(i).ServiceStatus.dwCurrentState
    Next i
    'Clean up.
    CloseServiceHandle (hSCM)
   
End Sub


0
 
LVL 1

Author Comment

by:bluedragon99
Comment Utility
aaaaaaaaaaaarrrggggggggggg how do you tell the SCM that your app is running!??
0
 
LVL 22

Accepted Solution

by:
cookre earned 500 total points
Comment Utility
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Here's MS's sample:

// Initialization complete - report running status.
MyServiceStatus.dwCurrentState       = SERVICE_RUNNING;
MyServiceStatus.dwCheckPoint         = 0;
MyServiceStatus.dwWaitHint           = 0;
 
if (!SetServiceStatus (MyServiceStatusHandle, &MyServiceStatus))
   {
   status = GetLastError();
   SvcDebugOut(" [MY_SERVICE] SetServiceStatus error %ld\n",status);
   }
 
// This is where the service does its work.
 
The above is from:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/writing_a_servicemain_function.asp?frame=true

The start of the topic is:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/service_program_tasks.asp?frame=true
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
If your service is expected to exceed the 30 second timeout for sending the SERVICE_RUNNING, you temporize by sending a START_PENDING with a hint (how much more time you expect to use before setting the RUNNING status).
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

15 Experts available now in Live!

Get 1:1 Help Now