首页 > Windows开发 > 详细

Windows启动及停止服务

时间:2015-07-14 15:06:04      阅读:198      评论:0      收藏:0      [点我收藏+]

// 启动服务

SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);  // 创建到服务控制管理器的连接

if (schSCManager == NULL)

{

  return FALSE;

}

SC_HANDLE schService = OpenService(schSCManager, _T("*"), SERVICE_ALL_ACCESS | DELETE);  // 打开服务, *表示服务名称

if (schService == NULL)
{
  return FALSE;
}
SERVICE_STATUS service;
QueryServiceStatus(schService, &service);
if (service.dwCurrentState == SERVICE_STOPPED)
{
  StartService(schService, 0, NULL);
  CloseServiceHandle(schSCManager);
  CloseServiceHandle(schService);
}

 

// 停止服务

SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
  return FALSE;
}
SC_HANDLE schService = OpenService(schSCManager, _T("*"), SERVICE_ALL_ACCESS | DELETE);
if (schService == NULL)
{
  return FALSE;
}
SERVICE_STATUS service;
QueryServiceStatus(schService, &service);
if (service.dwCurrentState == SERVICE_RUNNING)
{
  ControlService(schService, SERVICE_CONTROL_STOP, &service);
  CloseServiceHandle(schSCManager);
  CloseServiceHandle(schService);
}

Windows启动及停止服务

原文:http://www.cnblogs.com/92-05-29/p/4645409.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!