title: How to detect and get text from win32 popup in .net
body: <p>I've looked through several examples of trying to do this listed on stackoverflow, but i haven't found anything that lists how to do this for .NET coding. Also I need it to detect the popup, and once its found the popup then pull the title of the window, and the text inside the popup.</p>
  
  <p>I tried using the post:
  <a href="https://stackoverflow.com/questions/2668731/how-can-i-change-text-on-a-win32-window">how-can-i-change-text-on-a-win32-window</a></p>
  
  <p>but i had no luck with trying to find how to "get text" only how to see if the popup exists. The window i'm trying to get text from is a standard Windows error window (#32770) a.k.a WIN32 Critical popup window (one with Red X). I know the title of the error window is always going to be "TLP2011"</p>
  
  <p>I just need to be able to read the Title, Text, and then click "OK" on the error button.</p>
  
  <p>Reason? There's a software I run IT for and to cut back on inbound call costs i want to write a program that detects the errors as they popup, then once its been detected close the error window and tell the user that the error will be automatically fixed. (my new program will have to be able to read the text from the error window in order to determine how to automatically fix the error so this error doesn't show up again)</p>
  
  <p>----------Edited to show latest finds------------</p>
  
  <p>Thanks to GX for posting the following link <a href="https://stackoverflow.com/questions/1967604/how-can-i-get-functionality-similar-to-spy-in-my-c-sharp-app">How can I get functionality similar to Spy++ in my C# app?</a></p>
  
  <p>Now i have determined that i need something like the following:</p>
  
  <pre><code>Dim pProcess() As Process = Process.GetProcessesByName("TLP2011")
  
      For Each p As Process In pProcess
          For Each threadInfo As ProcessThread In p.Threads
  
              ' uncomment to dump thread handles
              'Console.WriteLine("\tthread {0:x}", threadInfo.Id);
              Dim windows As IntPtr() = GetWindowHandlesForThread(threadInfo.Id)
              If windows IsNot Nothing AndAlso windows.Length &gt; 0 Then
                  For Each hWnd As IntPtr In windows
                      Console.WriteLine(vbTab &amp; "window {0:x} text:{1} caption:{2}", hWnd.ToInt32(), GetText(hWnd), GetEditText(hWnd))
                  Next
              End If
          Next
      Next
  </code></pre>
  
  <p>Now this lists everything on the actual program itself. Every piece of text and everything. Anyone know a trick to just single out the popup box?</p>
  
  <p>Here is an example of what displays</p>
  
  <pre><code>window 3d21aa text:Select Package caption:
  window 551176 text: caption:
  window e8147c text:Do Not Auto Show  caption:Do Not Auto Show 
  window 8e21c6 text:&amp;Select All caption:&amp;Select All
  window 2300e10 text:&amp;Help caption:&amp;Help
  window 762164 text:Cancel caption:Cancel
  window 330f0e text:OK caption:OK
  window e8147c text:Do Not Auto Show  caption:Do Not Auto Show 
  window 8e21c6 text:&amp;Select All caption:&amp;Select All
  window 2300e10 text:&amp;Help caption:&amp;Help
  window 762164 text:Cancel caption:Cancel
  window 330f0e text:OK caption:OK
  window 10f20a6 text: caption:
  window 5f122ce text: caption:
  window 1824e2 text: caption:
  window 1824e2 text: caption:
  window 5f122ce text: caption:
  window 1824e2 text: caption:
  window 1824e2 text: caption:
  window 6e1be4 text: caption:
  window 342162 text: caption:
  window 1c1b92 text: caption:
  window 65105a text: caption:Admin
  window 291f72 text: caption:
  window 375104e text: caption:
  window 375104e text: caption:
  window 1c1b92 text: caption:
  window 65105a text: caption:Admin
  window 291f72 text: caption:
  window 375104e text: caption:
  window 375104e text: caption:
  window 242308 text:Form Filter caption:
  window 9ee294e text: caption:
  window 8ff0f54 text: caption:All Status
  window 468273c text: caption:
  window 1d110c text: caption:
  window 1d110c text: caption:
  window 8ff0f54 text: caption:All Status
  window 468273c text: caption:
  window 1d110c text: caption:
  window 1d110c text: caption:
  window b90bea text:cbEngine caption:
  window 5b17b0 text: caption:
  window ee24c6 text: caption:
  window ee24c6 text: caption:
  window fc1ae0 text: caption:
  window 141cfc text:TLP2011 caption:
  window d51928 text:OK caption:OK
  window 2c1223c text: caption:
  window 100f08 text:Access violation at address 6CABC667 in module 'tlpcore.dll'. Write of address F8FFFF86. caption:Access violation at address 6CABC667 in module 'tlpcore.dll'. Write of address F8FFFF86.
  window b2a214e text: caption:
  </code></pre>
  
  <p>Anyone know a better way to try and find the error box?  I don't think that doing a search for "TLP2011" then using the next 3 lines to try and determine the msgbox text to be a very "reliable" method. Anyone know how to single it out more?</p>
  
  <p>----Added more thoughts----</p>
  
  <p>Doing a check for this through all of the different threads every like 10 seconds seems like it would put a huge drain on the system. Is this really the most logical way to go about doing this?</p>