Click to See Complete Forum and Search --> : WinNT services


November 5th, 1999, 05:12 AM
There's a control that lets you run a VB5 Application
as a service on WinNT. Have someone tried it?
Could you tell me if you must be Administrator to install
a VB service using that control?
Thanks

SulSDK
November 5th, 1999, 05:18 AM
Hi Anonymous,
Yes I tried that control,its NTService.ocx and you can get that control forma the MSDN samples, And you can make your own NT Service by APIs (try to search on ServiceMain callbak), but you must compile your code in PCode format.
Best Regards
SulSDK

November 5th, 1999, 06:12 AM
Oh thanks SDK, but you didnt answer the
second question, can one install it
without loggin' in as administrator?

November 5th, 1999, 06:27 PM
You will need the necessary permissions to register a component - this doesn't necessarily mean "administrator" rights - however, with most business networks, it usually does.

SulSDK
November 7th, 1999, 01:40 AM
Hi Anonymous,
What I know is that the NTService is an EXE with special Callback APIs, So you dont have even to login, you just have to put the service startup option to be Automatic to run that NTService.
But Of course you need to install that NTService first
Best Regards
SulSDK

November 8th, 1999, 03:57 AM
The installation will probably require changes in the Registry, i guess.
So if the Admin is the only one who has the rights to do that i think
either i ask him or put the service in the trash can.
What do you think about it?

SulSDK
November 9th, 1999, 01:50 AM
Hi Anonymous,
This is an example of an NTService with VB
type MyService.exe install to install the service
type MyService.exe uninstall to uninstall the service
Add the following code to a .BAS module:


option Explicit

private Const SERVICE_WIN32_OWN_PROCESS = &H10&
private Const SERVICE_WIN32_SHARE_PROCESS = &H20&
private Const SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS + _
SERVICE_WIN32_SHARE_PROCESS

private Const SERVICE_ACCEPT_STOP = &H1
private Const SERVICE_ACCEPT_PAUSE_CONTINUE = &H2
private Const SERVICE_ACCEPT_SHUTDOWN = &H4

private Const SC_MANAGER_CONNECT = &H1
private Const SC_MANAGER_CREATE_SERVICE = &H2
private Const SC_MANAGER_ENUMERATE_SERVICE = &H4
private Const SC_MANAGER_LOCK = &H8
private Const SC_MANAGER_QUERY_LOCK_STATUS = &H10
private Const SC_MANAGER_MODIFY_BOOT_CONFIG = &H20

public Const STANDARD_RIGHTS_REQUIRED = &HF0000
private Const SERVICE_QUERY_CONFIG = &H1
private Const SERVICE_CHANGE_CONFIG = &H2
private Const SERVICE_QUERY_STATUS = &H4
private Const SERVICE_ENUMERATE_DEPENDENTS = &H8
private Const SERVICE_START = &H10
private Const SERVICE_STOP = &H20
private Const SERVICE_PAUSE_CONTINUE = &H40
private Const SERVICE_INTERROGATE = &H80
private Const SERVICE_USER_DEFINED_CONTROL = &H100
private 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)

private Const SERVICE_DEMAND_START as Long = &H3

private Const SERVICE_ERROR_NORMAL as Long = &H1

private Enum SERVICE_CONTROL
SERVICE_CONTROL_STOP = &H1
SERVICE_CONTROL_PAUSE = &H2
SERVICE_CONTROL_CONTINUE = &H3
SERVICE_CONTROL_INTERROGATE = &H4
SERVICE_CONTROL_SHUTDOWN = &H5
End Enum

private Enum SERVICE_STATE
SERVICE_STOPPED = &H1
SERVICE_START_PENDING = &H2
SERVICE_STOP_PENDING = &H3
SERVICE_RUNNING = &H4
SERVICE_CONTINUE_PENDING = &H5
SERVICE_PAUSE_PENDING = &H6
SERVICE_PAUSED = &H7
End Enum

private Type SERVICE_TABLE_ENTRY
lpServiceName as string
lpServiceProc as Long
lpServiceNameNull as Long
lpServiceProcNull as Long
End Type

private 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

private Declare Function StartServiceCtrlDispatcher _
Lib "advapi32.dll" Alias "StartServiceCtrlDispatcherA" _
(lpServiceStartTable as SERVICE_TABLE_ENTRY) as Long
private Declare Function RegisterServiceCtrlHandler _
Lib "advapi32.dll" Alias "RegisterServiceCtrlHandlerA" _
(byval lpServiceName as string, byval lpHandlerProc as Long) _
as Long
private Declare Function SetServiceStatus _
Lib "advapi32.dll" (byval hServiceStatus as Long, _
lpServiceStatus as SERVICE_STATUS) as Long
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, byval lpdwTagId as string, _
byval lpDependencies as string, byval lp as string, _
byval lpPassword as string) as Long
private Declare Function DeleteService _
Lib "advapi32.dll" (byval hService as Long) as Long
Declare Function CloseServiceHandle _
Lib "advapi32.dll" (byval hSCObject 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

'** Change SERVICE_NAME as needed
private Const SERVICE_NAME as string = "MyService"

private hServiceStatus as Long
private ServiceStatus as SERVICE_STATUS

Sub Main()
Dim hSCManager as Long
Dim hService as Long
Dim ServiceTableEntry as SERVICE_TABLE_ENTRY
Dim b as Boolean
Dim cmd as string

cmd = Trim(LCase(Command()))
Select Case cmd
Case "install" 'Install service on machine
hSCManager = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
hService = CreateService(hSCManager, SERVICE_NAME, _
SERVICE_NAME, SERVICE_ALL_ACCESS, _
SERVICE_WIN32_OWN_PROCESS, _
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, _
App.Path & "\" & App.EXEName, vbNullString, _
vbNullString, vbNullString, vbNullString, _
vbNullString)
CloseServiceHandle hService
CloseServiceHandle hSCManager
Case "uninstall" 'Remove service from machine
hSCManager = OpenSCManager(vbNullString, vbNullString, _
SC_MANAGER_CREATE_SERVICE)
hService = OpenService(hSCManager, SERVICE_NAME, _
SERVICE_ALL_ACCESS)
DeleteService hService
CloseServiceHandle hService
CloseServiceHandle hSCManager
Case else 'Start the service
ServiceTableEntry.lpServiceName = SERVICE_NAME
ServiceTableEntry.lpServiceProc = _
FncPtr(AddressOf ServiceMain)
b = StartServiceCtrlDispatcher(ServiceTableEntry)
End Select
End Sub

Sub ServiceMain(byval dwArgc as Long, byval lpszArgv as Long)
Dim b as Boolean

'set initial state
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS
ServiceStatus.dwCurrentState = SERVICE_START_PENDING
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP _
Or SERVICE_ACCEPT_PAUSE_CONTINUE _
Or SERVICE_ACCEPT_SHUTDOWN
ServiceStatus.dwWin32ExitCode = 0
ServiceStatus.dwServiceSpecificExitCode = 0
ServiceStatus.dwCheckPoint = 0
ServiceStatus.dwWaitHint = 0

hServiceStatus = RegisterServiceCtrlHandler(SERVICE_NAME, _
AddressOf Handler)
ServiceStatus.dwCurrentState = SERVICE_START_PENDING
b = SetServiceStatus(hServiceStatus, ServiceStatus)

'** Do Initialization Here

ServiceStatus.dwCurrentState = SERVICE_RUNNING
b = SetServiceStatus(hServiceStatus, ServiceStatus)

'** Perform tasks -- if none exit

''** If an error occurs the following should be used for shutting
''** down:
'' SetServerStatus SERVICE_STOP_PENDING
'' Clean up
'' SetServerStatus SERVICE_STOPPED
End Sub

Sub Handler(byval fdwControl as Long)
Dim b as Boolean

Select Case fdwControl
Case SERVICE_CONTROL_PAUSE
'** Do whatever it takes to pause here.
ServiceStatus.dwCurrentState = SERVICE_PAUSED
Case SERVICE_CONTROL_CONTINUE
'** Do whatever it takes to continue here.
ServiceStatus.dwCurrentState = SERVICE_RUNNING
Case SERVICE_CONTROL_STOP
ServiceStatus.dwWin32ExitCode = 0
ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING
ServiceStatus.dwCheckPoint = 0
ServiceStatus.dwWaitHint = 0 'Might want a time estimate
b = SetServiceStatus(hServiceStatus, ServiceStatus)
'** Do whatever it takes to stop here.
ServiceStatus.dwCurrentState = SERVICE_STOPPED
Case SERVICE_CONTROL_INTERROGATE
'Fall through to send current status.
Case else
End Select
'Send current status.
b = SetServiceStatus(hServiceStatus, ServiceStatus)
End Sub

Function FncPtr(byval fnp as Long) as Long
FncPtr = fnp
End Function







Best Regards
SulSDK

November 9th, 1999, 02:14 AM
Thank you SulSDK,
actually i already have one, got it from the microsoft site.
Is yours a different one?
Microsoft gives some warning about using VB apps as
service... did you come across any trouble?
I think that bypassing the 'control' doesnt change the
problem, take a look at the function 'CreateService',
if you havent the correct right i think it will probably
fail and if it wont, well, who can tell there isn't another
which will?

snchandu
December 29th, 2000, 05:58 PM
Hi SulSDK,

I have followed exactly what you have given in the code (posted on 11/9/99. In that code only modification I made is "Private Const SERVICE_NAME as string as string = "MyService" is changed to Global const service_name = "C:\abcd\abcd.exe" . Rest as it is I have copied. But it did not worked as a service . I could not see in Nt services dialog. I hjave tried through another method "NTservice1" control. I was not successful in that method also. Can any one suggest in this regard? Thanks in advance. Wish you all a happy and prosperous new Year.

Satya