枚举NT服务

 SC_HANDLE hSCManager = NULL;
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
 int nRet = 0;
    if(hSCManager == NULL)
    {
        return ;
    }
 DWORD dwServicesReturned = 0;
    DWORD cbBytesNeeded = 0;
    DWORD cbBufSize = 0;
 nRet = EnumServicesStatus(hSCManager,SERVICE_WIN32,SERVICE_STATE_ALL,NULL,0,&cbBytesNeeded,&dwServicesReturned,0);
 ENUM_SERVICE_STATUS *lpServices = new ENUM_SERVICE_STATUS[cbBytesNeeded];
 cbBufSize = cbBytesNeeded;

    nRet = EnumServicesStatus(hSCManager,SERVICE_WIN32,SERVICE_STATE_ALL,lpServices,cbBufSize,&cbBytesNeeded,&dwServicesReturned,0);
    if(nRet == 0)
    {
        return;
    }
    if(lpServices == 0)
    {
        CloseServiceHandle(hSCManager);
        return;
    }
 for(DWORD i = 0; i < dwServicesReturned; i++, lpServices++)
 {
  LVITEM lvi;
  lvi.mask = LVIF_TEXT | LVIF_IMAGE;
  lvi.state =LVIS_STATEIMAGEMASK;
  lvi.stateMask =0;
  lvi.iSubItem = 0;
  lvi.iItem = i++;
  lvi.pszText = lpServices->lpServiceName;
  lvi.iImage = 0;
  int i = ListView_InsertItem(hServiceList,&lvi);
  ListView_SetItemText(hServiceList,i,1,lpServices->lpDisplayName);
  ListView_SetItemText(hServiceList,i,2,ServiceSysGetType(lpServices->ServiceStatus.dwCurrentState));
 }
 CloseServiceHandle(hSCManager);

------------------------------------------启动

 SC_HANDLE hSCManager = NULL,hService=NULL;
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if(hSCManager == NULL)
    {
  MessageBox(NULL,"err hsc",0,0);
        return ;
    }
 hService = OpenService(hSCManager,sername,SERVICE_ALL_ACCESS);
 if(hService==NULL)
 {
  MessageBox(NULL,"err serv",0,0);
  return;
 }
 if(StartService(hService,0,NULL))
 {
  CloseServiceHandle(hSCManager);
  ServiceSysEnum();
 }

-------------------------------------停止--------------

 SC_HANDLE hSCManager,hService;
    hSCManager = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS);
    if(hSCManager == NULL)
    {
        return;
    }

    hService = OpenService(hSCManager,sername,SERVICE_ALL_ACCESS);
    if(hService == NULL)
    {
        CloseServiceHandle(hSCManager);
        return;
    }

    if(!StartService(hService,0,NULL))
    {
  SERVICE_STATUS ServerStatus;
        ControlService(hService,SERVICE_CONTROL_STOP,&ServerStatus);
        CloseServiceHandle(hService);
        CloseServiceHandle(hSCManager);
  ServiceSysEnum();
        return;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值