VC 关于windows服务状态更改
2012-05-08 11:56
537 查看
更改服务状态
摘自:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682006(v=vs.85).aspx
启动、停止服务
摘自:http://blog.csdn.net/huangchonghai/article/details/1583602
#include <winsvc.h>
void CStartServiceDlg::OnBnClickedButton1()
{
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, GENERIC_EXECUTE);
if( hSC == NULL)
{
TRACE( "open SCManager error");
return;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC",
SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
TRACE( "Open www erron。");
::CloseServiceHandle( hSC);
return;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
TRACE( "Get Service state error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
//如果处于停止状态则启动服务,否则停止服务。
if( status.dwCurrentState == SERVICE_RUNNING)
{
// 停止服务
if( ::ControlService( hSvc,
SERVICE_CONTROL_STOP, &status) == FALSE)
{
TRACE( "stop service error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
AfxMessageBox( "stop success。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
TRACE( "start service error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
AfxMessageBox( "start success。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
TRACE( "start error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
摘自:http://msdn.microsoft.com/en-us/library/windows/desktop/ms682006(v=vs.85).aspx
// Purpose: // Disables the service. // // Parameters: // None // // Return value: // None // VOID __stdcall DoDisableSvc() { SC_HANDLE schSCManager; SC_HANDLE schService; // 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) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_CHANGE_CONFIG); // need change config access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } // Change the service start type. if (! ChangeServiceConfig( schService, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_DISABLED, // service start type SERVICE_NO_CHANGE, // error control: no change NULL, // binary path: no change NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL) ) // display name: no change { printf("ChangeServiceConfig failed (%d)\n", GetLastError()); } else printf("Service disabled successfully.\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } // // Purpose: // Enables the service. // // Parameters: // None // // Return value: // None // VOID __stdcall DoEnableSvc() { SC_HANDLE schSCManager; SC_HANDLE schService; // 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) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_CHANGE_CONFIG); // need change config access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } // Change the service start type. if (! ChangeServiceConfig( schService, // handle of service SERVICE_NO_CHANGE, // service type: no change SERVICE_DEMAND_START, // service start type SERVICE_NO_CHANGE, // error control: no change NULL, // binary path: no change NULL, // load order group: no change NULL, // tag ID: no change NULL, // dependencies: no change NULL, // account name: no change NULL, // password: no change NULL) ) // display name: no change { printf("ChangeServiceConfig failed (%d)\n", GetLastError()); } else printf("Service enabled successfully.\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); } // // Purpose: // Updates the service description to "This is a test description". // // Parameters: // None // // Return value: // None // VOID __stdcall DoUpdateSvcDesc() { SC_HANDLE schSCManager; SC_HANDLE schService; SERVICE_DESCRIPTION sd; LPTSTR szDesc = TEXT("This is a test description"); // 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) { printf("OpenSCManager failed (%d)\n", GetLastError()); return; } // Get a handle to the service. schService = OpenService( schSCManager, // SCM database szSvcName, // name of service SERVICE_CHANGE_CONFIG); // need change config access if (schService == NULL) { printf("OpenService failed (%d)\n", GetLastError()); CloseServiceHandle(schSCManager); return; } // Change the service description. sd.lpDescription = szDesc; if( !ChangeServiceConfig2( schService, // handle to service SERVICE_CONFIG_DESCRIPTION, // change: description &sd) ) // new description { printf("ChangeServiceConfig2 failed\n"); } else printf("Service description updated successfully.\n"); CloseServiceHandle(schService); CloseServiceHandle(schSCManager); }
启动、停止服务
摘自:http://blog.csdn.net/huangchonghai/article/details/1583602
#include <winsvc.h>
void CStartServiceDlg::OnBnClickedButton1()
{
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, GENERIC_EXECUTE);
if( hSC == NULL)
{
TRACE( "open SCManager error");
return;
}
// 打开www服务。
SC_HANDLE hSvc = ::OpenService( hSC, "W3SVC",
SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
TRACE( "Open www erron。");
::CloseServiceHandle( hSC);
return;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
TRACE( "Get Service state error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
//如果处于停止状态则启动服务,否则停止服务。
if( status.dwCurrentState == SERVICE_RUNNING)
{
// 停止服务
if( ::ControlService( hSvc,
SERVICE_CONTROL_STOP, &status) == FALSE)
{
TRACE( "stop service error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
AfxMessageBox( "stop success。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
TRACE( "start service error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
AfxMessageBox( "start success。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
TRACE( "start error。");
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
相关文章推荐
- VC关于windows服务状态更改
- 关于windows的锁定状态
- 基于C#实现Windows服务状态启动和停止服务的方法
- VC2010 编写windows service 服务程序
- 关于C#开发 windows服务进程
- windows关于mysql忘记密码的情况下更改密码
- zabbix 关于监控windows 服务器的自定义服务的CPU 和内存占用 探索
- 关于windows服务方面的一些问题
- 用 VC++建立 Windows 服务程序
- vc 写windows 服务
- c# windows服务状态、启动和停止服务
- 用 VC 建立 Windows 服务程序
- 关于重装Windows组件服务
- 关于OracleOraDb11g_home1TNSListener监听程序在windows服务列表中消失问题
- 调用windows服务来监视文件夹状态
- 关于windows下安装wampserver服务器系统丢失msvcr100.dll及VCRUNTIME140.dll的解决办法
- VC制作 Windows服务 安装包
- VC++ 系统服务如何让服务进程结束后依赖windows自身把服务重启