This article mainly introduces the usage of several APIs in the openscmanager, createservice, openservice, controlservice, deleteservice, registerservicectrlhandler, setservicestatus, startservicectrldispatcher, and so on. For specific function parameters, refer to msdn.
Why should I use a service program? A service program can be automatically started and executed, just like some services in the system. Because the service program is different in layers from the general application program, it can run automatically when the system is started, but it does not have to run after login as common applications. These are some of the benefits of the service, if you want your program to have such a function, you can create a service application. Next, let me teach you how to create a service application step by step.
1. Create a Win32 application (you can also create other applications, but the service generally does not have a user interface) and name it servicetest.
2. Define global function variables. Here we mainly set the Service handle and status.
BOOL IsInstalled();BOOL Install();BOOL Uninstall();void LogEvent(LPCTSTR pszFormat, ...);void WINAPI ServiceMain();void WINAPI ServiceStrl(DWORD dwOpcode);TCHAR szServiceName[] = _T("ServiceTest");BOOL bInstall;SERVICE_STATUS_HANDLE hServiceStatus;SERVICE_STATUS status;DWORD dwThreadID;
3. Add the init initialization function.
void Init(){hServiceStatus = NULL;status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;status.dwCurrentState = SERVICE_STOPPED;tatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;status.dwWin32ExitCode = 0;status.dwServiceSpecificExitCode = 0;status.dwCheckPoint = 0;status.dwWaitHint = 0;}
4. Add and install and delete service functions. Here we mainly use the four functions openscmanager and createservice. Openscmanager is used to open the Service Control Manager. createservice is used to create a service. openservice is used to open an existing service and return the handle of the service. controlservice is used to control the status of the opened service, delete the service after it is stopped. deleteservice is used to delete the specified service.
Bool install (); {// the two main functions are listed here. You can find other functions in the code. // Open the Service Control Manager openscmanager (null, null, callback); // create the service SC _handle hservice =: createservice (hscm, szservicename, szservicename, service_all_access, callback, service_deman_start, service_error_normal, szfilepath, null, null, _ T (""), null, null);: closeservicehandle (hservice);: closeservicehandle (hscm );}
Bool uninstall (); {// The main two functions are listed here. Other functions can be found in the Code. // Open the Service Control Manager openscmanager (null, null, SC _manager_all_access); // open the service openservice (hscm, szservicename, service_stop | delete); // stop the service controlservice (hservice, service_control_stop, & status); // Delete the eservice (hservice );...}
5. Add main service thread functions and control functions. Call registerservicectrlhandler to register the service control function. Set status. dwcontrolsaccepted to service_accept_stop. Otherwise, you cannot control the service status.
Void winapi servicemain () {// register the control request handler status. dwcurrentstate = service_start_pending; status. dwcontrolsaccepted = service_accept_stop; // you need to use this command; otherwise, you cannot control it. // Register Service Control hservicestatus = registerservicectrlhandler (szservicename, servicestrl); If (hservicestatus = NULL) {logevent (_ T ("handler not installed"); return;} setservicestatus (hservicestatus, & status); status. dwwin32exitc Ode = s_ OK; status. dwcheckpoint = 0; status. dwwaithint = 0; status. dwcurrentstate = service_running; setservicestatus (hservicestatus, & status); // simulates the running of the service and automatically exits after 10. When the application is used, the main task can be placed here int I = 0; while (I <10) {sleep (1000); I ++;} // status. dwcurrentstate = service_stopped; setservicestatus (hservicestatus, & status); logevent (_ T ("Service stopped "));}
6. register the control function and program execution subject in the main thread function. This is the execution body of the program.
Void winapi servicemain (){... // Simulate the running of the service and exit automatically after 10. Put the main task here for the application. Int I = 0; while (I <10) {sleep (1000); I ++ ;}...}
7. Register, install, delete, and register the main function in the main function.
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){Init();dwThreadID = ::GetCurrentThreadId(); SERVICE_TABLE_ENTRY st[] = { { szServiceName, (LPSERVICE_MAIN_FUNCTION)ServiceMain }, { NULL, NULL } };if (stricmp(lpCmdLine, "/install") == 0){Install();}else if (stricmp(lpCmdLine, "/uninstall") == 0){Uninstall();}else{if (!::StartServiceCtrlDispatcher(st)){LogEvent(_T("Register Service Main Function Error!"));}}return 0;}
8. Summary. In fact, it is not difficult to build a service program. It is mainly to know where the execution body of the program is stored? And register the main and registration control functions of the program. If these two functions are not registered, you will not know how to control the program. Status. dwcontrolsaccepted = service_accept_stop; this is also important. If you have not set it, the service will not be controlled by you.