VC ++ builds an NT Service Framework

Source: Internet
Author: User

# Include <stdio. h>
# Include <windows. h>
Service_status m_servicestatus;
Service_status_handle m_servicestatushandle;
Bool brunning = true;
Void winapi servicemain (DWORD argc, lptstr * argv); // main service function
Void winapi servicectrlhandler (DWORD opcode); // Service Control Function
Void winapi restart start (void); //ProgramFunction
Bool installservice (); // The function for installing the service
Bool deleteservice (); // delete a service function

Int main (INT argc, char * argv [])
{
Printf ("/twindows Based Service 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 ");
Else
Printf ("/n/nerror installing Service/N ");
}
Else if (strcmp (argv [1], "-Remove") = 0) // remove
{
If (deleteservice ())
Printf ("/n/nservice remove sucessfully/N ");
Else
Printf ("/n/nerror removing service/N ");
}
Else
{
Printf ("/nusage: % s-install [remove]/n", argv [0]);
Return 0;
}
// Initialize servicemain in the entry point function,
// Initialize a service_table_entry structure array,
// This structure records the names of all services contained in the Service Program
// And the entry point function of the service
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.dwcurrentstate = service_start_pending;
M_servicestatus.dwcontrolsaccepted = service_accept_stop;
M_servicestatus.dwwin32exitcode = 0;
M_servicestatus.dwservicespecificexitcode = 0;
M_servicestatus.dwcheckpoint = 0;
M_servicestatus.dwwaithint = 0;
M_servicestatushandle = registerservicectrlhandler ("windowsmgr", servicectrlhandler );
If (m_servicestatushandle = (service_status_handle) 0) return;
M_servicestatus.dwcurrentstate = service_running; // sets 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 be correctly set before the structure is passed to setservicestatus
Setservicestatus (m_servicestatushandle, & m_servicestatus );
Brunning = true;
//*
Restart start (); // 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 it
M_servicestatus.dwcurrentstate = service_paused;
Break;
Case service_control_continue: // we got the command to continue
M_servicestatus.dwcurrentstate = service_running;
Break;
Case service_control_stop: // we got the command to stop this service
M_servicestatus.dwwin32exitcode = 0;
M_servicestatus.dwcurrentstate = service_stopped;
M_servicestatus.dwcheckpoint = 0;
M_servicestatus.dwwaithint = 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 programs 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)
{< br> printf ("Faint, open scmanger failed/N");
return false;
}< br> hservice = openservice (schscmanager, "windowsmgr", service_all_access);
If (hservice = NULL)
{< br> printf ("Faint, open services failt/N");
return false;
}< br> If (deletefile (chsyspath) = 0)
{< br> printf ("Dell file failure! /N ");
return false;
}< br> else printf (" delete file OK! /N ");
If (deleteservice (hservice) = 0)
return false;
If (closeservicehandle (hservice) = 0)
return false;
else
return true;
}

void winapi restart start (void)
{< br> // ------------------------------
// Add the Program Code to start the service.
// your code can be started as an NT Service
// ----------------------------------
}

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.