NT refers to the core of the computer operating system, including NT4.0/Server2000/XP/server2003/Vista/2008. It is a Microsoft NT series NT Server. The network is a computer network, and the server is an NT host.
An NT Service consists of three parts:
1: Service Control Manager (SCM) each win NT/2khas A scmname, which exists in service.exe.
2: the service itself has special code necessary to receive signals and commands from SCM, and can return its status to SCM after processing.
3: Service Control DISPATCHER (SCP), which has a user section, allows the user to start, pause, continue, and control Win32 installed on the computer as a Service Running
Next, let's take a look at the framework of implementing the NT Service and compile a complete NT Service.
# Include <stdio. h> # include <windows. h> service_status m_servicestatus; optional m_servicestatushandle; bool brunning = true; void winapi servicemain (DWORD argc, lptstr * argv); // Service Main Function void winapi servicectrlhandler (DWORD opcode ); // Service Control Function void winapi restart start (void); // The program function bool installservice () to be started; // install the service function bool deleteservice (); // Delete the int main (INT argc, char * argv []) {printf ("\ twindows based se Rvice demo \ n "); printf (" \ tgxisone@hotmail.com \ n "); If (argc! = 3) {printf ("Usage: % s-install [remove]", argv [0]); Return 0;} If (strcmp (argv [1], "-install") = 0) // install {If (installservice () printf ("\ n \ nservice installed sucessfully \ n "); elseprintf ("\ n \ nerror installing Service \ n");} else if (strcmp (argv [1], "-Remove") = 0) // remove {If (deleteservice () printf ("\ n \ nservice remove sucessfully \ n"); elseprintf ("\ n \ nerror removing service \ n ");} else {printf ("\ nusage: % s-ins Tall [remove] \ n ", argv [0]); Return 0;} // initialize servicemain in the entry function, // initialize a service_table_entry structure array, // This structure records the names of all services contained in this service program // and the entry point function service_table_entry dispatchtable [] ={{ "windowsmgr", servicemain }, {null, null }}; // The Last null indicates the end of the array startservicectrldispatcher (dispatchtable); Return 0;} void winapi servicemain (DWORD argc, lptstr * argv) {m_servicestatus.dwservicetype = service_win32; m_servicestatus.dw Currentstate = service_start_pending; running = service_accept_stop; running = 0; running = 0; m_servicestatus.dwcheckpoint = 0; running = 0; m_servicestatushandle = running ("windowsmgr", servicectrlhandler ); if (m_servicestatushandle = (service_status_handle) 0) return; m_services Tatus. dwcurrentstate = service_running; // set the service status m_servicestatus.dwcheckpoint = 0; m_servicestatus.dwwaithint = 0; // The service_status structure contains seven members, which reflect the current status of the service. // All these Members must correctly set setservicestatus (m_servicestatushandle, & m_servicestatus); brunning = true; // * restart start () before this structure is passed to setservicestatus (); // start our service program // * return;} void winapi servicectrlhandler (DWORD opcode) // Service Control Function {Switch (opcode) {Case service_control_pause: // We accept the command to pause itm_servicestatus.dwcurrentstate = service_paused; break; Case service_control_continue: // we got the command Condition = service_running; break; Case service_control_stop: // we got the command to stop this condition = 0; m_servicestatus.dwcurrentstate = service_stopped; condition = 0; condition = 0; setservicestatus (m_servicestatushandle, & m_servicestatus); brunning = false; break; Case service_control_interrogate: // Break;} return;} bool installservice () // install the service function {char strdir [1024]; SC _handle schscmanager, schservice; getcurrentdirectory (1024, strdir); getmodulefilename (null, strdir, sizeof (strdir); char chsyspath [1024]; getsystemdirectory (chsyspath, sizeof (chsyspath); strcat (chsyspath, "\ windowsmgr.exe"); If (! Copyfile (strdir, chsyspath, false) printf ("copy file OK \ n"); // copy our service program to the system root directory strcpy (strdir, chsyspath ); schscmanager = openscmanager (null, null, SC _manager_all_access); If (schscmanager = NULL) {printf ("Open scmanger failed, maybe you do not have the privilage to do this \ n "); Return false;} lpctstr lpszbinarypathname = strdir; schservice = createservice (schscmanager," windowsmgr "," Windows manger control ",/ /Add service information to the SCM database service_all_access, // desired access service_win32_own_process, // service type service_auto_start, // start type service_error_normal, // error control type lpszbinarypathname, // Service's binary null, // No load ordering group null, // No tag identifier null, // No dependencies null, // LocalSystem account null ); // no password if (schservice = NULL) {printf ("Faint, we failed Just because we invoke createservices failed \ n "); Return false;} closeservicehandle (schservice); Return true;} bool deleteservice () {SC _handle schscmanager; SC _handle hservice; schscmanager = openscmanager (null, null, SC _manager_all_access); char chsyspath [1024]; getsystemdirectory (chsyspath, sizeof (chsyspath); strcat (chsyspath, "\ windowsmgr.exe "); if (schscmanager = NULL) {printf ("Faint, open scmanger Fai Led \ n "); Return false;} hservice = openservice (schscmanager," windowsmgr ", service_all_access); If (hservice = NULL) {printf (" faint, open Services failt \ n "); Return false;} If (deletefile (chsyspath) = 0) {printf (" Dell file failure! \ N "); Return false;} else printf (" delete file OK! \ N "); If (deleteservice (hservice) = 0) return false; If (closeservicehandle (hservice) = 0) return false; elsereturn true ;} void winapi restart start (void) {// -------------------------------- // Add the code of the program to be made into service startup here // your code can be started as an NT Service //--------------------------------}