title: Attach console to process
body: <p>I want to attach a console to an monogame application. I used .Net Framework at first and everything worked fine, then I switched to .Net Core 3.1 and the console stopped attaching to the process.</p>
  <pre><code>[DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]
  private static extern bool AttachConsole(int dwProcessId);
  
  [DllImportAttribute(&quot;kernel32.dll&quot;, SetLastError = true, EntryPoint = &quot;AllocConsole&quot;)]
  [return: MarshalAsAttribute(UnmanagedType.Bool)]
  private static extern bool AllocConsole();
  
  [DllImportAttribute(&quot;kernel32.dll&quot;, SetLastError = true, EntryPoint = &quot;GetStdHandle&quot;)]
  private static extern IntPtr GetStdHandle(Int32 nStdHandle);
  
  [DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]
  private static extern bool FreeConsole();
  
  [DllImport(&quot;kernel32.dll&quot;, EntryPoint = &quot;GetLastError&quot;)]
  private static extern int GetLastError();
  
  private void InitializeConsole() {
      if (AttachConsole(Process.GetCurrentProcess().Id) &amp;&amp; AllocConsole()) {
          GetStdHandle(Process.GetCurrentProcess().Id);
          _isInitializeConsole = true;
      } else {
          _isInitializeConsole = false;
          throw new Exception($&quot;Console alloc error code: {GetLastError()}&quot;);
      }
  }
  </code></pre>
  <p>When trying to attach a console from the AttachConsole method, I get result &quot;false&quot; and last error code - 5 (Access is denied). I ran the application as administrator and getting error code - 6 (The handle is invalid).</p>
  <p>Is there a way to fix this error?</p>