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 ?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimCotteeHead of Software ServicesCommented:
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


' #VBIDEUtils#************************************************************
' * Programmer Name  : Matthew Ruffell
' * Web Site         :
' * E-Mail           :
' * 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.