65.9K
CodeProject is changing. Read more.
Home

SetServiceDescription

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.83/5 (7 votes)

Jul 12, 2006

CPOL
viewsIcon

23764

downloadIcon

76

A method for setting the "description" of a Windows Service.

Introduction

When you create a Windows Service by deriving from System.ServiceProcess.ServiceBase, you can set various properties of the service, but not the value that gets displayed in the "Description" column of the "Computer Management" screen. This is how to set it by using the Windows API rather than mucking about in the registry.

How It's Done

The Windows API provides several methods for working with the Service Control Manager; these are documented here.

The source code included with this article does the following:

  • Calls OpenSCManager to connect to the service control manager
  • Calls OpenService to connect to the requested service
  • Calls ChangeServiceConfig2w to set the description (this is the Unicode version of the function)
  • Calls CloseServiceHandle to close the service and service control manager

Using the Code

Add the included file to your project.

In classes that derive from System.ServiceProcess.ServiceBase, add something like the following:

public void AfterInstallEventHandler
    (object sender, System.Configuration.Install.InstallEventArgs e)
{
    SetServiceDescription ( ServiceName , "This is my service" ) ;
}

In classes that derive from System.Configuration.Install.Installer, after instantiating each System.ServiceProcess.ServiceInstaller, add something like the following:

serviceInstaller.AfterInstall += new System.Configuration.Install.InstallEventHandler
    ( myservice.AfterInstallEventHandler ) ;

History

  • 2006-06-03: First posted
  • 2008-01-07: Updated