Original: C Language writing Windows service program
#include <Windows.h> #include <stdio.h> #define SLEEP_TIME 5000//Interval # LOGFILE "C:\\memstatus.txt"//Information output file Service_status servicestatus; Service status Service_status_handle hstatus; Service state handle void ServiceMain (int argc, char** argv), void Ctrlhandler (DWORD request); int initservice (); int WriteToLog (char* STR) {file* pfile;fopen_s (&pfile,logfile, "A +"); if (pfile==null) {return-1;} fprintf_s (Pfile, "%s\n", str); fclose (pfile); return 0;} Service initializationint Initservice () {OutputDebugString ("monitoring started."); int result;result = WriteToLog ("monitoring started."); return (result); }//Control handlervoid Ctrlhandler (DWORD request) {switch (request) {Case service_control_stop:outputdebugstring (" Monitoring stopped. "); WriteToLog ("Monitoring stopped."); Servicestatus.dwwin32exitcode = 0; Servicestatus.dwcurrentstate = service_stopped; SetServiceStatus (Hstatus, &servicestatus); Return;case service_control_shutdown:outputdebugstring ("MonItoring stopped. "); WriteToLog ("Monitoring stopped."); Servicestatus.dwwin32exitcode = 0; Servicestatus.dwcurrentstate = service_stopped; SetServiceStatus (Hstatus, &servicestatus); return;default:break;} Report Current Statussetservicestatus (Hstatus, &servicestatus); return;} void ServiceMain (int argc, char** argv) {int error; Servicestatus.dwservicetype = Service_win32; Servicestatus.dwcurrentstate = service_start_pending; servicestatus.dwcontrolsaccepted = Service_accept_shutdown | service_accept_stop;//in this example only accept the system shutdown and stop the service of the two control commands servicestatus.dwwin32exitcode = 0; Servicestatus.dwservicespecificexitcode = 0; Servicestatus.dwcheckpoint = 0; Servicestatus.dwwaithint = 0;hstatus =:: RegisterServiceCtrlHandler ("Memorystatus", (lphandler_function) CtrlHandler if (Hstatus = = (service_status_handle) 0) {WriteToLog ("RegisterServiceCtrlHandler failed"); return;} WriteToLog ("RegisterServiceCtrlHandler success");//Initialize Service error = Initservice (); if (error) {// Initialization FailedservicestatUs.dwcurrentstate = service_stopped; Servicestatus.dwwin32exitcode =-1; SetServiceStatus (Hstatus, &servicestatus); Return }//Report run status to SCM servicestatus.dwcurrentstate = service_running; SetServiceStatus (Hstatus, &servicestatus);//Start the task loop below, and you can add the work you want the service to do Memorystatus Memstatus;while ( Servicestatus.dwcurrentstate = = service_running) {char buffer[16]; GlobalMemoryStatus (&memstatus); int availmb = memstatus.dwavailphys/1024/1024;sprintf_s (buffer,100, "available Memory is%dMB ", AVAILMB); OutputDebugString (buffer); int result = WriteToLog (buffer); if (result) { Servicestatus.dwcurrentstate = service_stopped; Servicestatus.dwwin32exitcode =-1; SetServiceStatus (Hstatus, &servicestatus); return;} Sleep (sleep_time);} WriteToLog ("service Stopped");} void Main () {service_table_entry servicetable[2]; Servicetable[0].lpservicename= "Memorystatus"; Servicetable[0].lpserviceproc= (lpservice_main_function) ServiceMain; Servicetable[1].lpservicename=null; Servicetable[1].lpserviceproc=null; StartsErvicectrldispatcher (servicetable);}
Command-Line Operations:
Note I here in SC create memortystatus binpath= D:\MemoryStatus.exe inside there is a spelling mistake, Memorystatus wrote Memortystatus, more than a T, this doesn't matter, As long as you are operating under the will wrong on the line.
Operation Result:
Finally, stop and delete services:
Reference:
http://www.vckbase.com/index.php/wv/1193
Http://www.2cto.com/kf/201111/111990.html
C Language Writing Windows service program