码迷,mamicode.com
首页 > Windows程序 > 详细

Windows启动及停止服务

时间:2015-07-14 15:08:15      阅读:147      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!