Say is every learning a bit of knowledge on the record, the previous time also did not make, or oneself lazy, today forced to write their own, when is in review, nonsense less said, into the topic.
The Windows Service program needs to service_table_entry the struct (describing the service name and the function Eg:servicemain that corresponds to the start), and then startservicectrldispatcher to invoke the previous struct. In the ServiceMain function to register the service state control method, or you can not control the initialization of services, startup, etc., this is the basic service program
The main code is as follows (only the service program, the following will be explained installation, uninstallation, etc.):
1 //defining the Global service state2 StaticService_status Serverstatus = {0};3 //ServiceName is the service name, ServiceMain is the service name corresponding to the execution method, there is nothing to do to add to this function4 intMainintargcChar*argv[]) {5Service_table_entry serverentry[] ={{(LPSTR) ServiceName, (lpservice_main_function) ServiceMain}, {null, NULL}};6 Startservicectrldispatchera (serverentry);7 }8 9 voidWINAPI ServiceMain ()Ten { One //Initializing the service configuration AServerstatus.dwservicetype =service_win32_own_process; -Serverstatus.dwcurrentstate =service_running; -serverstatus.dwcontrolsaccepted =Service_accept_stop; theServerstatus.dwwin32exitcode =S_OK; -Serverstatus.dwservicespecificexitcode =0; -Serverstatus.dwcheckpoint =0; -Serverstatus.dwwaithint =0; + //Registration Control -Servicestatushwnd =RegisterServiceCtrlHandler (ServiceName, Servicestrl); + A //The default setting service is the boot state service_running atSetServiceStatus (Servicestatushwnd, &serverstatus); - - //in this implementation of their own code, if you want to keep the service to run, remember to block below to do the following Stop command, if you do not add the following service stops, and does not block, the service is reported abnormal - - -Serverstatus.dwcurrentstate =service_stopped; inSetServiceStatus (Servicestatushwnd, &serverstatus); - } to + //Control Services - voidWINAPI Servicestrl (DWORD dwopcode) the { * Switch(Dwopcode) $ {Panax Notoginseng CaseService_control_stop: - Break; the CaseService_control_shutdown: + Break; A } the}
Almost just this one service program is good, the following is the installation services, etc.:
1 //The installation service encapsulates an easy way to pass in a service program.2 BOOLEAN Install (PCHAR szfilepath)3 {4 //Open the Service Control Manager5Sc_handle HSCM =OpenSCManager (null, NULL, sc_manager_all_access);6 if(HSCM = =NULL)7 {8LogEvent (TEXT ("couldn ' t Open Service Manager"));9 returnFALSE;Ten } One A //Create a service -Sc_handle Hservice =CreateService ( - HSCM, ServiceName, ServiceName, the service_all_access, Service_win32_own_process, - Service_Auto_Start, Service_error_normal, -Szfilepath, NULL, NULL, TEXT (""), NULL, NULL); - + if(Hservice = =NULL) - { + Closeservicehandle (HSCM); ALogEvent (TEXT ("couldn ' t create service")); at returnFALSE; - } - - //Change Service Description - service_description DESCRIPTION; -TCHAR szdescription[1024x768]; inZeroMemory (Szdescription,1024x768); -ZeroMemory (&description,sizeof(service_description)); tolstrcpy (Szdescription, TEXT ("Description of the service-side control program.")); +Description.lpdescription =szdescription; -ChangeServiceConfig2 (Hservice, Service_config_description, &Description); the * //Start Service $StartService (Hservice,0, NULL);Panax Notoginseng - Closeservicehandle (hservice); the Closeservicehandle (HSCM); + returnTRUE; A } the //Uninstall of service (stop service first, delete service) + BOOLEAN Uninstall () - { $Sc_handle HSCM =OpenSCManager (null, NULL, sc_manager_all_access); $ - if(HSCM = =NULL) - { theLogEvent (TEXT ("couldn ' t Open Service Manager")); - returnFALSE;Wuyi } the -Sc_handle Hservice = OpenService (HSCM, ServiceName, Service_stop |DELETE); Wu - if(Hservice = =NULL) About { $ Closeservicehandle (HSCM); -LogEvent (TEXT ("couldn ' t open service")); - returnFALSE; - } A service_status STATUS; +ControlService (Hservice, Service_control_stop, &status); the - //Delete Service $BOOLEAN bdelete =DeleteService (hservice); the Closeservicehandle (hservice); the Closeservicehandle (HSCM); the the if(bdelete) - { in returnTRUE; the } the AboutLogEvent (TEXT ("Service could not being deleted")); the returnFALSE; the}
Programming and installation of service program in C language under Windows