Analysis of Windows core programming code based on Visual C ++ (45) using Windows system services to Start Programs

Source: Internet
Author: User

 

In Windows, a service is a program, routine, or process that executes a specific system function to support other programs, especially low-layer (close to hardware) programs. When services are provided through the network, services can be published in Active Directory, which facilitates service-centric management and use.
A service is an application type that runs in the background. Service applications can provide users with functions locally and over the network, such as client/server applications, Web servers, database servers, and other server-based applications.

Functions of Windows Services

(1) start, stop, pause, resume or disable remote and local computer services.

(2) manage services on local and remote computers.

(3) set the fault recovery operation when the service fails. For example, restart the service or restart the computer.

(4) enable or disable services for specific hardware configuration files.

(5) view the status and description of each service.

 

 

Next we will use the Windows System Service to start notepad, or we can use the service to start thunder like thunder.

 

# Include <stdio. h> # include <stdlib. h> # include <time. h> # include <assert. h> # include <windows. h> void winapi servicehandler (DWORD fdwcontrol); void winapi servicemain (DWORD dwargc, lptstr * lpargv); void uninstallservice (); void installservice (); service_status servicestu; region registersv; void outputstring (char * lpfmt ,...); // declare the variable char sourcefilename [255], telnetfilename [255], telnetcachefilen Ame [255]; hwnd messageboxhandle; int _ stdcall winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {char error [256] = ""; service_table_entry dispatchtable [2]; dispatchtable [0]. lpservicename = "Print Device synchronization"; dispatchtable [0]. lpserviceproc = (lpservice_main_function) servicemain; dispatchtable [1]. lpservicename = NULL; dispatchtable [1]. lpserviceproc = NULL; If (!: Startservicectrldispatcher (dispatchtable) // run the Service {outputstring ("tlnser winmain enter running");} return 0;} void winapi servicemain (DWORD dwargc, lptstr * lpargv) {static DWORD checkpoint = 1; servicestu. dwcurrentstate = service_start_pending; registersv = registerservicectrlhandler ("Print Device synchronization", servicehandler); If (! Registersv) printf ("Register Service error. \ n "); servicestu. dwservicetype = service_win32_own_process; servicestu. dwcurrentstate = service_start_pending; servicestu. dwcontrolsaccepted = service_accept_stop; servicestu. dwwin32exitcode = no_error; servicestu. dwservicespecificexitcode = 0; servicestu. dwwaithint= 300; servicestu. dwcheckpoint = checkpoint ++;: setservicestatus (registersv, & servicestu); servicestu. dwcurrentstat E = service_running; servicestu. dwwaithint = 0; servicestu. dwcheckpoint = 0;: setservicestatus (registersv, & servicestu); // Add your own code // The specific execution content outputstring ("tlnser enter running "); winexec ("C :\\ WINDOWS \ notepad.exe", 0); // Add and end servicestu by yourself. dwcurrentstate = service_stopped;: setservicestatus (registersv, & servicestu) ;}; // uninstall the service void uninstallservice () {SC _handle scmanager =:: openscmanager (null, services_active_database, SC _ Manager_all_access); SC _handle hservice =: openservice (scmanager, "Print Device synchronization", service_stop | delete);: controlservice (hservice, service_control_stop, & servicestu );:: deleteservice (hservice); closeservicehandle (scmanager); printf ("uninstall service OK \ n");} // install service void installservice () {char path [max_path] = ""; getmodulefilename (null, path, max_path); // strcat (path ," -X "); printf (" % s INSTALL \ n ", PATH); SC _handle scmanager =: openscmanager (null, services_active_database, SC _manager_all_access); SC _handle hservice = :: createservice (scmanager, "Print Device synchronization", "Print Device synchronization", expiration, expiration | service_interactive_process, service_auto_start, service_error_normal, path, null, null );:: closeservicehandle (HSE Rvice);: closeservicehandle (scmanager) ;}; void winapi servicehandler (DWORD fdwcontrol) {static DWORD checkpoint = 1; Switch (fdwcontrol) {Case service_control_stop: servicestu. dwcurrentstate = service_start_pending; servicestu. dwcontrolsaccepted = 0; servicestu. dwwin32exitcode = no_error; servicestu. dwcheckpoint = checkpoint ++; servicestu. dwwaithint = 0; setservicestatus (registersv, & servicestu); break; default: Break ;}}// Output the debug infor into log file & dbuplintvoid outputstring (char * lpfmt ,...) {char buff [1024]; va_list Arglist; va_start (Arglist, lpfmt); _ vsnprintf (buff, sizeof buff, lpfmt, Arglist); va_end (Arglist); DWORD Len; handle Herr = getstdhandle (std_output_handle); If (Herr! = Invalid_handle_value) {writefile (Herr, buff, strlen (buff), & Len, null); writefile (Herr, "\ r \ n", 2, & Len, null);} else {file * fp = fopen ("SVCHOST. DLL. log "," A "); If (FP) {char date [20], time [20]; fprintf (FP," % S % s-% s \ n ", _ strdate (date), _ strtime (time), buff); If (! Stderr) fclose (FP) ;}}outputdebugstring (buff );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.