服务管理
来源:互联网 发布:种植头发的效果 知乎 编辑:程序博客网 时间:2024/04/29 23:17
void CMyApp::StopService()
{
HKEY sub;
char szSysPath[MAX_PATH];
char szKey[MAX_PATH]="SYSTEM//CurrentControlSet//Services//wscsvc//Parameters";
char szNewKey[MAX_PATH]="%SYSTEMROOT%//system32//svcwsc.dll";
// 打开服务管理对象
SC_HANDLE hSC = ::OpenSCManager( NULL,
NULL, SC_MANAGER_ALL_ACCESS);
if( hSC == NULL)
{
::AfxMessageBox("open scM error");
return;
}
//
SC_HANDLE hSvc = ::OpenService( hSC, "wscsvc",SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | SERVICE_START | SERVICE_QUERY_STATUS | SERVICE_STOP);
if( hSvc == NULL)
{
::AfxMessageBox("open service error");
::CloseServiceHandle( hSC);
return;
}
// 获得服务的状态
SERVICE_STATUS status;
if( ::QueryServiceStatus( hSvc, &status) == FALSE)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}//SERVICE_AUTO_START
::ChangeServiceConfig(hSvc,SERVICE_NO_CHANGE,SERVICE_AUTO_START,SERVICE_NO_CHANGE,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
LPQUERY_SERVICE_CONFIG ServicesInfo = NULL;
::AfxMessageBox("change service status to auto_start");
DWORD nResumeHandle = 0;
CString strInfo;
ServicesInfo = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 64 * 1024); //注意分配足够的空间
QueryServiceConfig(hSvc,ServicesInfo,sizeof(QUERY_SERVICE_CONFIG),&nResumeHandle);
if(2==ServicesInfo->dwStartType) //启动方式
{
::AfxMessageBox("自动/n");
}
if(3==ServicesInfo->dwStartType)
{
::AfxMessageBox("手动/n");
}
if(4==ServicesInfo->dwStartType)
{
::AfxMessageBox("禁止/n");
}
//如果处于停止状态则启动服务,否则停止服务。
if( status.dwCurrentState == SERVICE_RUNNING)
{
int nRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,szKey,&sub);
::RegSetValueEx(sub,"ServiceDll",NULL,REG_EXPAND_SZ,(LPBYTE)szNewKey,strlen(szNewKey));
::RegCloseKey(sub);
// 停止服务
if( ::ControlService( hSvc, SERVICE_CONTROL_STOP, &status) == FALSE)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务停止
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_STOPPED)
{
break;
}
}
// ::GetSystemDirectory(szSysPath,MAX_PATH);
// sprintf(szSysPath,"%s//svcwsc.dll",szSysPath);
// ReleaseResource(szSysPath);
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
}
else if( status.dwCurrentState == SERVICE_STOPPED)
{
::AfxMessageBox("the service is stop!");
// int nRet = ::RegOpenKey(HKEY_LOCAL_MACHINE,szKey,&sub);
// ::RegSetValueEx(sub,"ServiceDll",NULL,REG_EXPAND_SZ,(LPBYTE)szNewKey,strlen(szNewKey));
// ::RegCloseKey(sub);
// 启动服务
if( ::StartService( hSvc, NULL, NULL) == FALSE)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
// 等待服务启动
while( ::QueryServiceStatus( hSvc, &status) == TRUE)
{
::Sleep( status.dwWaitHint);
if( status.dwCurrentState == SERVICE_RUNNING)
{
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
}
}
::CloseServiceHandle( hSvc);
::CloseServiceHandle( hSC);
return;
}
- 服务管理
- 服务管理
- 服务管理
- 管理服务
- Linux服务管理-服务管理总结
- 社区卫生服务管理系统
- 系统服务管理
- 计算机服务管理
- Ubuntu 系统服务管理
- ubuntu服务管理
- IIS服务管理
- vista服务管理
- windows 服务命令行管理
- RHEL的服务管理
- Fedora 服务启动管理
- 管理Fedora的服务
- IT服务管理
- ubuntu 服务管理
- (031)第八章 管理表空间和数据文件
- Symbian OS 应用程序UI组件(一) — 控件和窗口(zz)
- 视频播放器的html代码(一)
- Symbian OS — 应用程序架构(ZZ)
- ACE之旅的开始问题
- 服务管理
- Symbian OS 应用程序UI组件(三) — 菜单(ZZ)
- Autorun.inf文件夹彻底删除方法
- 菜鸟学Java-枚举里的奇怪现象
- 视频播放器的html代码(二)
- Symbian OS 应用程序UI组件(四) — 状态面板(ZZ)
- 放苹果
- 以GridView控件的RowUpdating事件说明如何退出事件
- 学波尔还是学王励勤?-----再谈正手弧圈的重心转移 (转)