title: Junk characters coming in from pinvoke WM_GETTEXT
body: <p>I have a method set that uses pinvoke to call WM_GETTEXT on another program's textbox - and it works fairly well, but frequently I just get back total junk text appended to the end of it. (The ORIGINAL text is always intact.)</p>
  
  <p>This is random, I cannot reproduce it on demand, but it is frequent enough to be stopship.</p>
  
  <p>Here is the text to get the information.</p>
  
  <pre><code>System.Text.StringBuilder strBuffer = new System.Text.StringBuilder();
  
  int nLen = 0;
  
  bool nUpdated = false;
  
  try
  {
      this.isOpen = false;
  
      if (ptrHandle == null)
          return;
  
      if (ptrHandle == IntPtr.Zero)
          return;
  
      nLen =
          Converter.SendMessage(ptrHandle, Converter.WM_GETTEXTLENGTH, 0, 0);
  
      if (nLen &lt;= 0)
          return;
  
      if (nPreviousLen != nLen)
          nUpdated = true;
  
      if (nUpdated)
      {
          System.Diagnostics.Debug.WriteLine("nLen:\t{0}", nLen);
  
          strBuffer = new System.Text.StringBuilder(null, nLen + 1);
  
          System.Diagnostics.Debug.WriteLine("strBuffer:\t{0}", strBuffer.ToString());
  
          int sLen = Converter.SendMessageByString(ptrHandle, Converter.WM_GETTEXT, nLen
              , strBuffer);
  
          System.Diagnostics.Debug.WriteLine("sLen:\t{0}", sLen);
  
          System.Diagnostics.Debug.WriteLine("\n\nstrBuffern\n\n{0}", strBuffer.ToString());
  
          strBuffer = new System.Text.StringBuilder(strBuffer.ToString().Left(sLen));
  
          System.Diagnostics.Debug.WriteLine("\n\nsLenBuffer\n\n{0}", strBuffer.ToString());
  
  source = new Special.IO.TextReader( 
                      new System.IO.MemoryStream(  System.Text.Encoding.Default.GetBytes(strBuffer.ToString() ) ), nUpdated );
          }
      }
  }
  
  
      /// &lt;summary&gt;
      /// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
      /// &lt;br /&gt;
      /// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
      /// &lt;/summary&gt;
      /// &lt;param name="hWnd"&gt;
      /// Handle to the window whose window procedure will receive the message. 
      /// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
      /// &lt;/param&gt;
      /// &lt;param name="Msg"&gt;
      /// [in] Specifies the message to be sent.
      /// &lt;/param&gt;
      /// &lt;param name="wParam"&gt;
      /// [in] Specifies additional message-specific information.
      /// &lt;/param&gt;
      /// &lt;param name="lParam"&gt;
      /// [in] Specifies additional message-specific information.
      /// &lt;/param&gt;
      /// &lt;returns&gt;
      /// The return value specifies the result of the message processing; it depends on the message sent.
      /// &lt;/returns&gt;
      [DllImport("user32.dll", EntryPoint = "SendMessageA", CharSet = CharSet.Ansi, SetLastError = false)]
      internal static extern int SendMessageByString(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
  
      /// &lt;summary&gt;
      /// Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.
      /// &lt;br /&gt;
      /// To send a message and return immediately, use the SendMessageCallback or SendNotifyMessage function. To post a message to a thread's message queue and return immediately, use the PostMessage or PostThreadMessage function.
      /// &lt;/summary&gt;
      /// &lt;param name="hWnd"&gt;
      /// Handle to the window whose window procedure will receive the message. 
      /// If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system, including disabled or invisible unowned windows, overlapped windows, and pop-up windows; but the message is not sent to child windows.
      /// &lt;/param&gt;
      /// &lt;param name="Msg"&gt;
      /// [in] Specifies the message to be sent.
      /// &lt;/param&gt;
      /// &lt;param name="wParam"&gt;
      /// [in] Specifies additional message-specific information.
      /// &lt;/param&gt;
      /// &lt;param name="lParam"&gt;
      /// [in] Specifies additional message-specific information.
      /// &lt;/param&gt;
      /// &lt;returns&gt;
      /// The return value specifies the result of the message processing; it depends on the message sent.
      /// &lt;/returns&gt;
      [DllImport("user32.dll", CharSet = CharSet.Auto)]
      internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
  </code></pre>