/// <summary> /// Allocate a console if application started from within windows GUI. /// Detects the presence of an existing console associated with the application and /// attaches itself to it if available. /// </summary> public static void AllocateConsoleIfNecessary() { if (Debugger.IsAttached) { Available = false; return; // Don't create console under debugger } if (ParentProcessFinder.RunAsService()) { Available = false; return; // Don't create console if running as a service } ForceAllocateConsole(); }
/// <summary> /// Gets the parent process of a specified process. /// </summary> /// <param name="handle">The process handle.</param> /// <returns>An instance of the Process class.</returns> public static Process GetParentProcess(IntPtr handle) { ParentProcessFinder pbi = new ParentProcessFinder(); int returnLength; int status = NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out returnLength); if (status != 0) { throw new Win32Exception(status); } try { return(Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32())); } catch (ArgumentException) { // not found return(null); } }
public static void Init() { // Check for null here is necessary, because thread name can only be set once and unit test runner tends to do so before us // We better not set it here at all to let WorkerRunnerProcess32 to set it. //if (Thread.CurrentThread.Name == null) //{ // Thread.CurrentThread.Name = "Main thread"; //} Tracer.Create(Process.GetCurrentProcess().ProcessName); DebugConsole.AllocateConsoleIfNecessary(); Tracer.Info("Process {0} with ID {1} starts.", Process.GetCurrentProcess().ProcessName, Process.GetCurrentProcess().Id); RunAsService = ParentProcessFinder.RunAsService(); if (RunAsService) { ShimmedService = new ShimmedService { ServiceName = Process.GetCurrentProcess().ProcessName }; } }
private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessFinder processInformation, int processInformationLength, out int returnLength);