Posted on 30-11-2012
Filed Under (Technology) by bigbang

There are a lot of articles about how to write Windows Services for managed C++ or C# code. But for native C++ coder, the situation is more Complicated. You have to install Windows Service by command line tool in you source code, and add the command line arguments in setup projects.

Here I try to explain the whole process in two steps.

1. Write native Win32 C++ code with service installation code

Unmanaged code service installations should have a command to install as a service and also uninstall. That means you need to add the service installation code in the project.

The key point of the code is the usage of the Service Control Manager ( SCM ). Please follow this example A basic Windows service in C++ (CppWindowsService) to build basic Windows Service application.

After Compilation, then you use argument -install to install the service,

CppWindowsService.exe -install

and use argument -remove to remove the service

CppWindowsService.exe -remove

. Once the service is installed you can edit the service settings in the Windows Service Manager. To access this, run the following from the run dialog or command prompt.

SERVICES.MSC

or

services.msc

2. Add custom action in setup and deploy project to install the service while installing the application

All our application should be packaged to MSI installer to deploy to users. So they can use the setup file to install our application.

There are many third party solutions to provide this package function, like InstallShield. Visual Studio 2008 also provide Setup Projects to do this job.

The key point of unmanaged code service installations is that we should have a command to install as a service and also uninstall. In a Visual Studio setup you’d add a custom action to run your exe with that command line, probably -install and something like -remove as an uninstall custom action.

If you’re not using Visual Studio then just use the ServiceInstall / ServiceControl options in your setup tool.

Let’s use setup projects in Visual Studio as example. The key point is that you need to add custom action at the end of installation. With the above code, the argument of custom action should be -install.

By following steps, we can package the exe files for MSI installer:

To add a deployment project

  1. On the File menu, point to Add, and then click New Project.
  2. In the Add New Project dialog box’s Project Type pane, open the Other Project Types node, select Setup and Deployment Projects. In the Templates pane, choose Setup Project. In the Name box, type Custom Action Installer.

    The project is added to Solution Explorer and the File System Editor is displayed.

  3. In the File System Editor, select Application Folder in the left pane. On the Action menu, point to Add, and then choose Project Output.
  4. In the Add Project Output Group dialog box, CppWindowsService will be displayed in the Project list. Select Primary Output.

    Primary Output from CppWindowsService (Active) appears in the Application Folder.

To add the custom action

  1. Select the Custom Action Installer project in Solution Explorer. On the View menu, point to Editor, and then choose Custom Actions.

    The Custom Actions Editor is displayed.

  2. In the Custom Actions Editor, select the Commit node. On the Action menu, choose Add Custom Action.
  3. In the Select Item in Project dialog box, double-click the Application Folder. Then select Primary output from CppWindowsService.

    Primary output from CppWindowsService appears under the Commit node in the Custom Actions Editor.

  4. In the Properties window, make sure that the InstallerClass property is set to False (this is the default).
  5. In the Custom Actions Editor, select the Install node and add Primary output from CppWindowsService to this node as you did for the Commit node.
  6. On the Build menu, choose Build Custom Action Installer.

Then under the Debug or Release folder of setup project, you can find the MSI file. Just run the file, you will get the Windows service installed.

Copyright © 2012, The Big Bang on Reading Busy. All rights reserved.
On republishing this post you must provide link to the original post.

Comments

[...] know how to generate Win32 Services Installer with Native Win32 C++ Code, but if we want to do it in Python, [...]


Post a Comment
Name:
Email:
Website:
Comments: