// 重启指定服务
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. mysql 变量set

    在游标循环中,使用使用select into 变量var时,再判断var isnull 或者length(var)=0时,跳出循环. 解决方式: 使用set var=(select id from t ...

  2. table标签

    table标签使我们最常用的的标签,在使用table标签时我们要注意一些其属性,早期我们经常使用table标签对其进行页面布局但是现在我们基本不再使用,由此可见table标签也是非常强大的一个工具. ...

  3. Verilog中锁存器与多路选择器

    Verilog中锁存器与多路选择器 Verilog是一种硬件描述语言,它代表的是硬件. Verilog代表的就是逻辑门和连接线. 对于一个always@(*)控制的块而言,只要块中的表达式包含的任意的 ...

  4. YII框架中php入口文件隐藏

    Apache配置修改 主要修改下httpd文件中的两个地方 1.启用mod_rewrite.so模块,在Apache的配置文件中找到如下行,去掉前面的字符"#",保存 #LoadM ...

  5. JQuery 代码

    http://baike.baidu.com/view/136475.htmhttp://www.cnblogs.com/gleamy_ming/archive/2009/04/29/1446492. ...

  6. java分布式开发,什么是分布式开发

    就是同一个服务,把数据库的不同部分分开建立到不同的服务器上.以缓解数据库大量数据访问的压力.很多大公司的业务量比较大,每天的访问量都达到几百万上千万,甚至上亿的访问量,在访问量不是很大的情况下,是可以 ...

  7. Hadoop参数优化

    dfs.block.size 决定HDFS文件block数量的多少(文件个数),它会间接的影响Job Tracker的调度和内存的占用(更影响内存的使用), mapred.map.tasks.spec ...

  8. 商务智能(BI)技术

    以下内容仅为兴趣爱好. 商务智能技术是将数据仓库.联机分析处理(OLAP)和数据挖掘等结合起来应用到商业活动中,从不同的数据源收集数据,经过抽取(Extract).转换(Transform)和加载(L ...

  9. preventDefault() 方法 取消掉与事件关联的默认动作

    前几天写的 响应键盘的图片切换 中, 键盘总是让浏览器滚动,为了取消掉默认的事件,使用了 preventDefault() 方法 定义和用法 preventDefault() 方法取消事件的默认动作. ...

  10. 1.tomcat部署项目的几种方式和weblogic部署方式及一点通讯

      第一种部署方式: 直接使用myeclipse 找到server服务 添加要部署的项目Add Deployment ,然后选中某个项目,首选Exploded Archive(development ...