对于编程高手来说,这个题目有点无聊,不过在某些情况下还是相当有用的。像我删除卸载了一个软件,结果什么都删除了,结果服务还在那儿,很不雅观,找遍注册表都找不到,只好自己动手写了个小程序来删除服务。
首先做好界面,如下:
if(!sh)
{
MessageBox("faile to Open SCManager");
return FALSE;
} 在OnClose成员函数中加入 CloseServiceHandle(sh); 然后处理两个按钮事件 void CdelsDlg::ListServices()
{
// TODO: Add your control notification handler code here
DWORD eh=0,d1,nret,i;
BOOL ret;
SCV.ResetContent();
ret=EnumServicesStatus(sh,SERVICE_WIN32|SERVICE_DRIVER,
SERVICE_INACTIVE,ess,sizeof(ess),&d1,&nret,&eh);
if(!ret)
{
char temp[32];
sprintf(temp,"%d",GetLastError());
MessageBox(temp);
MessageBox("failed to enum services");
return;
}
for(i=0;i<nret;i++)
{
SCV.AddString(ess[i].lpDisplayName);
}
} void CdelsDlg::DelService()
{
// TODO: Add your control notification handler code here
int i,ret;
char temp[256];
i=SCV.GetCurSel();
sprintf(temp,"Do you realy want to remove the service\r\n"
"%d:\t%s\r\n%s",i,ess[i].lpServiceName,ess[i].lpDisplayName);
ret=MessageBox(temp,"Alert!",MB_YESNO|MB_ICONWARNING|MB_DEFBUTTON2);
if(ret==IDYES)
{
SC_HANDLE ds;
ds=OpenService(sh,ess[i].lpServiceName,SC_MANAGER_ALL_ACCESS);
if(!ds)
{
MessageBox("Failed to Open Service");
return;
}
if(DeleteService(ds))
{
MessageBox("Success to remove the service");
}
else
{
sprintf(temp,"Failed to remove the service\r\nERROR CODE:\t%d",
GetLastError());
MessageBox(temp);
}
CloseServiceHandle(ds);
}
} 前者枚举不活动的服务,并把它加入列表框,后者直接删除对应服务