2
private string GetCurrentChromeUrl()
    {
        try
        {
            string url = null;

            int handle = GetForegroundWindow();
            AutomationElement elm = AutomationElement.FromHandle((IntPtr)handle);
            AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
                  new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
            if (elmUrlBar != null)
            {
                AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
                if (patterns.Length > 0)
                {
                    ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                    url = val.Current.Value;
                    //break;
                }
            }
            return url;
        }
        catch (Exception e1)
        {
            return "";
        }
   }

I'm trying to find the URL from google chrome browser.

And I'm using above code. It is work good in other application but in my case it stops my application.

But the main issue is that it will work fine when I'm debugging it, so what wrong when no debugging.

please give your solutions.

thanx in advance

  • Any more information you can share? Also just guessing but is it perhaps a permission issue that doesn't show up due to debug settings? – EWit Sep 06 '14 at 09:58
  • ok I'm giving some aditional information – Bharat Prajapati Sep 06 '14 at 10:08
  • I'm using this code in void m_KeyboardHookManager_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {} when I just press one key my whole system goes hang and if i point the debug point at AutomationElement elm = AutomationElement.FromHandle((IntPtr)handle); or before it then it works fine.but doesn't work after pointing debug point after that – Bharat Prajapati Sep 06 '14 at 10:12
  • hello sir now you can see my full code. – Bharat Prajapati Sep 06 '14 at 10:16
  • Does the `catch` statement catch any exception? And if so, what exception? – EWit Sep 06 '14 at 10:23
  • No sir there is no any exception. – Bharat Prajapati Sep 06 '14 at 10:29
  • there is no any problem if I debug this code. – Bharat Prajapati Sep 06 '14 at 10:30
  • i have similar code - in chrome it is patterns[0] (total - 1), in opera - patterns[1] (total - 2) so i would put not `elmUrlBar.GetCurrentPattern(patterns[0]);` but `((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[patterns.Length - 1])).Current.Value;` – Sasha Bond Dec 29 '22 at 21:29

1 Answers1

1

I'm passing through this, it happen cause of the intptr type, it depends of the build of the machine, sometime the windowhandle is too big for an intptr of a 32bits machine.