NT Service HELP Please
Posted: Fri Jan 01, 2010 8:02 pm
I've been searching allover and coul'nt find an answer so I decided to ask the experts...
This is a service code whitvch works great:
But it doesn't report when service is started on the Service_CtrlHandler() procedure. How do I do that?
Note:
I'vr seen services code where is specified a user routine and a notify routine, like this: RunService("MyTestSvc", @ServiceFunction(), @ServiceNotify()), but lacks the install/start/stop/remove options which I need in this one. Any way to have both?
Thank You
This is a service code whitvch works great:
Code: Select all
Declare Main()
Declare Service_Install()
Declare Service_Remove()
Declare Service_Start()
Declare Service_Stop()
Declare Service_MainLoop()
Declare Service_CtrlHandler(CtrlRequest.l)
Declare WriteToLog(entry.s)
Structure SVC_DESCRIPTION
lpDescription.s
EndStructure
AppPath.s = ProgramFilename()
Global LogFile.s = GetPathPart(AppPath) + ReplaceString(GetFilePart(AppPath), ".exe",".log")
#MyService_Name = "NetTax"
#MyService_AppName = "NetTaxSvc.exe"
#MyService_DisplayName = "NetTax"
#MyService_Description = "NetTax Description"
Global ServiceStatus.SERVICE_STATUS
Global hStatus.l
Global Buffer.b
Procedure Main()
Select ProgramParameter(0)
Case "-i"
Service_Install()
Case "-r"
Service_Remove()
Case "-s"
Service_Start()
Case "-k"
Service_Stop()
EndSelect
EndProcedure
Procedure WriteToLog(EventText.s)
Protected hFile.l
EventDate.s = FormatDate("%dd-%mm-%yyyy %hh:%ii:%ss", Date())
hFile = OpenFile(#PB_Any, logfile)
If hFile = #Null
ProcedureReturn #False
EndIf
FileSeek(hFile, Lof(hFile))
WriteStringN(hFile, EventDate + " - " + EventText)
CloseFile(hFile)
ProcedureReturn #True
EndProcedure
Procedure Service_Install()
Protected sDir.s
Protected hSCManager.l, hService.l
Protected SD.SVC_DESCRIPTION
sDir = GetCurrentDirectory() + #MyService_AppName
hSCManager = OpenSCManager_(#Null, #Null, #SC_MANAGER_ALL_ACCESS)
hService = CreateService_(hSCManager, #MyService_Name, #MyService_DisplayName, #SERVICE_ALL_ACCESS, #SERVICE_WIN32_OWN_PROCESS, #SERVICE_AUTO_START, #SERVICE_ERROR_NORMAL, sDir, #Null, #Null, #Null, #Null, #Null)
SD\lpDescription = #MyService_Description
ChangeServiceConfig2_(hService, #SERVICE_CONFIG_DESCRIPTION, @SD)
CloseServiceHandle_(hService)
WriteToLog("Service Installed")
EndProcedure
Procedure Service_Remove()
Protected hSCManager.l, hServ.l
hSCManager = OpenSCManager_(#Null, #Null, #SC_MANAGER_ALL_ACCESS)
hServ = OpenService_(hSCManager, #MyService_Name, #SERVICE_ALL_ACCESS)
DeleteService_(hServ)
CloseServiceHandle_(hServ)
CloseServiceHandle_(hSCManager)
WriteToLog("Service Removed")
EndProcedure
Procedure Service_Start()
Protected hSCManager.l, hServ.l
hSCManager = OpenSCManager_(#Null, #Null, #SC_MANAGER_ALL_ACCESS)
hServ = OpenService_(hSCManager, #MyService_Name, #SERVICE_ALL_ACCESS)
StartService_(hServ, 0, #Null)
CloseServiceHandle_(hServ)
CloseServiceHandle_(hSCManager)
WriteToLog("Service Started")
EndProcedure
Procedure Service_Stop()
Protected hSCManager.l, hServ.l
hSCManager = OpenSCManager_(#Null, #Null, #SC_MANAGER_ALL_ACCESS)
hServ = OpenService_(hSCManager, #MyService_Name, #SERVICE_ALL_ACCESS)
ControlService_(hServ, #SERVICE_CONTROL_STOP, @ServiceStatus)
CloseServiceHandle_(hServ)
CloseServiceHandle_(hSCManager)
WriteToLog("Service Stoped")
EndProcedure
Procedure Service_MainLoop()
Protected hError.l
With ServiceStatus
\dwServiceType = #SERVICE_WIN32_OWN_PROCESS
\dwCurrentState = #SERVICE_START_PENDING
\dwControlsAccepted = #SERVICE_ACCEPT_STOP | #SERVICE_ACCEPT_SHUTDOWN | #SERVICE_ACCEPT_PAUSE_CONTINUE
\dwWin32ExitCode = 0
\dwServiceSpecificExitCode = 0
\dwCheckPoint = 0
\dwWaitHint = 0
EndWith
hStatus = RegisterServiceCtrlHandler_(#MyService_Name, @Service_CtrlHandler())
If hStatus = 0
ProcedureReturn
EndIf
SetServiceStatus_(hStatus, @ServiceStatus)
ServiceStatus\dwCurrentState = #SERVICE_RUNNING
SetServiceStatus_(hStatus, @ServiceStatus)
; Main Loop
Repeat
; Service Executing Code
WritetoLog("Looping on Service Executing Code")
Delay(10000)
Until ServiceStatus\dwCurrentState <> #SERVICE_RUNNING
ServiceStatus\dwCurrentState = #SERVICE_STOPPED
ServiceStatus\dwWin32ExitCode= -1
SetServiceStatus_(hStatus, @ServiceStatus)
ProcedureReturn #False
EndProcedure
Procedure Service_CtrlHandler(CtrlRequest.l)
Select CtrlRequest
Case #SERVICE_CONTROL_CONTINUE
WriteToLog("service continue. ")
With ServiceStatus
\dwCurrentState = #SERVICE_RUNNING
EndWith
Case #SERVICE_CONTROL_PAUSE
WriteToLog("service paused. " +)
With ServiceStatus
\dwCurrentState = #SERVICE_PAUSED
EndWith
Case #SERVICE_CONTROL_STOP
WriteToLog("service stopped. ")
With ServiceStatus
\dwWin32ExitCode = 0
\dwCurrentState = #SERVICE_STOPPED
EndWith
EndSelect
SetServiceStatus_(hStatus, @ServiceStatus);
EndProcedure
Main()
Note:
I'vr seen services code where is specified a user routine and a notify routine, like this: RunService("MyTestSvc", @ServiceFunction(), @ServiceNotify()), but lacks the install/start/stop/remove options which I need in this one. Any way to have both?
Thank You