The following global definitions is used in this sample.
C++
#define Svcname TEXT ("Svcname") service_status gsvcstatus; Service_status_handle Gsvcstatushandle; HANDLE ghsvcstopevent = NULL;
The following sample fragment is taken by the complete service sample.
C++
Purpose://Entry point for the service////parameters://dwargc-number of arguments in the LPSZARGV array// Lpszargv-array of strings. The first string is the name of//the service and subsequent strings be passed by the process//that called the S Tartservice function to start the service.////Return value://none.//void WINAPI svcmain (DWORD dwargc, LPTSTR *lpszar GV) {//Register the handler function for the service Gsvcstatushandle = RegisterServiceCtrlHandler (Svcnam E, Svcctrlhandler); if (!gsvcstatushandle) {svcreportevent (TEXT ("RegisterServiceCtrlHandler")); Return }//These service_status members remain as set here Gsvcstatus.dwservicetype = service_win32_own_process; Gsvcstatus.dwservicespecificexitcode = 0; Report initial status to the SCM reportsvcstatus (service_start_pending, No_error, 3000); Perform service-specific initialization and work. Svcinit (DWARGC, LPsZARGV);} Purpose://The service code////parameters://dwargc-number of arguments in the lpszargv array//lpszargv- Array of strings. The first string is the name of//the service and subsequent strings be passed by the process//that called the S Tartservice function to start the service.////Return value://none//void svcinit (DWORD dwargc, LPTSTR *lpszargv) { To_do:declare and set any required variables. Be sure to periodically call Reportsvcstatus () with//service_start_pending. If initialization fails, call//Reportsvcstatus with service_stopped. Create an event. The control handler function, Svcctrlhandler,//signals this event when it receives the stop control code. Ghsvcstopevent = CreateEvent (NULL,//default security attributes TRUE ,//Manual reset event FALSE,//not signaled NULL); No Name if (GHSvcstopevent = = NULL) {reportsvcstatus (service_stopped, no_error, 0); Return }//Report running status when initialization are complete. Reportsvcstatus (service_running, no_error, 0); To_do:perform work until service stops. while (1) {//Check whether to stop the service. WaitForSingleObject (Ghsvcstopevent, INFINITE); Reportsvcstatus (service_stopped, no_error, 0); Return }}////Purpose://sets the current service status and reports it to the scm.////parameters://dwcurrentstate-the C Urrent state (see Service_status)//dwwin32exitcode-the system error code//dwwaithint-estimated time for pending operation,//in milliseconds////Return value://none//void reportsvcstatus (DWORD dwcurrentstate, DWORD Dwwin32exitcode, DWORD dwwaithint) {static DWORD dwcheckpoint = 1; Fill in the service_status structure. Gsvcstatus.dwcurrentstate = theUrrentstate; Gsvcstatus.dwwin32exitcode = Dwwin32exitcode; Gsvcstatus.dwwaithint = Dwwaithint; if (dwcurrentstate = = service_start_pending) gsvcstatus.dwcontrolsaccepted = 0; else gsvcstatus.dwcontrolsaccepted = service_accept_stop; if ((dwcurrentstate = = service_running) | | (dwcurrentstate = = service_stopped)) Gsvcstatus.dwcheckpoint = 0; else Gsvcstatus.dwcheckpoint = dwcheckpoint++; The status of the service to the SCM. SetServiceStatus (Gsvcstatushandle, &gsvcstatus);}
In the following example, the Svcctrlhandler function is a example of a Handler function. Note that the ghsvcstopevent variable are a global variable that should being initialized and used as demonstrated in Writing A ServiceMain function.
C++
Purpose:// called by SCM whenever a control code was sent to the service// using the ControlService function. parameters:// Dwctrl-control code////Return value:// none//void WINAPI svcctrlhandler (DWORD Dwctrl) {
//Handle the requested control code. Switch (Dwctrl) {case service_control_stop: reportsvcstatus (service_stop_pending, no_error, 0); Signal the service to stop. SetEvent (ghsvcstopevent); Reportsvcstatus (gsvcstatus.dwcurrentstate, no_error, 0); return; Case service_control_interrogate: Break ; Default: Break ; } }
https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms687414 (v=vs.85). aspx
http://bbs.2ccc.com/topic.asp?topicid=510075
Writing a ServiceMain function (using RegisterServiceCtrlHandler functions)