Win32 programming-program framework for implementing the Windows NT Service using Windows APIs

Source: Internet
Author: User

// ClickHereDownload source code

# Include <windows. h>
# Include <stdio. h>

// Variable Declaration
Char servicename [] = "ntservice ";
Service_status ssstatus;
Service_status_handle servicestatushandle;

// Forward Declaration
Void showHelp ();
Void insallservice ();
Void removeservice ();
Void winapi servicemain (DWORD dwargc, lptstr * lpszargv );
Void winapi controlhandler (DWORD dwctrlcode );

// main Program entry
int main (INT argc, char ** argv)
{< br> // If (argc = 1)
{< br> service_table_entry servicetableentry [] ={{ servicename, servicemain }, {null, null }};
If (! Startservicectrldispatcher (servicetableentry)
{< br> printf ("startservicectrldispatcher failed! \ N ");
return 1;
}< br> return 0;
}

// The parameter is incorrect.
If (argc! = 2)
{< br> showHelp ();
return 0;
}< br>
// The parameter can be identified
// display help
If (! Strcmp ("/? ", Argv [1])
{< br> showHelp ();
return 0;
}< br> // install the service
If (! Strcmp ("/I", argv [1])
{< br> insallservice ();
return 0;
}< br> // remove a service
If (! Strcmp ("/R", argv [1])
{< br> removeservice ();
return 0;
}< br>
// The parameter cannot be identified
showHelp ();
return 0;
}

// Display help
Void showHelp ()
{
// Display the parameter prompt
Printf ("parameters: \ n ");
Printf ("/? Show this help \ n ");
Printf ("/I install the service \ n ");
Printf ("/R remove the service \ n ");
}

// Install the service
Void insallservice ()
{
SC _handle schservice, schscmanager;
Char filename [256], displayname [] = "NT Service ";
// Get the file name
If (getmodulefilename (0, filename, 256) = 0) return;
// Open the Service Manager
Schscmanager = openscmanager (null, null, SC _manager_all_access );
If (schscmanager = 0)
{
Printf ("Open scmanager failed! \ N ");
Return;
}
// Create a service
Schservice = createservice (schscmanager, servicename, displayname, service_all_access, service_win32_own_process, service_demand_start,
Service_error_normal, filename, null, null );
If (schservice> 0)
{
Printf ("service % s installed! \ N ", servicename );
// Close the service
Closeservicehandle (schservice );
}
Else
{
Printf ("service % s install failed! \ N ", servicename );
}
// Disable the service controller
Closeservicehandle (schscmanager );
}

// Remove the service
Void removeservice ()
{
SC _handle schservice, schscmanager;
// Open the Service Manager
Schscmanager = openscmanager (null, null, SC _manager_all_access );
If (schscmanager = 0)
{
Printf ("Open scmanager failed! \ N ");
Return;
}
// Open the service
Schservice = openservice (schscmanager, servicename, service_all_access );
If (schservice = 0)
{
Printf ("cann' t open service! \ N ");
// Close the service
Closeservicehandle (schscmanager );
Return;
}
Else
{
// Stop the service
If (controlservice (schservice, service_control_stop, & ssstatus ))
{
// Query the service status
While (queryservicestatus (schservice, & ssstatus ))
{
If (ssstatus. dwcurrentstate = service_stop_pending) printf ("% s ",".");
Else break;
}
If (ssstatus. dwcurrentstate = service_stopped) printf ("service stoped now! \ N ");
Else
{
Printf ("service stop failed! \ N ");
Return;
}
}
// Delete the service
If (deleteservice (schservice ))
{
Printf ("Service removed! \ N ");
}
Else
{
Printf ("service remove failed! \ N ");
}
// Close the service
Closeservicehandle (schservice );
}
// Disable the Controller
Closeservicehandle (schscmanager );
}

// Service program entry
Void winapi servicemain (DWORD dwargc, lptstr * lpszargv)
{
// Set the initial status
Ssstatus. dwservicetype = service_win32;
Ssstatus. dwcontrolsaccepted = service_accept_stop | service_accept_pause_continue;
Ssstatus. dwcurrentstate = service_start_pending;

// Register the service
Servicestatushandle = registerservicectrlhandler (servicename, controlhandler );
If (servicestatushandle = 0) return;

// Set the service status
If (! Setservicestatus (servicestatushandle, & ssstatus ))
{
// Stop the service
Ssstatus. dwcurrentstate = service_stopped;
Setservicestatus (servicestatushandle, & ssstatus );
}

// Service Running
ssstatus. dwcurrentstate = service_running;
setservicestatus (servicestatushandle, & ssstatus);
}

// Service controller
Void winapi controlhandler (DWORD dwctrlcode)
{
// Identify the control code
Switch (dwctrlcode)
{
// Pause
Case service_control_pause:
Ssstatus. dwcurrentstate = service_paused;
Break;
// Continue
Case service_control_continue:
Ssstatus. dwcurrentstate = service_running;
Break;
// Stop
Case service_control_stop:
Ssstatus. dwcurrentstate = service_stopped;
Setservicestatus (servicestatushandle, & ssstatus );
Break;
}

// Set the service status
Setservicestatus (servicestatushandle, & ssstatus );

Return;
}

Note: For detailed descriptions of each function, see msdn.

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.