Always feel that VC + + is too complicated, but yesterday I read the Wangbuyang of the Internet application programming, which wrote to the background of the service program, the elaboration of a very detailed, and logical clarity, after reading a lot of understanding, so take to share with the needs of people, and corrected the original program of some errors, supplemented by some materials. There is also a C + + writing background service program ideas (not counted.) NET Service Program development model), after finishing up again sent up.
In 2000/xp, such as the NT-based operating system, there is a service manager, it manages the background process is called service.
A service is an application type that runs in the background and is similar to a UNIX background application. Service applications can usually
Provide users with functionality locally and over the network, such as client/server applications, WEB servers, database services
Server and other server-based applications.
The background service program is quietly running in the background. By registering our program as a service, we can make our program not appear
In Task Manager, and runs first with system startup, and ends with system shutdown.
The Service Control Manager is an RPC server that exposes a set of application programming interfaces that programmers can easily write to configure
Service and control the service program in the remote server.
Service programs are usually written into console-type applications, in general, a program that adheres to the interface requirements of the service Control management program
Contains the following three functions:
1, service program main function (main): Call system function StartServiceCtrlDispatcher Connector main thread to service control management program.
2. Service entry point function (ServiceMain): Performs service initialization tasks, and service processes that perform multiple services have multiple service entry functions.
3. Control Service handler function (Handler): referenced by the control distribution thread when the service program receives the control request. (Here is Service_ctrl).
Additionally, you need to install the enrollment service before the system runs this service: the Installservice function. Deleting a service requires that you first remove the service installation registration: RemoveService function.
Service Type:
Type
Description
service_file_system_driver=2
File system-driven services.
Service_kernel_driver=1
Drive services.
Service_win32_own_process=16
A service that exclusively processes.
Service_win32_share_process=32
A service that shares a process with other services.
Create a new WIN32 console program with the source file named Service.cpp. The development tool I use is vc++.net.
1. Service Program main function
After the Service Control Manager starts the service program, it waits for the service program's main function to invoke the system letter StartServiceCtrlDispatcher. A service_win32_own_process type of service should immediately call the StartServiceCtrlDispatcher function, which allows the service entry point function to complete initialization after the service has started. For service_win32_own_process type of service and all services in the program common initialization work can be done in the main function, but not more than 30 seconds. Otherwise, additional threads must be created to perform these common initialization tasks, thus ensuring that the service program main function can call the StartServiceCtrlDispatcher function in a timely manner.
[CPP] View plain copy//service program main function. #include "stdafx.h" #include "Windows.h" #define SZAPPNAME "Serversample" //Service Program name #define SZSERVICENAME "Servicesample" //logo Service internal name /internal variable bool bdebugserver=false; service_status ssStatus; service_status_handle sshstatushandle; dword dwErr=0; tchar szErr[256]; //The following functions are implemented by program Void winapi service_main (dword DWARGC, LPTSTR *LPSZARGV); Void winapi service_ctrl ( Dword dwctrlcode); void installservice (); void RemoveService (); void debugservice (INT ARGC,CHAR** ARGV); Bool reportstatustoscmgr (dword dwcurrentstate,dword dwwin32exitcode,dword Dwwaithint); void addtomessagelog (lptstr lpszmsg); Int _tmain (int argc, _tchar* argv[]) { &Nbsp;