title: (C/C++, Windows) Issues with AttachConsole and FreeConsole only working once
body: <p>I'm trying to send a ctrl+C signal to the console of another process when a button is clicked, by attaching the current process to its console, sending a ctrl+C signal, then freeing the current process from a console. This works fine the first time, but doesn't do anything the second time.</p>
  
  <pre><code>void abort(){
      AttachConsole(processInfo.dwProcessId); //processInfo is of type PROCESS_INFORMATION
      SetConsoleCtrlHandler(0, true);
      GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0);
      FreeConsole();
  }
  </code></pre>
  
  <p>Also, even manually clicking in the console and pressing ctrl+C works the first time but not the second. Closing the console manually always works.</p>
  
  <p>process.exe is a child process, and is created in exactly the same way as in <a href="https://stackoverflow.com/a/10888698/2805328">this post</a>.</p>
  
  <hr>
  
  <p>Full code to recreate the problem (using Qt 4.8 with vs2010 compiler on windows 10 for gui/threading):</p>
  
  <p>main.cpp:</p>
  
  <pre><code>#include "dialog.h"
  #include &lt;QApplication&gt;
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      Dialog w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>dialog.h:</p>
  
  <pre><code>#ifndef DIALOG_H
  #define DIALOG_H
  
  #include &lt;QDialog&gt;
  #include "ui_dialog.h"
  #include "worker.h"
  
  namespace Ui {
  class Dialog;
  }
  
  class Dialog : public QDialog
  {
      Q_OBJECT
  
  public:
      explicit Dialog(QWidget *parent = 0):
          QDialog(parent),
          ui(new Ui::Dialog),
          w(new worker()),
          t(new QThread(this))
      {
          ui-&gt;setupUi(this);
          connect(ui-&gt;startButton, SIGNAL(clicked(bool)), this, SLOT(startProcess()));
          connect(ui-&gt;abortButton, SIGNAL(clicked(bool)), this, SLOT(abortProcess()));
          connect(w, SIGNAL(finished()), t, SLOT(quit()));
  
          w-&gt;setup(t);
          w-&gt;moveToThread(t);
      }
  
      ~Dialog(){delete ui;}
  
  private slots:
      void startProcess(){
          t-&gt;start();
      }
  
      void abortProcess(){
          w-&gt;abort();
          t-&gt;quit();
      }
  
  private:
      Ui::Dialog *ui;
      worker *w;
      QThread *t;
  
  };
  
  #endif // DIALOG_H
  </code></pre>
  
  <p>worker.h:</p>
  
  <pre><code>#ifndef WORKER_H
  #define WORKER_H
  
  #include &lt;QDebug&gt;
  #include &lt;QObject&gt;
  #include &lt;QThread&gt;
  #include &lt;Windows.h&gt;
  
  class worker : public QObject
  {
      Q_OBJECT
  public:
      explicit worker(QObject *parent = 0):QObject(parent){}
      void setup(QThread *t){
          connect(t, SIGNAL(started()), this, SLOT(startProcess()));
      }
      void abort(){
          if(!AttachConsole(p.dwProcessId)) qDebug() &lt;&lt; "AttachConsole" &lt;&lt; GetLastError();
          if(!SetConsoleCtrlHandler(0, true)) qDebug() &lt;&lt; "SetConsoleCtrlHandler" &lt;&lt; GetLastError();
          if(!GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0)) qDebug() &lt;&lt; "GenerateConsoleCtrlEvent" &lt;&lt; GetLastError();
          if(!FreeConsole()) qDebug() &lt;&lt; "FreeConsole" &lt;&lt; GetLastError();
      }
  
  private slots:
      void startProcess(){
          ZeroMemory(&amp;p, sizeof(p));
          STARTUPINFOA s;
          SECURITY_ATTRIBUTES sec;
          HANDLE read = NULL;
          HANDLE write = NULL;
  
          ZeroMemory(&amp;sec, sizeof(sec));
          sec.nLength = sizeof(SECURITY_ATTRIBUTES);
          sec.bInheritHandle = true;
          sec.lpSecurityDescriptor = NULL;
  
          if(!CreatePipe(&amp;read, &amp;write, &amp;sec, 0)) qDebug() &lt;&lt; "CreatePipe error" &lt;&lt; GetLastError();
          if(!SetHandleInformation(read, HANDLE_FLAG_INHERIT, 0)) qDebug() &lt;&lt; "SetHandleInformation error" &lt;&lt; GetLastError();
  
          ZeroMemory(&amp;s, sizeof(s));
          s.cb = sizeof(s);
          s.hStdOutput = write;
          s.hStdError = write;
          s.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
          s.dwFlags |= STARTF_USESTDHANDLES;
  
          if(!CreateProcessA("helloworld.exe", "helloworld", NULL, NULL, true, CREATE_NO_WINDOW, NULL, NULL, &amp;s, &amp;p)) qDebug() &lt;&lt; "CreateProcessA error " &lt;&lt; GetLastError() &lt;&lt; "\n";
  
          CloseHandle(write);
  
          char buff[65];
          DWORD bytesRead;
  
          while(true){
              if(!ReadFile(read, buff, 64, &amp;bytesRead, NULL)){
                  qDebug() &lt;&lt; "ReadFile error";
                  break;
              }
              if(bytesRead &gt; 0){
                  buff[64] = '\0';
                  qDebug() &lt;&lt; QByteArray(buff);
              }
          }
  
          CloseHandle(p.hProcess);
          CloseHandle(p.hThread);
  
          emit finished();
      }
  
  private:
      PROCESS_INFORMATION p;
  
  signals:
      void finished();
  
  };
  
  #endif // WORKER_H
  </code></pre>
  
  <p>The UI just contains 2 buttons, startButton and abortButton. helloworld.exe is just a program that prints "Hello world!" and then waits for input. When startButton is clicked, the process is created, and the output is redirected to my program correctly. When abortButton is clicked, the process is terminated correctly. startProcess works perfectly the second time as well, but now, clicking abortButton does not work. The abort() function is called, and no error messages are printed, but the helloworld process fails to terminate, and the QThread t does not quit.</p>