title: How can I output text to another console already open C++
body: <p>Hi this is my first question on stack overflow
  (Im a junior programmer :P and french too...So apologies in advance for gramatical mistake I make)</p>
  
  <p>Im trying to start a elevated process to attach back to the console of the parent to write its output</p>
  
  <p>(no crash no error just plain nothingness)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
  
      HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
  
      if (UAC::IsAppRunningAsAdminMode())
      {
          printf("Process Already elevated\nChecking if self invocated from unprevileged previous run...\n");
           if (argc &gt; 1)
          {
              std::string consoleTextOutputBuffer("Elevated privileges session started...\n");
               WriteConsoleA((HANDLE)argv[2], consoleTextOutputBuffer.c_str(), consoleTextOutputBuffer.size(), NULL, NULL);
          }
      }
      else
      {
          printf("Process need elevation...\n");
          if (UAC::BeginPrivilegeElevationPrompt(consoleHandle))
          {
              printf("Elevation succesfull!\n");
          }
          else
          {
              printf("Elevation failed\n");
              system("pause&gt;nul");
              exit(-1);
          }
      }
  }
  </code></pre>
  
  <p>And From the class UAC that I wrote:</p>
  
  <pre><code>BOOL BeginPrivilegeElevationPrompt(const HANDLE&amp; oldConsoleHandle)
  {
      wchar_t szPath[MAX_PATH];
      if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath)))
      {
          // Launch itself as admin
          std::string oldConsoleHandleToString = std::to_string((int)oldConsoleHandle);
          std::wstring wsConsoleString(oldConsoleHandleToString.begin(), oldConsoleHandleToString.end());
          SHELLEXECUTEINFO sei = { sizeof(sei) };
          sei.lpVerb = L"runas";
          sei.lpFile = szPath;
          sei.hwnd = NULL;
          sei.lpParameters = wsConsoleString.c_str();
          sei.nShow = SW_NORMAL;
          if (!ShellExecuteEx(&amp;sei))
          {
              DWORD dwError = GetLastError();
              if (dwError == ERROR_CANCELLED)
              {
                  // The user refused to allow privileges elevation.
                  printf("User did not allow elevation.\n");
                  return false;
              }
              return false;
          }
          else
          {
              return true;
              _exit(1);  // Quit itself
          }
      }
      printf("Could not load module name.\n");
      return false;
  };
  </code></pre>