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,
and use argument -remove to remove the service
. 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.
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.
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
The project is added to Solution Explorer and the File System Editor is displayed.
Primary Output from CppWindowsService (Active) appears in the Application Folder.
To add the custom action
The Custom Actions Editor is displayed.
Primary output from CppWindowsService appears under the Commit node in the Custom Actions Editor.
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.
© 2012, The Big Bang on Reading Busy. All rights reserved.
On republishing this post you must provide link to the original post.
[…] know how to generate Win32 Services Installer with Native Win32 C++ Code, but if we want to do it in Python, […]