开启服务并设置服务启动方式

/*******************************************************************************
函数名:OpenServerWithStartTypeByName
函数功能:以请求的方式打开服务
函数参数:
strServerName: 服务名称
nStartType: 启动方式
函数返回值:
TRUE : 设置成功
FALSE: 设置失败
函数描述:
********************************************************************************/
BOOL OpenServerWithStartTypeByName(CString strServerName,int nStartType)
{
if((SERVICE_BOOT_START>nStartType)||(SERVICE_DISABLED<nStartType))
{
OutputDebugStringA("OpenServerWithAutoByName----无法以设置的方式启动服务");
return FALSE;
}

BOOL bRet = TRUE;
SC_HANDLE hScm;  
SC_HANDLE hService; 
//LPQUERY_SERVICE_CONFIG lpsc; 
DWORD dwBytesNeeded, cbBufSize, dwError;
CString strServicename = strServerName;
//打开服务管理器
if((hScm=OpenSCManagerA(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)  
{  
CloseServiceHandle(hScm);  
int nErr = GetLastError();
CString strErr ;
strErr.Format("NTP:Get Service Manager Fail:%d",nErr);
OutputDebugStringA(strErr);
return false;  
}  
//打开服务
hService=OpenServiceA(hScm, strServicename,SERVICE_ALL_ACCESS);  
if(!hService)
{  


CloseServiceHandle(hScm); 
int nErr = GetLastError();
CString strErr ;
strErr.Format("NTP:Get Service Handle Fail:%d",nErr);
OutputDebugStringA(strErr);
return FALSE;
}
//改变服务启动方式为自动
QUERY_SERVICE_CONFIGA ServiceConfig;
DWORD dwNeed = 0;
//查询服务启动状态
QueryServiceConfigA(hService,&ServiceConfig,sizeof(QUERY_SERVICE_CONFIGA),&dwNeed);
if (nStartType!=ServiceConfig.dwStartType)
{
//改服务状态
SC_LOCK sclLockA;  
sclLockA = LockServiceDatabase(hScm);
if (sclLockA != NULL)
{
bRet = ChangeServiceConfigA(  
hService, // handle of service  
SERVICE_NO_CHANGE, // service type: no change  
nStartType, // 这里做了更改  
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);
if (FALSE == bRet)
{
CloseServiceHandle(hService);  
CloseServiceHandle(hScm); 
return FALSE;
}


}
else
{
SC_LOCK sclLockB;  
sclLockB = LockServiceDatabase(hScm);  
if (sclLockB != NULL)
{
bRet = ChangeServiceConfigA(  
hService, // handle of service  
SERVICE_NO_CHANGE, // service type: no change  
nStartType, // 这里做了更改  
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);
if (FALSE == bRet)
{
CloseServiceHandle(hService);  
CloseServiceHandle(hScm); 
return FALSE;
}
}
}


}
SERVICE_STATUS status; 
//查询服务状态
QueryServiceStatus(hService,&status);
if(SERVICE_RUNNING == status.dwCurrentState)
{
//服务已经开启
CloseServiceHandle(hService);  
CloseServiceHandle(hScm); 
return TRUE;
}
if(SERVICE_STOPPED == status.dwCurrentState)
{
//服务未启动则开启服务
bRet = StartServiceA(hService, 0, NULL);
if ((0 == bRet)||(ERROR_SERVICE_DISABLED == GetLastError()))
{
CloseServiceHandle(hService);  
CloseServiceHandle(hScm); 
return FALSE;


}
}
CloseServiceHandle(hService);  
CloseServiceHandle(hScm); 
return TRUE;




}

使用示例:

CString strServiceName = "W32Time";
BOOL bRet =  OpenServerWithStartTypeByName(strServiceName,SERVICE_AUTO_START);
if (FALSE == bRet)
{
//MessageBox(NULL,"Open Service Fail",NULL,0);
return FALSE;

}

猜你喜欢

转载自blog.csdn.net/xyb_l_code/article/details/79915675