Add reference 'SHDocVw' in C# project usin

2020-02-03 04:41发布

I am following a tutorial which creates a BHO using MS Visual Studio 2010 and C#. In order to run the tutorial code, I have to add this reference in my project:-

using SHDocVw

But it is not available in .NET or COM section in Add References.

So I wanted to ask is this namespace not available in Microsoft Visual C# 2010 Express? and if it is available, how to add it.

This is my halfway project code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IEPlugin
{
 [
    ComVisible(true),
    InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
    Guid("FC4801A3-2BA9-11CF-A229-00AA003D7352")
]
public interface IObjectWithSite
{
    [PreserveSig]
    int SetSite([MarshalAs(UnmanagedType.IUnknown)]object site);
    [PreserveSig]
    int GetSite(ref Guid guid, out IntPtr ppvSite);
}
[
    ComVisible(true),
    Guid("2159CB25-EF9A-54C1-B43C-E30D1A4A8277"),
    ClassInterface(ClassInterfaceType.None)
]

public class BHO : IObjectWithSite
{
    private WebBrowser webBrowser;

    public int SetSite(object site)
    {
        if (site != null)
        {
            webBrowser = (WebBrowser)site;
            webBrowser.DocumentComplete +=
              new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
        }
        else
        {
            webBrowser.DocumentComplete -=
              new DWebBrowserEvents2_DocumentCompleteEventHandler(
              this.OnDocumentComplete);
            webBrowser = null;
        }

        return 0;

    }

    public int GetSite(ref Guid guid, out IntPtr ppvSite)
    {
        IntPtr punk = Marshal.GetIUnknownForObject(webBrowser);
        int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
        Marshal.Release(punk);
        return hr;
    }

    public void OnDocumentComplete(object pDisp, ref object URL)
    {
        HTMLDocument document = (HTMLDocument)webBrowser.Document;
    }

}

}

Here are the errors attached:-

Error 1 The type or namespace name 'SHDocVw' could not be found (are you missing a using directive or an assembly reference?)

Error 2 The type or namespace name 'WebBrowser' could not be found (are you missing a using directive or an assembly reference?)

1条回答
2楼-- · 2020-02-03 04:52

You need to add a reference to a COM component called Microsoft Internet Controls.

enter image description here

查看更多
登录 后发表回答