Windows Service installer class

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
KaraK
Posts: 72
Joined: Sep 13, 2006 19:01
Location: Argentina

Windows Service installer class

Post by KaraK »

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.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Windows Service installer class

Post by D.J.Peters »

Looks usefull for special tasks.
Does it needs admin rights on vista or win 7 ?

Joshy
KaraK
Posts: 72
Joined: Sep 13, 2006 19:01
Location: Argentina

Re: Windows Service installer class

Post by KaraK »

yes , to use OpenSCManager it needs Administrator priviledges.
as explained in
http://msdn.microsoft.com/en-us/library ... s.85).aspx
Post Reply