title: GetProcessId failed with error 6: The handle is invalid
body: <p>I posted a little earlier with a sendmessage issue and we came to the conclusion that it would be extremely hard to get the chat window from Xchat. I have now moved to ThrashIRC and using spy++ was able to find the chat window(highlighted):</p>
  
  <p><img src="https://i.stack.imgur.com/BVwhq.png" alt="enter image description here"></p>
  
  <p>As you can see it does have a caption which means that it does see the text. Here is the code that I am using to get the HWND's and the text:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;strsafe.h&gt;
  
  using namespace std;
  
  void FindThrash()
  {
      cout &lt;&lt; "[ThrashIRC]" &lt;&lt; endl;
  
  cout &lt;&lt; "| find ThrashIRC window" &lt;&lt; endl;
  HWND hwndThrashIRC = FindWindow(L"ThrashIRC", NULL);
  
  if (NULL != hwndThrashIRC)
  {
      cout &lt;&lt; "   + found ThrashIRC window" &lt;&lt; endl;
      cout &lt;&lt; "| find MDIClient window" &lt;&lt; endl;
      HWND hwndMDIClient = FindWindowEx(hwndThrashIRC, NULL, L"MDIClient", NULL);
  
      if (NULL != hwndMDIClient)
      {
  
          cout &lt;&lt; "   + found MDIClient window" &lt;&lt; endl;
          cout &lt;&lt; "| find DefChannel window" &lt;&lt; endl;
          HWND hwndDefChannel = FindWindowEx(hwndMDIClient, NULL, L"DefChannel", NULL);
  
          if (NULL != hwndDefChannel)
          {
              cout &lt;&lt; "   + found MDIClient window" &lt;&lt; endl;
              cout &lt;&lt; "| find RichEdit20W window" &lt;&lt; endl;
              HWND hwndRichEdit20W = FindWindowEx(hwndDefChannel, NULL, L"RichEdit20W", NULL);
  
              if (NULL != hwndRichEdit20W)
              {
                  cout &lt;&lt; "   + found RichEdit20W window" &lt;&lt; endl &lt;&lt; endl;
                  cout &lt;&lt; "- get text " &lt;&lt; endl;
  
                  const int bufferSize = 32768;
                  char textBuffer[bufferSize] = "";
                  SendMessage(hwndRichEdit20W, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
  
                  cout &lt;&lt; "[begin text]" &lt;&lt; endl;
                  cout &lt;&lt; textBuffer &lt;&lt; endl;
                  cout &lt;&lt; "[end text]" &lt;&lt; endl;
  
              }
              else
              {
                  cerr &lt;&lt; "RichEdit20W not found." &lt;&lt; endl;
              }
  
  
  
  
          }
          else
          {
              cerr &lt;&lt; "DefChannel not found." &lt;&lt; endl;
          }
  
  
  
  
      }
      else
      {
          cerr &lt;&lt; "MDIClient not found." &lt;&lt; endl;
      }
  
  }
  else
  {
      cerr &lt;&lt; "ThrashIRC not open." &lt;&lt; endl;
  }
  }
  
  void ErrorExit(LPTSTR lpszFunction)
  
  {
  
  
  
  // Retrieve the system error message for the last-error code
  
  LPVOID lpMsgBuf;
  LPVOID lpDisplayBuf;
  DWORD dw = GetLastError();
  
  FormatMessage(
      FORMAT_MESSAGE_ALLOCATE_BUFFER |
      FORMAT_MESSAGE_FROM_SYSTEM |
      FORMAT_MESSAGE_IGNORE_INSERTS,
      NULL,
      dw,
      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
      (LPTSTR)&amp;lpMsgBuf,
      0, NULL);
  
  // Display the error message and exit the process
  
  lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
      (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40)*sizeof(TCHAR));
  StringCchPrintf((LPTSTR)lpDisplayBuf,
      LocalSize(lpDisplayBuf) / sizeof(TCHAR),
      TEXT("%s failed with error %d: %s"),
      lpszFunction, dw, lpMsgBuf);
  MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
  
  LocalFree(lpMsgBuf);
  LocalFree(lpDisplayBuf);
  ExitProcess(dw);
  }
  
  int main()
  {
  
      FindThrash();
      if (!GetProcessId(NULL))
          ErrorExit(TEXT("GetProcessId"));
  
      return 0;
  }
  </code></pre>
  
  <p>The buffer is only that large because 1: I thought it might have been an error where the buffer was to small, and 2: the buffer is going to have to be large because of the amount of text. Any suggestions / code would be greatly appreciated! Thanks!</p>