启动指定的服务(LanmanServer)

//1、判断服务状态,如果是启动状态,直接返回;否则进行第2步
//2、判断服务启动类型是否是禁用,如果是禁用,那么改为手动;修改成功进行第3步,否则返回失败;
//3、启动服务;成功返回TURE,失败返回FALS;


BOOL CheckService()
{
SERVICE_STATUS_PROCESS ssStatus; 
DWORD dwBytesNeeded;
BOOL bRet = FALSE;
SC_HANDLE schSCManager;
SC_HANDLE schService;
CString strDbg;


do 
{
// 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) 
{
strDbg.Format(_T("CheckService: OpenSCManager failed (%d)\n"), GetLastError());
return FALSE;
}


// Get a handle to the service.


schService = OpenService( 
schSCManager,         // SCM database 
_T("LanmanServer"),   // name of service 
SERVICE_ALL_ACCESS);  // full access 


if (schService == NULL)

strDbg.Format(_T("CheckService: OpenService failed (%d)\n"), GetLastError()); 
CloseServiceHandle(schSCManager);
return FALSE;
}    


// Check the status in case the service is not stopped. 


if (!QueryServiceStatusEx( 
schService,                     // handle to service 
SC_STATUS_PROCESS_INFO,         // information level
(LPBYTE) &ssStatus,             // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) )              // size needed if buffer is too small
{
strDbg.Format(_T("CheckService: QueryServiceStatusEx failed (%d)\n"), GetLastError());
break; 
}


// Check if the service is already running. It would be possible 
// to stop the service here, but for simplicity this example just returns. 


if(ssStatus.dwCurrentState != SERVICE_STOPPED)
{
strDbg.Format(_T("CheckService: do not need start LanmanServer because it is already running\n"));
bRet = TRUE;
break; 
}

LPQUERY_SERVICE_CONFIG lpsc = NULL;
DWORD dwBytesNeeded, cbBufSize, dwError;
if( !QueryServiceConfig( 
schService, 
NULL, 
0, 
&dwBytesNeeded))
{
dwError = GetLastError();
if( ERROR_INSUFFICIENT_BUFFER == dwError )
{
cbBufSize = dwBytesNeeded;
lpsc = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LMEM_FIXED, cbBufSize);
}
else
{
strDbg.Format(_T("CheckService: QueryServiceConfig failed (%d)\n"), dwError);
break; 
}
}


if( !QueryServiceConfig( 
schService, 
lpsc, 
cbBufSize, 
&dwBytesNeeded) ) 
{
strDbg.Format(_T("CheckService: QueryServiceConfig failed (%d)"), GetLastError());
break;
}


BOOL bDisable = (lpsc->dwStartType == SERVICE_DISABLED);
LocalFree(lpsc);


if (bDisable)
{
//修改启动类型
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
{
strDbg.Format(_T("CheckService: ChangeServiceConfig failed (%d)\n"), GetLastError());
break;
}
else
{
strDbg.Format(_T("CheckService: Service demand start successfully.\n"));
OutputDebugString(strDbg);
}
}


//启动服务
if (!StartService(
schService,  // handle to service 
0,           // number of arguments 
NULL) )      // no arguments 
{
strDbg.Format(_T("CheckService: StartService failed (%d)\n"), GetLastError());
break;
}
else 
{
strDbg.Format(_T("CheckService: Service start pending...\n")); 
Sleep(1000*3);
bRet = TRUE;
break;
}



} while (FALSE);

CloseServiceHandle(schService); 
CloseServiceHandle(schSCManager);
OutputDebugString(strDbg);


return bRet;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值