public static int StopServiceRecursive(IntPtr pHandle) { int iRet = 0; bool bIsDependenciesStopped = false; try { iRet = ServiceManagerInteropWrapper.ApiLwSmStopService(pHandle); if (iRet == (int)41205) { string[] servicelist = null; iRet = ApiLwSmQueryServiceReverseDependencyClosure(pHandle, out servicelist); if (servicelist != null && servicelist.Length != 0) { foreach (string service in servicelist) { IntPtr pDHandle = ServiceManagerInteropWrapper.ApiLwSmAcquireServiceHandle(service); StopServiceRecursive(pDHandle); } bIsDependenciesStopped = true; } } if (bIsDependenciesStopped) { iRet = ServiceManagerInteropWrapper.ApiLwSmStopService(pHandle); } } catch (Exception ex) { Logger.LogException("ServiceManagerInteropWrapper:StartAllServiceDependencies", ex); } return(iRet); }
public static int StartAllServiceDependencies(IntPtr pHandle, ref int iReturn) { try { if (pHandle != IntPtr.Zero) { ServiceManagerApi.LW_SERVICE_INFO serviceInfo = ServiceManagerInteropWrapper.ApiLwSmQueryServiceInfo(pHandle); ServiceManagerApi.LW_SERVICE_STATUS serviceStatus = ServiceManagerInteropWrapper.ApiLwSmQueryServiceStatus(pHandle); if (serviceStatus.state == ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING) { Logger.Log("Dependency servcie: " + serviceInfo.pwszName + " running"); return(iReturn); } if (serviceInfo.ppwszDependencies != null && serviceInfo.ppwszDependencies.Length != 0) { foreach (string service in serviceInfo.ppwszDependencies) { Logger.Log("Dependency servcie: " + service); IntPtr pDHandle = ServiceManagerInteropWrapper.ApiLwSmAcquireServiceHandle(service); if (pDHandle != IntPtr.Zero) { ServiceManagerApi.LW_SERVICE_INFO InnerServiceInfo = ServiceManagerInteropWrapper.ApiLwSmQueryServiceInfo(pDHandle); ServiceManagerApi.LW_SERVICE_STATUS Status = ServiceManagerInteropWrapper.ApiLwSmQueryServiceStatus(pDHandle); if (InnerServiceInfo.ppwszDependencies == null || InnerServiceInfo.ppwszDependencies.Length == 0) { if (Status.state != ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING) { iReturn = ServiceManagerInteropWrapper.ApiLwSmStartService(pDHandle); if (iReturn != 0) { return(iReturn); } } else { Logger.Log("Dependency servcie: " + service + " running"); } } else { StartAllServiceDependencies(pDHandle, ref iReturn); iReturn = ServiceManagerInteropWrapper.ApiLwSmStartService(pDHandle); } ServiceManagerInteropWrapper.ApiLwSmReleaseServiceHandle(pDHandle); } } iReturn = ServiceManagerInteropWrapper.ApiLwSmStartService(pHandle); } else { if (serviceStatus.state != ServiceManagerApi.LW_SERVICE_STATE.LW_SERVICE_STATE_RUNNING) { iReturn = ServiceManagerInteropWrapper.ApiLwSmStartService(pHandle); } } } } catch (Exception ex) { Logger.LogException("ServiceManagerInteropWrapper:StartAllServiceDependencies", ex); } return(iReturn); }