Add Reference..., then clicking Browse and navigating to \"C:\\Windows\\System32" />
开发者

Cannot add "C:\Windows\System32\shdocvw.dll" to my project

开发者 https://www.devze.com 2023-03-16 03:40 出处:网络
I use VS 2010 Ultimate. I\'m trying to add \"shdocvw.dll\" to my project\'s references by right clicking References -> Add Reference..., then clicking Browse and navigating to \"C:\\Windows\\System32

I use VS 2010 Ultimate.

I'm trying to add "shdocvw.dll" to my project's references by right clicking References -> Add Reference..., then clicking Browse and navigating to "C:\Windows\System32\shdocvw.dll", but when I click the Add button nothing happens at all. The dialog doesn't even close.

Any idea what could I be doing wrong?

I tri开发者_开发知识库ed restarting VS but kept having this problem.


In your C# solution, if you add a reference to the COM component named "Microsoft Internet Controls", you should be able to access the SHDocVw namespace from a C# console app, without having to do anything unusual.

Once I did that (in VS 2008) I was then able to use SHDocVw.ShellWindows, SHDocVw.IWebBrowser2, and so forth. For example:

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

foreach (SHDocVw.IWebBrowser2 ie in shellWindows)
{
    Console.WriteLine("ie.LocationURL: " + ie.LocationURL);
    if (ie.LocationURL.Contains("foo.com"))
        ie.Quit();
}

EDIT: When using VS 2012/.NET 4.x, you can use the approach below instead, to work around the error "Interop type 'SHDocVw.ShellWindowsClass' cannot be embedded."

using SHDocVw;
// ... snip ...
            SHDocVw.ShellWindows shellWindows = new ShellWindows();
            foreach (SHDocVw.IWebBrowser2 ie in shellWindows)
            {
                Console.WriteLine("ie.LocationURL: " + ie.LocationURL);
                if (ie.LocationURL.Contains("foo.com"))
                    ie.Quit();

For more information on the VS 2012 issue, see this answer:

C# How to get current URL from the IE?


The problem is that shdocvw.dll is not a .NET assembly, it is both a normal Win32 DLL and an ActiveX control, but not an assembly. The only things you can add as references to a C# project are .NET assemblies.

OK, so that begs the question, why do you want to add a reference to shdocvw.dll to a C# project? Probably because you want to use the ActiveX interface to Internet Explorer to include a web-browser in your application.

If, so, then if you are writing a Windows Forms application, you should use the WinForms WebBrowser control, and if you are writing a WPF application you should use the WPF WebBrowser control. Here are instructions for Windows Forms:

  • How to: Add Web Browser Capabilities to a Windows Forms Application

But if for some reason you want to include the control yourself, instead of using the "canned" version, then you need to add it to Visual Studio Toolbox as described here:

  • How to: Add ActiveX Controls to Windows Forms


It's an unfortunately common request which I have seen before.

You can't add it as a reference. You need to add it as a toolbox item, drop it into a form, and then bob's your uncle...

It's a peculiarity!

Full instructions here : http://www.codeproject.com/KB/miscctrl/WebBrowserEx.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消