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 ...
随机推荐
- android插件化-获取apkplug框架已安装插件-03
上一篇文章成功的将apkplug框架嵌入了应用中而且启动 链接http://www.apkplug.com/blog/?post=10 这一篇文章实现怎样获取全部已安装插件 一 获取框架的System ...
- SVN经常使用命令说明
SVN版本号:1.5 及更新版本号 名词说明: WC:Working Copy 你的工作区 Versioned:受控的:受版本号控制的 SVN是什么? SVN是开源的版本号控制系统. 比CVS很多其它 ...
- 使用正则表达式给网址添加a标签
在内容中存在链接地址的时候,我们在前台显示时一定想自动的将地址添加上a标签,方便用户进入链接.使用正则表达式就能轻松实现. Jsvascript正则替换 //javascript 正则替换 var s ...
- 手工释放Linux内存
转载自:http://blog.csdn.net/wyzxg/article/details/7279986/ linux的内存查看: [root@localhost 0.1.0]# free -m ...
- java获取字符串格式日期向前或向后n天的日期
private void setTilteMessage(){ BaseDao dao = new BaseDao(); String titleData = da ...
- 网络编程 socket-实例
1.设计界面: 2.效果界面: 3.具体实现代码: public partial class frmMain : Form { public frmMain() { InitializeC ...
- sql语法图
- 详解boost库中的Message Queue .
Message Queue(后文简写成MQ或消息队列)是boost库中用来封装进程间通信的一种实现,同一台机器上的进程或线程可以通过消息队列来进行通迅.消息队列中的消息由优先级.消息长度.消息数据三部 ...
- 读书笔记 -part1
自从毕业以后到现在~看的书是越来越少了 の其实好像貌似从来没有认认真真的看书 除非工作遇到难于解决的问题迫不得已才去翻书看 有些问题也是莫名其妙的就这样解决了 于是乎被人美名其曰“高人”或&quo ...
- phpcms v9联动菜单的调用方法_详解get_linkage函数
phpcms v9联动菜单调用方法[此为内容页调用方法]: {get_linkage($areaid,1,' >> ',1)} 显示效果: phpcms吧 >> 模板下载 &g ...