Windows下重启指定名称的服务
// 重启指定服务
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下重启指定名称的服务的更多相关文章
- windows下关闭指定端口服务,解决tomcat端口占用问题
http://blog.aizhet.com/Server/640.html 在windows下做java EE的开发时:搭建 Eclipse+tomcat的java web开发环境:在应用之中经常遇 ...
- Windows下安装Django及WEB服务启动
如果使用的是 Linux 或 Mac OS X ,系统可能已经预装了 Python .在命令提示符下 (或 OS X 的终端中) 输入python ,如果出现python编辑环境,说明 ...
- windows下重启mysql
其中第二种方法对我这无效,以后再搞清楚! 一.MYSQL服务 我的电脑——(右键)管理——服务与应用程序——服务——MYSQL——开启(停止.重启动) 二.命令行方式 Windows 1.点击“开始” ...
- windows下安装redis并部署服务
下载地址: windows版本: https://github.com/MSOpenTech/redis/releases Linux版本: 官网下载: http://www.redis.cn/ gi ...
- Windows下将nginx安装为服务运行
今天看到nginx这个小服务器软件正式版更新到了1.4.2,想玩下它.这个服务器软件虽小,但功能强大,是开源软件,有着良好的性能,被很多个人.企业,甚至大型企业所使用! 由于是在Windows下,所以 ...
- windows下安装、卸载mysql服务
将下载下来的mysql解压到指定目录下(如:d:\mysql)安装服务在命令行输入d:\mysql\bin\mysqld -installnet start mysql卸载服务在命令行输入net st ...
- Windows下Nginx+Web.py+FastCGI服务搭建
在搭建之前,有必要了解下什么是fastcgi,但鉴于我自己也不大了解,这里就不搬门弄斧了,请参考各种百科和官网资料. 1.资源下载 python下载地址:戳这里webpy下载地址:戳这里flup下载地 ...
- Windows 下配置 Logstash 为后台服务
到目前为止,logstash 没有给出官方的,在 windows 系统中作为后台服务运行的方式.本文将介绍如何使用第三方工具 nssm 让 logstash 作为后台服务运行在 windows 中.说 ...
- Windows下快速启动/关闭orcl服务
大家都知道windows下绝大部分都是图形操作化,很少用命令来执行,例如启动.关闭orcl数据库服务时,一般情况都是在任务管理器(taskmgr.ctrl+shift+esc)或服务(services ...
随机推荐
- 定时关机命令——shutdown
通常会用到的定时关机命令有两种: Shutdown -s -t 36001小时后自己主动关机(3600秒) at 12:00 Shutdown -s 12:00自己主动关闭计算机 系统定时关机: Wi ...
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- jpeg和gif已经影响互联网发展进程了,他们应该被历史淘汰了!!!
本人发现.传统的图片格式已经不适应互联网时代了!!!,故本人发起定义一种新的图片格式.后缀名为 .gnet 互联网上的图片大多有这几种来源.微博上传,视频截图,网络编辑人上传等,以眼下的技术.这些图片 ...
- CCDictionary&CCArray执行retain()重要点
CCDictionary也需要执行retain(),否则则跟CCArray,返回则释放对象. 在Lua中,忘记了retain(),导致一些出现gCCDictionary:objectForKey(ke ...
- Android 仿Win8的metro的UI界面(上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23441455 昨晚没事手机下载了一些APP,发现现在仿win8的主界面越来越多, ...
- -bash: ulimit: open files: cannot modify limit: Operation not permitted
普通用户登录系统报错,提示: -bash: ulimit: open files: cannot modify limit: Operation not permitted. 处理方法: #vi /e ...
- 富文本 Htmll类 html标签
HTML类可解析的标签 在手机上显示从网络端获取的数据有两种方式,一种是WebView,另一种是TextView,WebView大家都知道,功能强大但不灵活,下面主要说下TextView. 通过查看a ...
- C#总结项目《影院售票系统》编写总结完结篇
回顾:昨天总结了影院售票系统核心部分-售票,整个项目也就完成了2/3了,需求中也要求了对销售信息的保存,今天就继续总结销售信息的保存以及加载销售信息. 分析:退出程序时将已售出票集合中的对象循环写入到 ...
- web页面打印
在使用的两种方式打印: 第一种:js如下 function doPrint() { allhtml = window.document.body.innerHTML; starstr = " ...
- Html.ActionLink简单用法(转)
一 Html.ActionLink("要显示的文字","actionName") 该重载的第一个参数是该链接要显示的文字,第二个参数是对应的控制器的方法, 默认 ...