• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

How can I write a program to run in NT as a service ?

How can I write a program to run in NT as a service ?
1 Solution
Any application can be run as a service under NT. There are however a number of considerations. Normally a service does not have a user interface, if it does then it cannot normally have network / database access. There are ways around this though but there is an element of security risk depending on the methods chosen. If you have or can obtain the Windows NT Resource Kit, there are full details of how to make any application run as a service. The word document srvany.doc details the methodology and the restrictions / concerns about doing this. If you cannot obtain this I can Email you the required files if you post your address or EMail me at TimCottee@Earthling.net


' #VBIDEUtils#************************************************************
' * Programmer Name  : Matthew Ruffell
' * Web Site         : www.geocities.com/ResearchTriangle/6311/
' * E-Mail           : waty.thierry@usa.net
' * Date             : 29/09/99
' * Time             : 11:10
' **********************************************************************
' * Comments         : Install Windows NT Service
' *  Install a Windows NT service on a local or remote server.
' *   Configure how the service is installed. Requires Windows NT
' *   and administrator rights.
' *
' *  ServiceFileName [string] = binary service file path and name,
' *  ServiceName [string] = name of service,
' *  DisplayName [string] = unofficial name of service,
' *  Interactive [boolean] = communicates with desktop,
' *  AutoStart [boolean] = run when system starts,
' *  MachineName [optional string] = target server name

' **********************************************************************
   dwServiceType As Long
   dwCurrentState As Long
   dwControlsAccepted As Long
   dwWin32ExitCode As Long
   dwServiceSpecificExitCode As Long
   dwCheckPoint As Long
   dwWaitHint As Long
End Type

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

Private Declare Function CreateService Lib "advapi32.dll" 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, _
   lpdwTagId As Long, _
   ByVal lpDependencies As String, _
   ByVal lpServiceStartName As String, _
   ByVal lpPassword As String) As Long

Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long

Public Function Install_SVC(strServiceFileName As String, strServiceName As String, strDisplayName As String, bolInteractive As Boolean, bolAutoStart As Boolean, Optional strMachineName As Variant, Optional strAccount As Variant, Optional strAccountPassword As Variant) As Boolean

   Dim hSCM As Long
   Dim hSVC As Long
   Dim lngInteractive As Long
   Dim lngAutoStart As Long
   If bolInteractive = True Then lngInteractive = (&H100 Or &H10) Else lngInteractive = &H10
   If bolAutoStart = True Then lngAutoStart = &H2 Else lngAutoStart = &H3
   If IsMissing(strMachineName) = True Then strMachineName = vbNullString Else strMachineName = CStr(strMachineName)
   If IsMissing(strAccount) = True Then strAccount = vbNullString Else strAccount = CStr(strAccount)
   If IsMissing(strAccountPassword) = True Then strAccountPassword = vbNullString Else strAccountPassword = CStr(strAccountPassword)

   '// Open the service manager
   hSCM = OpenSCManager(strMachineName, vbNullString, &H2)
   If hSCM = 0 Then Exit Function '// Error opening

   '// Install the service
   hSVC = CreateService(hSCM, _
      strServiceName, _
      strDisplayName, _
      983551, _
      lngInteractive, _
      lngAutoStart, _
      0, _
      strServiceFileName, _
      vbNull, _
      vbNull, _
      vbNullString, _
      strAccount, _

   If hSVC <> 0 Then Install_SVC = True

   Call CloseServiceHandle(hSVC)
   Call CloseServiceHandle(hSCM)
End Function
stanleylcmAuthor Commented:
Hi waty !
Thanks for your answer.
However, I have some problem, actually my prog is a backup
program which will use a time control to continuously run,
when i set it run as a service, it can run, but quit after some
time ( about 2 min )
and when i look up the event viewer,
it got "Timeout (120000 milliseconds) waiting for service to connect"
Pls help
stanleylcmAuthor Commented:
Bought the question.

I have the same problem that you last described, what did you do to get around the timeout?

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now