Microsoft Windows Service abstraction for GLib

From the Wikipedia: a Windows service is a computer program that operates in the background. It is similar in concept to an Unix daemon. A Windows service must conform to the interface rules and protocols of the Service Control Manager, the component responsible for managing Windows services.

If you use GLib to try to implement such a program you will end up using the Win C api directly which for this case it is not something that easy to do. At NICE as part of a project we are working on, we decided to make it easier and to provide an abstraction using all the goodness from GLib for these services. Even though we have simplified a bit the design since we allow to have only one service per process, the abstraction allows to “Wrap” a GApplication and run it as a Windows Service.

In the nice-mit repository you can find two classes to make it easy to handle these services:

  • dcv-win32-service-manager: it provides some actions to handle the service manager, by allowing to request to install/uninstall/stop/start a service.
  •  dcv-win32-service: it is the main class to use, it allows to pass the GApplication that will make run and to specify the flags for the actions it will provide to the Windows Service Manager. The Service also hooks into the GApplication to provide some command line options to make it easy to run the actions provided by the manager, i.e you can launch your application as ./test –install to ask the Service Manager to install the service. As a bonus it also handles the parameter “-e or –exec” to provide a way to launch the application without the need of the Service Manager, this way you can launch your application from a terminal directly.

As example of how you would use this you can see here some code:

int main(int argc, char **argv)
{
  /* the GApplication to run */
  GApplication *app = g_application_new("com.nice-software.testservice", G_APPLICATION_FLAGS_NONE);
  /* the service wrapping the GApplication */
  DcvWin32Service *service = dcv_win32_service_new("Test DCV Service",
                                                   DCV_WIN32_SERVICE_CAN_BE_SUSPENDED |
                                                   DCV_WIN32_SERVICE_CAN_BE_STOPPED |
                                                   DCV_WIN32_SERVICE_STOP_ON_SHUTDOWN,
                                                   app);
  int status = dcv_win32_service_run(service, argc, argv);
  g_object_unref(service);
  g_object_unref(app);

  return status;
}
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Microsoft Windows Service abstraction for GLib

  1. Jens says:

    Awsome!

    *removes TODO point from his list*

  2. marc-andré says:

    Nice! 🙂 (even better would be with a build-sys and test directory, pull req accepted I suppose?)

    • Ignacio Casal Quinteiro says:

      since we are directly bundling the code in our software we just provided the classes directly without build system, but sure if you feel like pull requesting I’d be glad to have something like libgd where you can enable from configure which things to build.

Leave a Reply to marc-andré Cancel reply

Your email address will not be published. Required fields are marked *