Click here to Skip to main content
14,934,103 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
[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
Posted
Updated 19-Jul-18 1:39am
Comments
KarstenK 19-Jul-18 8:00am
   
Jochens answer is correct, but it is a difficult task even for a seasonded COM-programmer. So be careful and you will need a lot of time!!!

Tip: for better debugging you should disable some security settings.

1 solution

The interface class:
IObjectWithSiteImpl Class | Microsoft Docs[^]

This contains code showing how to implement the interface and event sinks:
Writing a BHO in Plain C++[^]

Quote:
Also I want to reference shdocvw.dll and mshtml.dll in C++ Project
Just add them to the project settings (Linker - Input - Additional Dependencies) or specify them in one source file using comment (C/C++) | Microsoft Docs[^] :
C++
#pragma comment(lib, "shdocvw.lib")
#pragma comment(lib, "mshtml.lib")
   
Comments
Aashish Chavan 19-Jul-18 9:26am
   
public void OnDocumentComplete(object pDisp, ref object URL)
{
this.document = (HTMLDocument)this.webBrowser.Document;
Marshal.ReleaseComObject(this.document);
}

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;
}

Please help me out with this code in C++
Jochen Arndt 19-Jul-18 9:35am
   
It can't be directly translated to C++.

You have to implement the interface and the event sinks as described in the CP article from my solution.

The article provides the implementation. So you are lucky and have not to do it yourself because - as KarstenK noted already in his comment - implementing such COM interfaces and sinks is an advanced and difficult task.

However, if you need to modify the article code to match your needs (which I guess is necessary) you should have read about COM interfaces with C++ / MFC before and understand at least the basics.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900