Create a simple Win32 service program with C + +

Source: Internet
Author: User
Tags command line log rar system log win32

Source code compression Package There are three RAR files in the PRC

Ntservice.rar for ntservice Example source code

Ntservicecpl.rar for ntservcpl Example source code

Ntservicectrl.rar for Ntservctrl Example source code

Summary

This article describes how to create a Windows NT service program in Visual C + +. Creating the service uses only a C + + class, which provides a simple interface between the service and the operating system. Using this class to implement your own service is very simple, just rewrite the virtual functions in a few of the base classes. There are three source code reference examples in this article:

NTservice is a simple Win32 service, which is established by the method described in this paper;

Ntservcpl is a control surface program that is used to control ntservice services;

Ntservctrl is an independent program example that can be used to monitor a WIN32 service;

Brief introduction

A service in Windows NT is actually a program that the service can run as soon as the computer operating system starts. It does not require user login. A service program is a user-independent task, such as directory replication, process monitoring, or services that are used by other machines on the network, such as HTTP protocol support.

It is not difficult to create Windows NT service programs. But debugging a service program is not an easy task. For my own part, I like to write my own C + + program in Visual C + +. Most Win32 services are written in C, so I think it would be interesting to use a C + + class to implement the basic functions of WIN32 services. With this C + + class, who wants to create a WIN32 service in C + + is a very simple thing. I've developed a C + + base class for this, and there's no big problem with it as a starting point for writing Win32 services.

In addition to writing service code, the Create service program must do some additional coding work:

Warning and error messages are reported in the System log or application log and cannot be exported to the screen because the user is not logged in at all.

The control of a service program can be either through a separate application or through a control surface program. It depends on what kind of communication mechanism your service implements.

Installing and uninstalling services from the system

Most service programs are installed using one installer, and are uninstalled using a different program. In this article I built these functions within the service program itself to make it integrated so that only one is distributed. EXE file. You can run the service program directly from the command line, and you can install and uninstall or report its version information as you wish. NTservice supports the following command-line arguments:

-V, the name and version number of the reporting service;

-I, installation services;

-U, uninstall service;

By default, there is no command-line parameter passing when the system starts the service.

Creating an application framework

I have always created an MFC based application. When I first contacted the WIN32 service program, I first created a SDI/MFC program with Visual C + + AppWizard. Then remove the document and view classes, icons, and other useless items, leaving only the frame. Results to the end of everything, including the main window (the service program can not have this stuff), nothing left, very stupid. I had to go back to AppWizard and create a console program with a single source file that contained the main entry function, which I named the file NTServApp.cpp. I use this CPP extension instead of C, because I just want to write the program in C + + instead of directly using C. We'll discuss the file code implementation later.

Because I wanted to build the service with C + + classes, I created the NTService.h and NTService.cpp files to implement the Cntservice base class. I also created MyService.h and MyService.cpp files to implement my own service class (Cmyservice), which derives from Cntservice. We'll see the code later.

When I set up a new project, I liked to see the results as soon as possible, so I decided that the first thing the service program would do was to create a system application log record. With this logging mechanism, I can track the service when it starts, when it stops, and so on. I can also log any error messages that occur in the service. Creating this log record is a lot more complicated than I thought.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.