BOOL StopService()
{
SC_HANDLE scm;
SC_HANDLE service;
if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
{
CloseServiceHandle(scm);
return 0;
}
service=OpenService(scm, "ShellHWDetection",SERVICE_ALL_ACCESS);
if(!service)
{
CloseServiceHandle(service);
CloseServiceHandle(scm);
return 0;
}
SERVICE_STATUS status;
if(QueryServiceStatus(service,&status)){
if(status.dwCurrentState!=SERVICE_STOPPED) { //如果服务当前状态为启用
ControlService(service,SERVICE_CONTROL_STOP,&status);
SC_LOCK sclLockA;
sclLockA = LockServiceDatabase(scm);
if (sclLockA != NULL){
if (! ChangeServiceConfig(
service, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_DISABLED, // 这里做了更改
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) //displayname
{
}
}
UnlockServiceDatabase(sclLockA);
}
else
{
SC_LOCK sclLockB;
sclLockB = LockServiceDatabase(scm);
if (sclLockB != NULL){
if (! ChangeServiceConfig(
service, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_DISABLED, // 这里做了更改
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) //displayname
{
}
}
UnlockServiceDatabase(sclLockB);
}
CloseServiceHandle(service);
CloseServiceHandle(scm);
}
return TRUE;
}
C++ 停止并禁用服务
最新推荐文章于 2020-08-04 13:34:24 发布