Code: Select all
#Include "windows.bi"
'#define DEBUGMODE
'******* Service Installer **** // Windows service installer by Leonardo Neumann <karak.neumann@gmail.com>
Type TInstaller
Public:
Declare Constructor()
Declare Destructor()
Declare Function InstallSvc(SvcExeName As String,SvcName As String,AutoRecover As BOOLEAN = FALSE) As BOOLEAN
Declare Function UnInstall() As BOOLEAN
LastError As String
FileName As String
FilePath As String
ServiceName As String
End Type
'******* Service Installer ****
Constructor TInstaller()
End Constructor
Destructor TInstaller()
DeleteFile(StrPtr(this.FilePath))
End Destructor
Function TInstaller.InstallSvc(SvcExeName As String,SvcName As String,AutoRecover As BOOLEAN = FALSE) As BOOLEAN
this.FilePath = CurDir() & "\" & SvcExeName
this.FileName = SvcExeName
this.ServiceName = SvcName
Dim hSC_Man As SC_HANDLE
Dim hSC_Service As SC_HANDLE
hSC_Man = OpenSCManager(NULL,NULL,GENERIC_READ or GENERIC_WRITE)
If hSC_Man = NULL Then
#ifdef DEBUGMODE
?"Service.TInstaller.InstallSvc(): OpenSCManager() = NULL"
#endif
Return FALSE
EndIf
hSC_Service = OpenService(hSC_Man,ServiceName,SERVICE_ALL_ACCESS)
If hSC_Service = NULL Then 'service did not exists
hSC_Service = CreateService(hSC_Man,ServiceName,ServiceName,SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,StrPtr(FilePath),0,0,0,0,0)
If hSC_Service = NULL Then
#ifdef DEBUGMODE
?"Service.TInstaller.InstallSvc(): CreateService() = NULL"
#endif
Return FALSE
EndIf
Else
'if it already existed, change the config to the new .sys location
If ChangeServiceConfig(hSC_Service,SERVICE_WIN32_OWN_PROCESS,SERVICE_DEMAND_START,SERVICE_ERROR_NORMAL,StrPtr(FilePath),0,0,0,0,0,ServiceName) = 0 Then
#ifdef DEBUGMODE
?"Service.TInstaller.InstallSvc(): ChangeServiceConfig() = 0"
#endif
CloseServiceHandle(hSC_Service)
Return FALSE
EndIf
If AutoRecover = TRUE Then
Dim svcrecoveropt1 As SERVICE_FAILURE_ACTIONS
Dim scaction As SC_ACTION
scaction.Type = SC_ACTION_RESTART
scaction.Delay = 5000
svcrecoveropt1.dwResetPeriod = INFINITE
svcrecoveropt1.lpRebootMsg = NULL
svcrecoveropt1.lpCommand = NULL
svcrecoveropt1.cActions = 1
svcrecoveropt1.lpsaActions = @scaction
If ChangeServiceConfig2(hSC_Service,SERVICE_CONFIG_FAILURE_ACTIONS,@svcrecoveropt1) = FALSE Then
#ifdef DEBUGMODE
?"Service.TInstaller.InstallSvc(): ChangeServiceConfig2() = 0"
#endif
EndIf
EndIf
EndIf
If StartService(hSC_Service,0,NULL) = 0 Then
If GetLastError() <> ERROR_SERVICE_ALREADY_RUNNING Then
#ifdef DEBUGMODE
?"Service.TInstaller.InstallSvc(): StartService() = 0"
#endif
CloseServiceHandle(hSC_Service)
Return FALSE
EndIf
EndIf
'this.hService = hSC_Service
CloseServiceHandle(hSC_Service)
Return TRUE
End Function
Function TInstaller.UnInstall() As BOOLEAN
Dim hSC_temp As SC_HANDLE
Dim hSC_temp2 As SC_HANDLE
Dim stat As SERVICE_STATUS
hSC_temp = OpenSCManager(0,0,GENERIC_READ Or GENERIC_WRITE)
If hSC_temp = NULL Then
#ifdef DEBUGMODE
?"Service.TInstaller.UnInstall(): OpenSCManager() = NULL"
#endif
Return FALSE
EndIf
hSC_temp2 = OpenService(hSC_temp,ServiceName,SERVICE_ALL_ACCESS)
If hSC_temp2 = NULL Then
#ifdef DEBUGMODE
?"Service.TInstaller.UnInstall(): OpenService() = NULL"
#endif
Return FALSE
EndIf
If ControlService(hSC_temp2,SERVICE_CONTROL_STOP,@stat) = 0 Then
#ifdef DEBUGMODE
?"Service.TInstaller.UnInstall(): Service were stopped (SERVICE_CONTROL_STOP = 0)"
#endif
EndIf
If DeleteService(hSC_temp2) = 0 Then
#ifdef DEBUGMODE
?"Service.TInstaller.UnInstall(): Service were deleted."
#endif
EndIf
CloseServiceHandle(hSC_temp2)
CloseServiceHandle(hSC_temp)
Return TRUE
End Function
usage:
Code: Select all
Dim oInstaller As TInstaller
If oInstaller.InstallSvc("myservice.exe","My Service",TRUE) = FALSE Then
?"could not install windows service"
Sleep:End
EndIf
Thats it... i needed this for a project of mine so im posting it here , maybe its useful for anyone else.
I will implement the start-stop routines if anyone needs them.