This section describes the usage of several APIs used to operate service programs such as openscmanager, createservice, openservice, controlservice, deleteservice, registerservicectrlhandler, setservicestatus, and startservicectrldispatcher. For details about function parameters, refer to.
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.
Ii. Define global function variables
// Define the global function Variable Void Init (); 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
Here we mainly set the Service handle and status.
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
Five functions are used: openscmanager, createservice, openservice, controlservice, and deleteservice. 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 two main functions are listed here. You can find other functions 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 handlerstatus. 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. dwwin32exitcode = s_ OK; s Tatus. dwcheckpoint = 0; status. dwwaithint = 0; status. dwcurrentstate = service_running; setservicestatus (hservicestatus, & status); // 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) // 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
Void winapi servicemain (){... // As shown above, it mainly indicates that this is the execution body of the program. // The simulation service runs and exits automatically after 10. Put the main task here for the application, int I = 0; while (I <10 )...}
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)else if (stricmp(lpCmdLine, "/uninstall") == 0)else{if (!::StartServiceCtrlDispatcher(st)){LogEvent(_T("Register Service Main Function Error!"));}}return 0;}
VIII. Summary
Where is the execution body of the program stored?
Register the main function and registration control function 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.