// Service. cpp: defines the entry point for the console application.
//
// Main function of the service program.
# Include "stdafx. H"
# Include "windows. H"
# Define szappname "serversample" // service program name
# Define szservicename "servicesample" // identifies the internal name of the service
// Internal variable
Bool bdebugserver = false;
Service_status ssstatus;
Service_status_handle sshstatushandle;
DWORD dwerr = 0;
Tchar szerr [256];
// The following functions are implemented by the program
Void winapi service_main (DWORD dwargc, lptstr * lpszargv );
Void winapi service_ctrl (DWORD dwctrlcode );
Void servicestart (DWORD dwargc, lptstr * lpszargv); // initialization entry function of a specific service
Void installservice ();
Void removeservice ();
Void debugservice (INT argc, char ** argv );
Bool reportstatustoscmgr (DWORD dwcurrentstate, DWORD dwwin32exitcode, DWORD dwwaithint );
Void addtomessagelog (lptstr lpszmsg );
// Int _ tmain (INT argc, _ tchar * argv [])
Int _ tmain (INT argc, tchar * argv [])
{
Service_table_entry dispatchtable [] =
{
{Text (szservicename), (lpservice_main_function) service_main },
{Null, null}
};
If (argc> 1) & (* argv [1] = '-') | (argv [1] = text ("/"))))
{
If (_ tcsicmp (text ("Install"), argv [1] + 1) = 0)
{
Installservice ();
}
Else if (_ tcsicmp (text ("Remove"), argv [1] + 1) = 0)
{
Removeservice ();
}
Else if (_ tcsicmp (text ("debug"), argv [1] + 1) = 0)
{
Bdebugserver = true;
// Debugservice (argc, argv );
}
Else
{// If it does not match the preceding parameter, it may be the Service Control Manager program to start the program. Call now
// Startservicectrldispatcher function.
Printf ("% s-install to install the service \ n", szappname );
Printf ("% s-remove to remove the service \ n", szappname );
Printf ("% s-Debug to debug the service \ n", szappname );
Printf ("\ n startservicectrldispatcher being called. \ n ");
Printf ("This may take several seconds. Please wait. \ n ");
If (! Startservicectrldispatcher (dispatchtable ))
Addtomessagelog (text ("startservicectrldispatcher failed ."));
Else
Addtomessagelog (text ("startservicectrldispatcher OK ."));
}
Exit (0 );
}
Return 0;
}
// Service entry point function
Void servicestart (DWORD dwargc, lptstr * lpszargv) // initialization entry function of a specific service
{
Printf ("\ n startservicectrldispatcher being called. \ n ");
}
Void winapi service_main (DWORD dwargc, lptstr * lpszargv)
{
// Register the Service Control Handler
Sshstatushandle = registerservicectrlhandler (text (szservicename), service_ctrl );
// If the registration fails
If (! Sshstatushandle)
{
Goto cleanup;
Return;
}
// Initialize the members in the service_status Structure
Ssstatus. dwservicetype = service_win32_own_process;
Ssstatus. dwservicespecificexitcode = 0;
// Update the service status
If (! Reportstatustoscmgr (
Service_start_pending, // service status, the service is starting.
No_error, // exit code
3000) // wait time
Goto cleanup; // switch to cleanup if the service status fails to be updated
Servicestart (dwargc, lpszargv );
Return;
Cleanup:
// Update the service status to service_stopped and exit.
If (sshstatushandle)
(Void) reportstatustoscmgr (service_stopped, dwerr, 0 );
}
// Control Handler Functions
Void winapi service_ctrl (DWORD dwctrlcode)
{
// Process the control request code
Switch (dwctrlcode)
{
// Update the service status to servicde_stop_pending before stopping the service.
Case service_control_stop:
Reportstatustoscmgr (service_stop_pending, no_error, 500 );
// Servicestop (); // implemented by a specific service program
Return;
// Suspend the service
Case service_control_pause:
Reportstatustoscmgr (service_stop_pending, no_error, 500 );
// Servicepause (); // implemented by a specific service program
Ssstatus. dwcurrentstate = service_paused;
Return;
// Continue service
Case service_control_continue:
Reportstatustoscmgr (service_stop_pending, no_error, 500 );
// Servicecontinue (); // implemented by a specific service program
Ssstatus. dwcurrentstate = service_running;
Return;
// Update the service status
Case service_control_interrogate:
Break;
// Invalid control code
Default:
Break;
}
Reportstatustoscmgr (ssstatus. dwcurrentstate, no_error, 0 );
}
// Install the Service Program
Void installservice ()
{
SC _handle schservice;
SC _handle schscmanager;
Tchar szpath [512];
// Obtain the path of the program Disk File
If (getmodulefilename (null, szpath, 512) = 0)
{
_ Tprintf (text ("unable to install % s-% s \ n "),
Text (szappname ),
Getlasterror (); // @ 1 get the last error code returned by the call Function
Return;
}
// Open the service management database
Schscmanager = openscmanager (
Null, // Local Computer
Null, // default database
SC _manager_all_access // All access permissions are required.
);
If (schscmanager)
{
// Register the Service Program
Schservice = createservice (
Schscmanager, // service management database handle
Text (szservicename), // service name
Text (szappname), // used to display the service ID
Service_all_access, // responds to all access requests
Service_win32_own_process, // service type
// Service_demand_start, // start Type
Service_auto_start,
Service_error_normal, // Error Control Type
Szpath, // path of the disk file of the Service Program
Null, // The service does not belong to any group
Null, // No tag identifier
Null, // start the service or service group on which the service depends. Here it is just an empty string
Null, // LocalSystem account
Null );
If (schservice)
{
_ Tprintf (text ("% s installed. \ n"), text (szappname ));
Closeservicehandle (schservice );
}
Else
{
_ Tprintf (text ("createservice failed-% s \ n"), getlasterror ());
}
Closeservicehandle (schscmanager );
}
Else
_ Tprintf (text ("openscmanager failed-% s \ n"), getlasterror ());
}
Void removeservice ()
{
SC _handle service, SCM; // defines the Service handle and service control management database handle
Service_status status; // defines the service status structure.
// If (argc! = 2) return; // if the number of command line parameters is smaller than 2, the uninstallation program is not executed.
// Open the service control management database and return the handle of the service control management database.
SCM = openscmanager (0, 0, SC _manager_create_service );
// Obtain the Service handle and add the delete option in Service Access
// Service = openservice (SCM, argv [1], service_all_access | delete );
Service = openservice (SCM, text (szservicename), service_all_access | delete );
// Obtain the current status of the service
Queryservicestatus (Service, & status );
// If the service is not in the stopped status, set it to the stopped status.
If (status. dwcurrentstate! = Service_stopped)
Controlservice (Service, service_control_stop, & status );
Deleteservice (service); // delete a service
Closeservicehandle (service); // close the new Service handle
Closeservicehandle (SCM); // closes the service control management database handle
}
Void debugservice (INT argc, char ** argv)
{
}
Bool reportstatustoscmgr (DWORD dwcurrentstate, DWORD dwwin32exitcode, DWORD dwwaithint)
{
Return true;
}
Void addtomessagelog (lptstr lpszmsg)
{
}
Http://blog.chinaunix.net/u1/37538/showart_493320.html
Http://www.wei2008.com/Code/softdown.asp? Softid = 35719