Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / Visual Basic

Install a Windows service the way YOU want to!

Rate me:
Please Sign up or sign in to vote.
4.94/5 (36 votes)
26 Nov 20033 min read 271.3K   1.1K   94  
Shows how to change settings for a windows service during installation.
Imports System.ComponentModel
Imports System.Configuration.Install

<RunInstaller(True)> Public Class ProjectInstaller
    Inherits System.Configuration.Install.Installer

#Region " Component Designer generated code "

	Public Sub New()
		MyBase.New()

		'This call is required by the Component Designer.
		InitializeComponent()

		'Add any initialization after the InitializeComponent() call

	End Sub

	'Installer overrides dispose to clean up the component list.
	Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
		If disposing Then
			If Not (components Is Nothing) Then
				components.Dispose()
			End If
		End If
		MyBase.Dispose(disposing)
	End Sub

	'Required by the Component Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Component Designer
	'It can be modified using the Component Designer.  
	'Do not modify it using the code editor.
	Friend WithEvents ServiceProcessInstaller1 As System.ServiceProcess.ServiceProcessInstaller
	Friend WithEvents ServiceInstaller1 As System.ServiceProcess.ServiceInstaller
	<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
		Me.ServiceProcessInstaller1 = New System.ServiceProcess.ServiceProcessInstaller
		Me.ServiceInstaller1 = New System.ServiceProcess.ServiceInstaller
		'
		'ServiceProcessInstaller1
		'
		Me.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem
		Me.ServiceProcessInstaller1.Password = Nothing
		Me.ServiceProcessInstaller1.Username = Nothing
		'
		'ServiceInstaller1
		'
		Me.ServiceInstaller1.DisplayName = "ServiceTest"
		Me.ServiceInstaller1.ServiceName = "ServiceTest"
		Me.ServiceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic
		'
		'ProjectInstaller
		'
		Me.Installers.AddRange(New System.Configuration.Install.Installer() {Me.ServiceProcessInstaller1, Me.ServiceInstaller1})

	End Sub

#End Region

	Private Sub ProjectInstaller_AfterInstall(ByVal sender As Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles MyBase.AfterInstall
		'Our code goes in this event because it is the only one that will do
		'a proper job of letting the user know that an error has occurred,
		'if one indeed occurs. Installation will be rolled back if an error occurs.

		Dim iSCManagerHandle As Integer
		Dim iServiceHandle As Integer
		Dim bChangeServiceConfig As Boolean
		Dim bChangeServiceConfig2 As Boolean
		Dim ServiceDescription As modAPI.SERVICE_DESCRIPTION

		Dim bCloseService As Boolean
		Dim bCloseSCManager As Boolean

		Try
			'Obtain a handle to the Service Control Manager, with appropriate rights.
			'This handle is used to open the relevant service.
			iSCManagerHandle = modAPI.OpenSCManager(vbNullString, vbNullString, modAPI.ServiceControlManagerType.SC_MANAGER_ALL_ACCESS)
			'Check that it's open. If not throw an exception.
			If iSCManagerHandle < 1 Then
				Throw New Exception("Unable to open the Services Manager.")
			End If
			'Obtain a handle to the relevant service, with appropriate rights.
			'This handle is sent along to change the settings. The second parameter
			'should contain the name you assign to the service.
			iServiceHandle = modAPI.OpenService(iSCManagerHandle, "ServiceTest", modAPI.ACCESS_TYPE.SERVICE_ALL_ACCESS)
			'Check that it's open. If not throw an exception.
			If iServiceHandle < 1 Then
				Throw New Exception("Unable to open the Service for modification.")
			End If
			'Call ChangeServiceConfig to update the ServiceType to SERVICE_INTERACTIVE_PROCESS.
			'Very important is that you do not leave out or change the other relevant
			'ServiceType settings. The call will return False if you do.
			'Also, only services that use the LocalSystem account can be set to
			'SERVICE_INTERACTIVE_PROCESS.
			bChangeServiceConfig = modAPI.ChangeServiceConfig(iServiceHandle, modAPI.ServiceType.SERVICE_WIN32_OWN_PROCESS + modAPI.ServiceType.SERVICE_INTERACTIVE_PROCESS, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, vbNullString, vbNullString, vbNullString, vbNullString, vbNullString, vbNullString, vbNullString)
			'If the call is unsuccessful, throw an exception.
			If Not bChangeServiceConfig Then
				Throw New Exception("Unable to change the Service settings.")
			End If
			'To change the description, create an instance of the SERVICE_DESCRIPTION
			'structure and set the lpDescription member to your desired description.
			ServiceDescription.lpDescription = "This is my custom description for my Windows Service Application!"
			'Call ChangeServiceConfig2 with SERVICE_CONFIG_DESCRIPTION in the second
			'parameter and the SERVICE_DESCRIPTION instance in the third parameter
			'to update the description.
			bChangeServiceConfig2 = modAPI.ChangeServiceConfig2(iServiceHandle, modAPI.InfoLevel.SERVICE_CONFIG_DESCRIPTION, ServiceDescription)
			'If the update of the description is unsuccessful it is up to you to
			'throw an exception or not. The fact that the description did not update
			'should not impact the functionality of your service.
			If Not bChangeServiceConfig2 Then
				Throw New Exception("Unable to set the Service description.")
			End If
		Catch ex As Exception
			Throw ex
		End Try

		'Once you are done you should close/release the handles to the service
		'and the Service Control Manager.
		bCloseService = modAPI.CloseServiceHandle(iServiceHandle)
		bCloseSCManager = modAPI.CloseServiceHandle(iSCManagerHandle)

		'When installation is done go check out your handy work using Computer Management!
	End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United Kingdom United Kingdom
I have been a VB developer since 1999 and started on .NET 3 months before it was released (on the beta).

I love what I do and even though I always write applications that work with data in one way or another for the company I work for, I love working on things like encryption, Windows API, graphics (trying out managed DirectX) and anything else that the big corporates hardly have an interest in.

Comments and Discussions