|
发表于 2020-1-4 11:24:01
|
显示全部楼层
嘿嘿 刚好写相关程序
因为不方便完全公开
给你看两段关键代码 感觉有用的话给我写信
xo0888@tom.com
//添加服务
BOOL AddServices()
{
char name[100];
char info[200];
char path[300];
printf("请输入服务名\n\n");
scanf ("%s",&name);
printf("请输入服务描述\n\n");
scanf ("%s",&info);
printf("请输入程序路径\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("服务创建成功\n\n");
else
printf("服务创建失败\n\n");
CloseServiceHandle(service);
CloseServiceHandle(manager);
return 0;
}
//删除服务
BOOL DelServices()
{
char name[100];
SC_HANDLE scm;
SC_HANDLE service;
SERVICE_STATUS status;
printf("请输入要删除的服务名\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("删除服务失败!");
else
printf("删除服务成功!");
CloseServiceHandle(service );
CloseServiceHandle(scm);
return 0;
}
|
|