[ClassInterface(ClassInterfaceType.None), ComVisible(true), Guid("8a194578-81ea-4850-9911-13ba2d71efbd")]
public class BrowserHelperObject : IObjectWithSite
{
private WebBrowser webBrowser;
private HTMLDocument document;
public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";
public void OnDocumentComplete(object pDisp, ref object URL)
{
this.document = (HTMLDocument)this.webBrowser.Document;
Marshal.ReleaseComObject(this.document);
}
[ComRegisterFunction]
public static void RegisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BrowserHelperObject.BHOKEYNAME, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
if (registryKey == null)
{
registryKey = Registry.LocalMachine.CreateSubKey(BrowserHelperObject.BHOKEYNAME);
}
string guid = type.GUID.ToString("B");
RegistryKey ourKey = registryKey.OpenSubKey(guid, true);
if (ourKey == null)
{
ourKey = registryKey.CreateSubKey(guid);
}
ourKey.SetValue("IEAddinApplied", 1, RegistryValueKind.DWord);
registryKey.Close();
ourKey.Close();
}
[ComUnregisterFunction]
public static void UnregisterBHO(Type type)
{
RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BrowserHelperObject.BHOKEYNAME, RegistryKeyPermissionCheck.ReadWriteSubTree, System.Security.AccessControl.RegistryRights.FullControl);
string guid = type.GUID.ToString("B");
if (registryKey != null)
{
registryKey.DeleteSubKey(guid, false);
}
}
public int SetSite(object site)
{
if (site != null)
{
this.webBrowser = (WebBrowser)site;
this.webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
this.webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
Marshal.ReleaseComObject(this.document);
this.webBrowser = null;
}
return 0;
}
public int GetSite(ref Guid guid, out IntPtr ppvSite)
{
IntPtr punk = Marshal.GetIUnknownForObject(this.webBrowser);
int hr = Marshal.QueryInterface(punk, ref guid, out ppvSite);
Marshal.Release(punk);
return hr;
}
What I have tried:
The problem I'm facing is with
OnDocumentComplete
Method and with this part of code
if (site != null)
{
this.webBrowser = (WebBrowser)site;
this.webBrowser.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
}
else
{
this.webBrowser.DocumentComplete -= new DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);
Marshal.ReleaseComObject(this.document);
this.webBrowser = null;
}
Also I want to reference shdocvw.dll and mshtml.dll in C++ Project