//停止和删除已安装的服务程序
void removeService()
{
SC_HANDLE schService;
SC_HANDLE schSCManager;
//打开服务管理数据库
schSCManager=OpenSCManager(
NULL, //本地计算机
NULL, //默认的数据库
SC_MANAGER_ALL_ACCESS //要求所有的访问权
);
if(schSCManager)
{
//获取服务程序句柄
schService=OpenService(
schSCManager, //服务管理数据库句柄
TEXT(SZSERVICENAME), //服务名
SERVICE_ALL_ACCESS //响应所有的访问请求
);
if(schService)
{
//试图停止服务
if(ControlService(
schService, //服务程序句柄
SERVICE_CONTROL_STOP, //停止服务请求码
&ssStatus //接收最后的服务状态信息
))
{
_tprintf(TEXT("Stopping %s."),TEXT(SZAPPNAME));
Sleep(1000);
//等待服务停止
//
while(QueryServiceStatus(schService,&ssStatus))
{
if(SERVICE_STOP_PENDING==ssStatus.dwCurrentState)
{
_tprintf(TEXT("."));
Sleep(1000);
}
else
break;
}
if(SERVICE_STOPPED==ssStatus.dwCurrentState)
_tprintf(TEXT("\n %s stopped. \n"),TEXT(SZAPPNAME));
else
_tprintf(TEXT("\n %s failed to stopp. \n"),TEXT(SZAPPNAME));
}
//删除已安装的服务程序安装
if(DeleteService(schService))
_tprintf(TEXT("%s removed. \n"),TEXT(SZAPPNAME));
else
_tprintf(TEXT("DeleteService failed - %s. \n"), GetLastError());
CloseServiceHandle(schService);
}
else
_tprintf(TEXT("OpenService failed - %s \n"),GetLastError());
CloseServiceHandle(schSCManager);
}
else
_tprintf(TEXT("OpenSCManager failed - %s \n"),GetLastError());
}
|