欢迎访问:常州市武进区嘉泽中心小学网站 !今天是:
栏目列表
您现在的位置是:首页>>教师>>计算机技术>>程序设计>>杂项>>文章内容
删除Windows服务
发布时间:2008-11-20   点击:   来源:本站原创   录入者:佚名
 

     对于编程高手来说,这个题目有点无聊,不过在某些情况下还是相当有用的。像我删除卸载了一个软件,结果什么都删除了,结果服务还在那儿,很不雅观,找遍注册表都找不到,只好自己动手写了个小程序来删除服务。

   首先做好界面,如下:

   

    这要用到Windows的服务管理函数组,首先要打开服务管理器,这要在头文件中包含winsvc.h    把SC_HANDLE  sh;定义加入对话框类中。    在OnInitDialog成员函数中加入如下代码,以初始化服务管理。 sh=OpenSCManager(0,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
 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);
 }
} 前者枚举不活动的服务,并把它加入列表框,后者直接删除对应服务
附件:
    关闭窗口
    打印文档
    账号登录
    保持登录 忘记密码?
    账号与武进教师培训平台同步