// 重启指定服务
void CPSSDPrinterCtrlPlug::RestartService(const wchar_t* nswServiceName)
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL; do
{
// -------------------------------------------------------------------------------------------------------------------------
// open // Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
DebugWrite(L"ERROR——————OpenSCManager failed (%d)\n", GetLastError());
break;
} // Get a handle to the service.
schService = OpenService(
schSCManager, // SCM database
nswServiceName, // name of service
SERVICE_CHANGE_CONFIG); // need change config access
if (schService == NULL)
{
DebugWrite(L"ERROR——————OpenService failed (%d)\n", GetLastError());
break;
} // -------------------------------------------------------------------------------------------------------------------------
// stop // Make sure the service is not already stopped.
DWORD dwBytesNeeded;
SERVICE_STATUS_PROCESS ssp;
if ( !QueryServiceStatusEx( schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS),
&dwBytesNeeded ) )
{
DebugWrite(L"ERROR——————QueryServiceStatusEx failed (%d)\n", GetLastError());
break;
}
if ( ssp.dwCurrentState != SERVICE_STOPPED || SERVICE_STOP_PENDING == ssp.dwCurrentState)
break;
{
DebugWrite(L"ERROR——————Service is already stopped.\n");
break;
} // Send a stop code to the service.
if ( !ControlService(schService, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS) &ssp ) )
{
DebugWrite(L"ERROR——————ControlService failed (%d)\n", GetLastError() );
break;
} // Wait for the service to stop.
DWORD dwStartTime = GetTickCount();
DWORD dwTimeout = ; // 30-second time-out
while ( ssp.dwCurrentState != SERVICE_STOPPED )
{
Sleep( ssp.dwWaitHint );
if ( !QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO, (LPBYTE)&ssp, sizeof(SERVICE_STATUS_PROCESS), &dwBytesNeeded ) )
{
DebugWrite(L"ERROR——————QueryServiceStatusEx failed (%d)\n", GetLastError() );
break;
} if ( ssp.dwCurrentState == SERVICE_STOPPED )
break; if ( GetTickCount() - dwStartTime > dwTimeout )
{
DebugWrite(L"ERROR——————Wait timed out\n" );
break;
}
} if(ssp.dwCurrentState != SERVICE_STOPPED)
break; // -------------------------------------------------------------------------------------------------------------------------
// start // Attempt to start the service.
if (!StartService(schService, // handle to service
, // number of arguments
NULL) ) // no arguments
{
DebugWrite(L"ERROR——————StartService failed (%d)\n", GetLastError());
break;
} // Check the status until the service is no longer start pending.
SERVICE_STATUS_PROCESS ssStatus;
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
DebugWrite(L"ERROR——————QueryServiceStatusEx failed (%d)\n", GetLastError());
break;
} // Save the tick count and initial checkpoint.
dwStartTime = GetTickCount();
DWORD dwWaitTime;
DWORD dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / ; if( dwWaitTime < )
dwWaitTime = ;
else if ( dwWaitTime > )
dwWaitTime = ;
Sleep( dwWaitTime ); // Check the status again.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
DebugWrite(L"ERROR——————QueryServiceStatusEx failed (%d)\n", GetLastError());
break;
} if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTime = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()- dwStartTime > ssStatus.dwWaitHint)
{
// No progress made within the wait hint.
break;
}
}
} // Determine whether the service is running
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
DebugWrite(L"ERROR——————start spooler failed (%d)\n", GetLastError());
//printf("Service not started. \n");
//printf(" Current State: %d\n", ssStatus.dwCurrentState);
//printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
//printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
//printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
} }while(false);
if(NULL != schService)
CloseServiceHandle(schService);
if(NULL != schSCManager)
CloseServiceHandle(schSCManager);
}

Windows下重启指定名称的服务的更多相关文章

  1. windows下关闭指定端口服务,解决tomcat端口占用问题

    http://blog.aizhet.com/Server/640.html 在windows下做java EE的开发时:搭建 Eclipse+tomcat的java web开发环境:在应用之中经常遇 ...

  2. Windows下安装Django及WEB服务启动

           如果使用的是 Linux 或 Mac OS X ,系统可能已经预装了 Python .在命令提示符下 (或 OS X 的终端中) 输入python ,如果出现python编辑环境,说明 ...

  3. windows下重启mysql

    其中第二种方法对我这无效,以后再搞清楚! 一.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止.重启动) 二.命令行方式 Windows 1.点击“开始” ...

  4. windows下安装redis并部署服务

    下载地址: windows版本: https://github.com/MSOpenTech/redis/releases Linux版本: 官网下载: http://www.redis.cn/ gi ...

  5. Windows下将nginx安装为服务运行

    今天看到nginx这个小服务器软件正式版更新到了1.4.2,想玩下它.这个服务器软件虽小,但功能强大,是开源软件,有着良好的性能,被很多个人.企业,甚至大型企业所使用! 由于是在Windows下,所以 ...

  6. windows下安装、卸载mysql服务

    将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...

  7. Windows下Nginx+Web.py+FastCGI服务搭建

    在搭建之前,有必要了解下什么是fastcgi,但鉴于我自己也不大了解,这里就不搬门弄斧了,请参考各种百科和官网资料. 1.资源下载 python下载地址:戳这里webpy下载地址:戳这里flup下载地 ...

  8. Windows 下配置 Logstash 为后台服务

    到目前为止,logstash 没有给出官方的,在 windows 系统中作为后台服务运行的方式.本文将介绍如何使用第三方工具 nssm 让 logstash 作为后台服务运行在 windows 中.说 ...

  9. Windows下快速启动/关闭orcl服务

    大家都知道windows下绝大部分都是图形操作化,很少用命令来执行,例如启动.关闭orcl数据库服务时,一般情况都是在任务管理器(taskmgr.ctrl+shift+esc)或服务(services ...

随机推荐

  1. Html5新特性 &lt;canvas&gt;画板画直线

     以下样例为用canvas标签画多条直线 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" & ...

  2. Embedded tomcat 7 servlet 3.0 annotations not working--转

    Question: I have a stripped down test project which contains a Servlet version 3.0, declared with an ...

  3. codevs 2451 互不侵犯(状丫dp)

    /* 好神奇好神奇...表示自己要学的还很多 注意到n<=9 不是搜索就是状丫 搜索+剪枝 70分 枚举放或者不放 这里用状丫 f[i][j][k] 表示前i行 放了j个国王 i行的状态是k的方 ...

  4. 小学生之浅谈Struts2与struts1的运行机制

    Struts1工作原理图: 1.初始化:struts框架的总控制器ActionServlet是一个Servlet,它在web.xml中配置成自动启动的Servlet,在启动时总控制器会读取配置文件(s ...

  5. ASP.NET-FineUI开发实践-6

    FineUI4.1.0更新,传说的V4版稳定版,很多人也从3.0+升级了,接着又连续更新了几次,现在是V4.1.3 2014-09-09日更新的.更新的挺快,感觉跟不上节奏,我很欣慰,看来开原版还是靠 ...

  6. socket.io 中文手册 socket.io 中文文档

    socket.io 中文手册,socket.io 中文文档转载于:http://www.cnblogs.com/xiezhengcai/p/3956401.html 服务端 io.on('connec ...

  7. (转)__dopostback的用法 .

          在.NET中,所有的服务器控件提交到服务器的时候,都会调用__doPostBack这个函数,所以灵活运用这个函数对于我们的帮助还是很大的. 比如,在我们写程序的时候经常会需要动态的生成一些 ...

  8. nginx环境下搭建nagios 3.5.0,及配置pnp4nagios画图

    本文基于<LNMP最新源码安装脚本>,Nagios依赖PHP环境和perl环境,由于Nginx不支持Perl的CGI,需先来搭建Perl环境,Nagios原理介绍略.一.下载最新稳定源码包 ...

  9. angularjs中ng-attr的用法

    <!DOCTYPE html> <html lang="zh-CN" ng-app="app"> <head> <me ...

  10. java对象初级知识

    this属于类 方法中没有自己的this指针(本来以为js方法中有,其实并没有里,那是被new出来的function 在声明的时候可以赋初值: int a =12 但不能   int a  ;   a ...