3

I'm writing a small application where I wish to get the URL from the Chrome browse.

In order to first check if the Chrome browser is open or not I use the following code:

 IntPtr WindowTitleTextPtr = GetForegroundWindow();

 StringBuilder WindowTitleText = new StringBuilder();

 GetWindowText(WindowTitleTextPtr, WindowTitleText, 256); // Problem

 ...

I'm using GetWindowText() function to get the Windows title text, but I'm facing a problem there.

If the Chrome window has NO URL and is simply a New Tab then I have no issues, WindowTitleText.ToString() is equal to New Tab - Google Chrome.

However if I open a webpage, in which case the URL is filled with some URL then at the line GetWindowText() I get: vs32host.exe has stopped working message window asking for me to enter image description here

What's going on?

Help!

HaggarTheHorrible
  • 7,083
  • 20
  • 70
  • 81
  • It might help if you enclose the `GetWindowText()` in a [try/catch](http://csharp-station.com/Tutorial/CSharp/Lesson15), and see exactly what exception is causing this error... assuming it's an exception, rather than some external thing - just posting this as a way to possibly find answers on your own, or at least come closer to finding one. – mcmonkey4eva Jun 29 '13 at 06:36
  • @mcmonkey4eva: Sorry, forgot to mention that in my question. I have tried that as well, I enclosed GetWindowText() inside a try block but the control doesn't enter into catch block at all :-( – HaggarTheHorrible Jun 29 '13 at 06:41
  • that's interesting... I very much doubt it but does the 'Debug' button do anything useful? – mcmonkey4eva Jun 29 '13 at 06:42
  • I tried even that, it tries to open just in time debugger but it fails to do so(Throws up some other error message saying - unable to debug). – HaggarTheHorrible Jun 29 '13 at 06:46

1 Answers1

5

You should allocate memory within the StringBuilder instance:

  StringBuilder WindowTitleText = new StringBuilder();

  int size = 256;
  WindowTitleText.Length = size; // <- Memory allocation

  // Read text into allocated memory
  GetWindowText(WindowTitleTextPtr, WindowTitleText, WindowTitleText.Length); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I had used code which looked basically the same like HaggarTheHorrible's in a VS2010 project and it worked perfectly. I don't know how it stopped working in VS2012. Nonetheless, why does this work? Isn't memory allocated, like, dynamically? This strikes me as a real oddity. – TotoTitus Nov 06 '13 at 14:05
  • 1
    @TotoTitus: Access Violation errors are usually unstable ones. When you don't allocate memory, StringBuilder's internal buffer pointer could allocate small memory amount, say 16 byte, by default (in order not to do re-allocatation but confirmation); it may happen that writing data beyond internal buffer borders doesn't spoil data in your very environment, when other users aren't so happy. – Dmitry Bychenko Nov 07 '13 at 09:44
  • Bizarre things happen when venturing past the confines of managed code. Thanks, your solution helped me tremendously. – TotoTitus Nov 07 '13 at 13:06