//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;
}
//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;
}