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 ...
随机推荐
- 【高级算法】模拟退火算法解决3SAT问题(C++实现)
转载请注明出处:http://blog.csdn.net/zhoubin1992/article/details/46453761 ---------------------------------- ...
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- JAVA实现AES和MD5加密
package test; import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; ...
- AsyncTask两种线程池
AsyncTask两种线程池 http://bbs.51cto.com/thread-1114378-1.html (API 3.0以后): 1.THREAD_POOL_EXECUTOR, ...
- linux elinks命令
Elinks是基于文本的免费浏览器,用于Unix及基于Unix的系统.Elinks支持 HTTP,HTTP Cookies以及支持浏览Perl和Ruby脚本.也很好的支持选项卡浏览.最棒的是它支持鼠标 ...
- CSS优先级总结(转载)
样式的优先级 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下: (外部样式)External styl ...
- IE8兼容placeholder的方案
用JavaScript解决Placeholder的IE8兼容问题 placeholder属性是HTML5新添加的属性,当input或者textarea设置了该属性后,该值的内容将作为灰色提示显示在文本 ...
- 再谈Cookies欺骗
在上一篇关于cookies欺骗的随笔中,提到的解决方案是把密码MD5加密之后存入cookies中,确实这种方法实现了效果,不过把密码留在客户端等待着去被破解不是一个合适的方法,在此也感谢 @老牛吃肉 ...
- c-整型家族(integer family)
C中,整型有: characters, short integer, integer, long integer 看起来,long integer要比short integer大,但是这也是不一定的. ...
- ASP.NET 文本编辑器使用(CKEditor)与上传图片
CKEditor是什么 CKEidtor是一个在线富文本编辑器,可以将让用户所见即所得的获得编辑在线文本,编辑器或自动将用户编辑的文字格式转换成html代码. 方法一.在ASP.NET工程中添加CKE ...