title: How do I redirect output for an already running process in c#
body: <p>I'm launching a new process from a service (Administrative privileges) in C# by using a custom DLL written in c++ that basically modifies the security and privileges to reduce it.
  This is because the new process when run through Ring 0 of Windows 7 does not allow it to access the hardware encoder in OpenCL (Windows driver design limitation). So I impersonate the Windows Explorer privileges and launch it using this DLL.</p>
  
  <p>The crux of the DLL file is:</p>
  
  <pre><code>                                typedef BOOL (WINAPI *LPFN_CreateProcessWithTokenW)(
                                  HANDLE hToken,
                                  DWORD dwLogonFlags,
                                  LPCWSTR lpApplicationName,
                                  LPWSTR lpCommandLine,
                                  DWORD dwCreationFlags,
                                  LPVOID lpEnvironment,
                                  LPCWSTR lpCurrentDirectory,
                                  LPSTARTUPINFOW lpStartupInfo,
                                  LPPROCESS_INFORMATION lpProcessInfo
                                  );
                              LPFN_CreateProcessWithTokenW fnCreateProcessWithTokenW=NULL;
                              HINSTANCE hmodAdvApi32=LoadLibraryA("AdvApi32");
                              if(hmodAdvApi32)
                                  fnCreateProcessWithTokenW=(LPFN_CreateProcessWithTokenW)GetProcAddress(hmodAdvApi32, "CreateProcessWithTokenW");
                              if(fnCreateProcessWithTokenW)
                              {
                                  bRet=fnCreateProcessWithTokenW(hNewToken, 0, 
                                      szProcessName, szCmdLine, 
                                      0, NULL, NULL, &amp;StartupInfo, &amp;ProcInfo);
                                  if(!bRet)
                                      hr=HRESULT_FROM_WIN32(GetLastError());
                              }
  </code></pre>
  
  <p>Now the question is how do I redirect the output from this process to my C# program to monitor the output? This is critical since I need to know how the process is progressing or hanging so the C# app can take appropriate actions.
  So can I:</p>
  
  <ol>
  <li>Redirect/capture output from an existing process using a process id?</li>
  <li>How can I pass a callback function from c# to a dll to register that output handler with the above function so I can capture the output from the c# app.</li>
  </ol>