public static string GetRunAsAccountName(string machineName = "") { IntPtr ptr = Marshal.AllocHGlobal(4096); IntPtr svcManagerHandle = IntPtr.Zero; IntPtr svcHandle = IntPtr.Zero; try { svcManagerHandle = NativeMethods.OpenSCManager(machineName, null, NativeMethods.SC_MANAGER_CONNECT); if (svcManagerHandle == IntPtr.Zero) { DeployerTrace.WriteError(StringResources.Error_UnableToChangeOpenServiceManagerHandle, Utility.GetWin32ErrorMessage(Marshal.GetLastWin32Error())); return(null); } svcHandle = NativeMethods.OpenService(svcManagerHandle, Constants.FabricHostServiceName, NativeMethods.SERVICE_QUERY_CONFIG | NativeMethods.SERVICE_CHANGE_CONFIG); if (svcHandle == IntPtr.Zero) { DeployerTrace.WriteError(StringResources.Error_UnableToChangeOpenServiceManagerHandle, Utility.GetWin32ErrorMessage(Marshal.GetLastWin32Error())); return(null); } int bytesNeeded = 0; if (NativeMethods.QueryServiceConfig(svcHandle, ptr, 4096, out bytesNeeded)) { var config = new NativeMethods.QUERY_SERVICE_CONFIG(); Marshal.PtrToStructure(ptr, config); return(config.lpServiceStartName); } else { int error = Marshal.GetLastWin32Error(); if (error == NativeMethods.ERROR_INSUFFICIENT_BUFFER) { Marshal.FreeHGlobal(ptr); ptr = Marshal.AllocHGlobal(bytesNeeded); if (NativeMethods.QueryServiceConfig(svcHandle, ptr, 4096, out bytesNeeded)) { var config = new NativeMethods.QUERY_SERVICE_CONFIG(); Marshal.PtrToStructure(ptr, config); return(config.lpServiceStartName); } else { error = Marshal.GetLastWin32Error(); } } DeployerTrace.WriteError(StringResources.Error_UnableToQueryServiceConfiguration, Utility.GetWin32ErrorMessage(error)); return(null); } } catch (Exception e) { DeployerTrace.WriteError(StringResources.Error_UnableToQueryServiceConfiguration, e); return(null); } finally { Marshal.FreeHGlobal(ptr); if (svcHandle != IntPtr.Zero) { NativeMethods.CloseServiceHandle(svcHandle); } if (svcManagerHandle != IntPtr.Zero) { NativeMethods.CloseServiceHandle(svcManagerHandle); } } }
internal static ServiceStartupType GetServiceStartupType(string machineName, string serviceName) { if (string.IsNullOrEmpty(machineName)) { machineName = "."; } IntPtr svcManagerHandle = IntPtr.Zero; IntPtr svcHandle = IntPtr.Zero; IntPtr lpConfig = IntPtr.Zero; try { svcManagerHandle = NativeMethods.OpenSCManager(machineName, null, NativeMethods.SC_MANAGER_CONNECT); if (svcManagerHandle == IntPtr.Zero) { string errorMessage = string.Format(CultureInfo.InvariantCulture, StringResources.Error_UnableToChangeOpenServiceManagerHandle, Utility.GetWin32ErrorMessage(Marshal.GetLastWin32Error())); DeployerTrace.WriteError(errorMessage); throw new InvalidOperationException(errorMessage); } svcHandle = NativeMethods.OpenService(svcManagerHandle, serviceName, NativeMethods.SERVICE_QUERY_CONFIG); if (svcHandle == IntPtr.Zero) { string errorMessage = string.Format(CultureInfo.InvariantCulture, StringResources.Error_UnableToChangeOpenServiceHandle, Utility.GetWin32ErrorMessage(Marshal.GetLastWin32Error())); DeployerTrace.WriteError(errorMessage); throw new InvalidOperationException(errorMessage); } int bytesNeeded = 0; lpConfig = Marshal.AllocHGlobal(4096); if (lpConfig == IntPtr.Zero) { throw new OutOfMemoryException( string.Format(StringResources.Error_UpdateServiceConfigOOM, "lpConfig", Marshal.GetLastWin32Error())); } if (NativeMethods.QueryServiceConfig(svcHandle, lpConfig, 4096, out bytesNeeded)) { var config = new NativeMethods.QUERY_SERVICE_CONFIG(); Marshal.PtrToStructure(lpConfig, config); return((ServiceStartupType)config.dwStartType); } else { int error = Marshal.GetLastWin32Error(); if (error == NativeMethods.ERROR_INSUFFICIENT_BUFFER) { Marshal.FreeHGlobal(lpConfig); lpConfig = Marshal.AllocHGlobal(bytesNeeded); if (NativeMethods.QueryServiceConfig(svcHandle, lpConfig, bytesNeeded, out bytesNeeded)) { var config = new NativeMethods.QUERY_SERVICE_CONFIG(); Marshal.PtrToStructure(lpConfig, config); return((ServiceStartupType)config.dwStartType); } else { error = Marshal.GetLastWin32Error(); } } string errorMessage = string.Format(CultureInfo.InvariantCulture, StringResources.Error_UnableToQueryServiceConfiguration, Utility.GetWin32ErrorMessage(error)); DeployerTrace.WriteError(errorMessage); throw new InvalidOperationException(errorMessage); } } catch (Exception e) { DeployerTrace.WriteError(StringResources.Error_UnableToQueryServiceConfiguration, e); throw; } finally { if (svcHandle != IntPtr.Zero) { NativeMethods.CloseServiceHandle(svcHandle); } if (svcManagerHandle != IntPtr.Zero) { NativeMethods.CloseServiceHandle(svcManagerHandle); } if (lpConfig != IntPtr.Zero) { Marshal.FreeHGlobal(lpConfig); } } }