Register, start, stop, and delete a service program
Last Update:2018-12-07
Source: Internet
Author: User
Char * sname = "service1 ";
//---------------------------------------------------------------------------
Void _ fastcall tform2: button1click (tobject * sender)
{
SC _handle SCM = openscmanager (null, null,
SC _manager_create_service );
If (SCM! = NULL ){
SC _handle SVC = createservice (SCM,
Sname, sname, // service name
Service_all_access,
Service_win32_own_process
| Service_interactive_process,
Service_auto_start,
// Start automatically
Service_error_ignore,
"C: \ Documents and Settings \ Administrator \ My Documents ents \ rad studio \ projects \ servicetest \ debug \ project1.exe", // service ontology Program Path,
Null, null, null );
If (SVC! = NULL)
Closeservicehandle (SVC );
Closeservicehandle (SCM );
}
}
//---------------------------------------------------------------------------
Void _ fastcall tform2: button2click (tobject * sender)
{
SC _handle SCM = openscmanager (null, null, SC _manager_connect );
If (SCM! = NULL ){
SC _handle SVC = openservice (SCM, sname,
Service_all_access );
If (SVC! = NULL ){
_ Service_status servicestatus; // = new lpservice_status ();
Queryservicestatus (SVC, & servicestatus );
If (servicestatus. dwcurrentstate =
Service_running) // stop the service before deletion.
Controlservice (SVC,
Service_control_stop, & servicestatus );
Deleteservice (SVC );
Closeservicehandle (SVC );
// Call closeservicehandle after the service is deleted
}
// To immediately remove this entry from the database.
Closeservicehandle (SCM );
}
}
//---------------------------------------------------------------------------
Void _ fastcall tform2: button3click (tobject * sender)
{
SC _handle SCM = openscmanager (null, null, SC _manager_connect );
If (SCM! = NULL ){
SC _handle SVC = openservice (SCM, sname, service_start );
If (SVC! = NULL ){
Startservice (SVC, 0, null); // start service
Closeservicehandle (SVC );
}
Closeservicehandle (SCM );
}
}
//---------------------------------------------------------------------------
Void _ fastcall tform2: button4click (tobject * sender)
{
SC _handle SCM = openscmanager (null, null,
SC _manager_all_access );
If (SCM! = NULL ){
SC _handle SVC = openservice (SCM, sname,
Service_stop | service_query_status );
If (SVC! = NULL ){
_ Service_status servicestatus;
Queryservicestatus (SVC, & servicestatus );
If (servicestatus. dwcurrentstate =
Service_running)
Controlservice (SVC,
Service_control_stop, & servicestatus );
Closeservicehandle (SVC );
}
Closeservicehandle (SCM );
}
}
//---------------------------------------------------------------------------