2010-07-20 7 views
5

Sto tentando di estrarre l'URL dalla barra degli indirizzi di IE. (IE 8 su Windows 7) utilizzando il seguente codice C#.Accesso negato - quando si tenta di ottenere l'url (testo) dall'handle della barra degli indirizzi

 static string GetUrlFromIE() 
     { 
      IntPtr windowHandle = APIFuncs.getForegroundWindow(); 
      IntPtr childHandle; 
      String strUrlToReturn = ""; 

      //try to get a handle to IE's toolbar container 
      childHandle = APIFuncs.FindWindowEx(windowHandle, IntPtr.Zero, "WorkerW", IntPtr.Zero); 
      if (childHandle != IntPtr.Zero) 
      { 
       //get a handle to address bar 
       childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "ReBarWindow32", IntPtr.Zero); 
       if (childHandle != IntPtr.Zero) 
       { 
        childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "Address Band Root", IntPtr.Zero); 
        if (childHandle != IntPtr.Zero) 
        { 
         childHandle = APIFuncs.FindWindowEx(childHandle, IntPtr.Zero, "Edit", IntPtr.Zero); 
         if (childHandle != IntPtr.Zero) 
         { 
          strUrlToReturn = new string((char)0, 256); 
          GetWindowText(hwnd, strUrlToReturn , strUrlToReturn.Length); 
         } 
        } 
       } 
      } 
      return strUrlToReturn; 
     } 

La chiamata GetWindowText restituisce un'eccezione "Accesso negato". Eseguendo l'app con i privilegi di amministratore, lancia "Un sistema non riesce a trovare il file specificato".

Qualche idea?

risposta

2

GetWindowText() non può recuperare il testo di un controllo in un altro processo, invece si dovrebbe usare SendMessage() con WM_GETTEXTLENGTH/WM_GETTEXT.

Modifica; Versione modo agnostico:

(Aggiungi un ref per c: \ WINDOWS \ system32 \ shdocvw.dll)

using SHDocVw; 
. 
. 
foreach (InternetExplorer ieInst in new ShellWindowsClass()) 
    Console.WriteLine(ieInst.LocationURL); 
+0

Grazie! SHDocVw è un riferimento geniale! Tuttavia, la ragione per cui penso che dovrei attenermi alla metodologia GetText è che ho bisogno di fare la stessa cosa nella maggior parte dei popolari browser per Windows - Chrome, Firefox, Safari, Opera. Il mio piano era di scrivere un metodo personalizzato per ciascun browser per estrarre l'url dal suo handle della barra degli indirizzi. – Sameet

Problemi correlati