|
Hey, I just wrote a related program
Because it is inconvenient to be completely public
Let me show you two key pieces of code
xo0888@tom.com
// Add service
BOOL AddServices ()
{
char name [100];
char info [200];
char path [300];
printf ("Please enter service name\n\n");
scanf ("% s",&name);
printf ("Please enter a service description\n\n");
scanf ("% s",&info);
printf ("Please enter the program path\n\n");
scanf ("% s",&path);
SC_HANDLE manager = NULL;
SC_HANDLE service = NULL;
if ((manager = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf ("OpenSCManager Error");
}
service = CreateService (
manager, name, info,
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
path, 0, 0, 0, 0, 0);
if (service)
printf ("Service created successfully\n\n");
else
printf ("Service creation failed\n\n");
CloseServiceHandle (service);
CloseServiceHandle (manager);
return 0;
}
// Delete service
BOOL DelServices ()
{
char name [100];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf ("Please enter the service name to be deleted\n\n");
scanf ("% s",&name);
if ((scm = OpenSCManager (NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL)
{
printf ("OpenSCManager Error\n");
}
service = OpenService (scm, name, SERVICE_ALL_ACCESS | DELETE);
if (! service)
printf ("OpenService error!\n");
BOOL isSuccess = QueryServiceStatus (service,&status);
if (! isSuccess)
printf ("QueryServiceStatus error!\n");
if (status.dwCurrentState! = SERVICE_STOPPED)
{
isSuccess = ControlService (service, SERVICE_CONTROL_STOP,&status);
if (! isSuccess)
printf ("Stop Service error!\n");
Sleep (500);
}
isSuccess = DeleteService (service);
if (! isSuccess)
printf ("Failed to delete service!");
else
printf ("Successfully deleted service!");
CloseServiceHandle (service);
CloseServiceHandle (scm);
return 0;
} |
|