7

I want to get the current URL from the IE (.NET 4) . To do so, I added a reference to Microsoft Interner Controls and added the code (from http://omegacoder.com/?p=63)

foreach (InternetExplorer ie in new ShellWindowsClass())
{
   textBox1.Text = ie.LocationURL.ToString();
}

but I get 2 errors:

1] The type 'SHDocVw.ShellWindowsClass' has no constructors defined

2] Interop type 'SHDocVw.ShellWindowsClass' cannot be embedded.
   Use the applicable interface instead.

How to solve that ?

Tony
  • 12,405
  • 36
  • 126
  • 226
  • if you were using this control http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.aspx you could get it from the URL property. – jcolebrand Nov 13 '10 at 18:12
  • No, my app will get the URL from IE wchich is the other process – Tony Nov 13 '10 at 18:17
  • @tony ~ wait what? Can you add some explanation on that to the question? – jcolebrand Nov 13 '10 at 18:24
  • I run IE and my app. Then, in my app, if I click on some button, I'll see the current URL from the IE – Tony Nov 13 '10 at 18:25
  • So let me get this straight, the user starts IE, then they start your program, and you want to get the value from the URL address bar of IE for the currently loaded tab for use in your program? – jcolebrand Nov 13 '10 at 18:28
  • Yes, many tabs can be runned in IE, and I need to get the current tab's URL – Tony Nov 13 '10 at 18:34

1 Answers1

17

The 2nd error causes the first one. Open the project's References node, select SHDocVw. In the Properties window, change "Embed Interop Types" to false. You will have to deploy the Interop.SHDocVw.dll assembly you'll find the build output directory along with your program.

EDIT: after researching this error, I found a better way to do this. The issue is that only COM interface types can be embedded, not classes. So avoid using the synthetic XxxxClass wrappers in your code. Make it look like this instead:

        foreach (InternetExplorer ie in new ShellWindows()) {
            //...
        }

Which looks strange, you cannot normally use the new operator on an interface type in the C# language. But is actually supported for COM interfaces.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    it works, thanks ! :) But, I see URLs from all tabs, but how to het URL from the current tab ? – Tony Nov 13 '10 at 18:33
  • Figure out which tab is selected :) – Richard J. Ross III Nov 13 '10 at 18:48
  • I get a `Error 1 The type or namespace name 'ShellWindowsClass' could not be found (are you missing a using directive or an assembly reference?) c:\users\usrs\documents\visual studio 2012\Projects\CrawlerWeb SLN\CrawlerWeb\frmMain.cs 58 49 CrawlerWeb` How do I fix it? – Si8 Jun 03 '14 at 14:30