Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to grab parent process standard output?
    primarykey
    data
    text
    <p>I'm writing an utility (<a href="http://reg2run.sf.net" rel="nofollow noreferrer">http://reg2run.sf.net</a>) which in case execution without arguments works as windows application (shows OpenFileDialog, etc), otherwise - as console application.</p> <p>So, in first case I don't want to show a console window, that's why project is Windows Application. But in second - I need to show it, and it's created with </p> <pre><code>if (ptrNew == IntPtr.Zero) { ptrNew = GetStdHandle(-11); } if (!AllocConsole()) { throw new ExternalCallException("AllocConsole"); } ptrNew = CreateFile("CONOUT$", 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); if (!SetStdHandle(-11, ptrNew)) { throw new ExternalCallException("SetStdHandle"); } StreamWriter newOut = new StreamWriter(Console.OpenStandardOutput()); newOut.AutoFlush = true; Console.SetOut(newOut); Console.SetError(newOut); </code></pre> <p>And what I want - is to grab parent process standard output and use it, if it exists (in case execution via cmd.exe or Far Manager). How can I do it?</p> <p>I tried</p> <pre><code>static Process GetParentProc() { int pidParent = 0; int pidCurrent = Process.GetCurrentProcess().Id; IntPtr hSnapshot = CreateToolhelp32Snapshot(2, 0); if (hSnapshot == IntPtr.Zero) { return null; } PROCESSENTRY32 oProcInfo = new PROCESSENTRY32(); oProcInfo.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32)); if (!Process32First(hSnapshot, ref oProcInfo)) { return null; } do { if (pidCurrent == oProcInfo.th32ProcessID) { pidParent = (int)oProcInfo.th32ParentProcessID; } } while (pidParent == 0 &amp;&amp; Process32Next(hSnapshot, ref oProcInfo)); if (pidParent &gt; 0) { return Process.GetProcessById(pidParent); } else { return null; } </code></pre> <p>and</p> <pre><code>StreamWriter newOut = GetParentProc().StandardInput; </code></pre> <p>but got InvalidOperationException: StandardIn has not been redirected. Because of</p> <pre><code>GetParentProc().StartInfo.RedirectStandardOutput = false </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload