Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8347833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:29:40+00:00 2026-06-09T07:29:40+00:00

this is a totally newbie question since my field is Java and not C#

  • 0

this is a totally newbie question since my field is Java and not C# but I got given the task of building a BHO and have reached a point where I need a menu item (inside the tools menu) to call a function within my code.

I read that I have to declare and implement the IOleCommandTarget interface, which I did. Now I need to register this same class as a COM Object.

Apparently to do that I need to declare a couple of functions with [ComRegisterFunction] but even after doing so I cannot register them with regsvr32. And uppon further inspection with DLL Export viewer the functions do not seem to be there 🙁

What am I doing wrong?

Thank you in advance

EDIT:

Ok so from the comments I got I suppose that My COM object is registered correctly but still my problem remains: When I click the newly created menu item I have no reaction or way to debug what’s really going on.

This is my code so far:

using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHDocVw;
using MSHTML;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing;
using System.Diagnostics;
using System.Threading;


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct OLECMDTEXT {
    public uint cmdtextf;
    public uint cwActual;
    public uint cwBuf;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
    public char rgwz;
}

[StructLayout(LayoutKind.Sequential)]
public struct OLECMD {
    public uint cmdID;
    public uint cmdf;
}

// Interop definition for IOleCommandTarget. 
[ComImport,
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleCommandTarget {
    //IMPORTANT: The order of the methods is critical here. You
    //perform early binding in most cases, so the order of the methods
    //here MUST match the order of their vtable layout (which is determined
    //by their layout in IDL). The interop calls key off the vtable ordering,
    //not the symbolic names. Therefore, if you switched these method declarations
    //and tried to call the Exec method on an IOleCommandTarget interface from your
    //application, it would translate into a call to the QueryStatus method instead.
    void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] OLECMD[] prgCmds, ref OLECMDTEXT CmdText);
    void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, ref object pvaIn, ref object pvaOut);
}

namespace ANAISBHO {

    [
    ComVisible(true),
    Guid("8a194578-81ea-4850-9911-13ba2d71efbd"),
    ClassInterface(ClassInterfaceType.None)
    ]
    public class BHO : IObjectWithSite, IOleCommandTarget {

        public SHDocVw.WebBrowser webBrowser;

        public HTMLDocument document;

        public static string BHOKEYNAME = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Browser Helper Objects";

        private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");

        private static Bitmap bmpScreenshot;

        private static Graphics gfxScreenshot;


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

        //HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Extensions\{GUID}\CLSID
        private static void RegisterCiteNPLMenuEntry() {
            Console.WriteLine("Registrying the CiteNPL menu entry");
            RegistryKey MicrosoftKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default).OpenSubKey("Software").OpenSubKey("Microsoft");
            RegistryKey ExtensionsKey = MicrosoftKey.OpenSubKey("Internet Explorer").OpenSubKey("Extensions", RegistryKeyPermissionCheck.ReadWriteSubTree);
            String GUID = @"{C3E1CD69-3928-4b69-ABEC-936CA3A3BAAA}";

            ExtensionsKey.CreateSubKey(GUID);
            RegistryKey GUIDKey = ExtensionsKey.OpenSubKey(GUID, RegistryKeyPermissionCheck.ReadWriteSubTree);
            String CLSID_Shell_ToolbarExtExec = @"{1FBA04EE-3024-11d2-8F1F-0000F87ABD16}";
            GUIDKey.SetValue("CLSID", CLSID_Shell_ToolbarExtExec, RegistryValueKind.String);
            GUIDKey.SetValue("MenuText", "CiteNPL", RegistryValueKind.String);
            GUIDKey.SetValue("ClsidExtension", "{c4127d56-7114-46da-a853-6ea1b8a199f7}", RegistryValueKind.String);

            GUIDKey.Close();
            ExtensionsKey.Close();
            MicrosoftKey.Close();
        }


        public void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] OLECMD[] prgCmds, ref OLECMDTEXT CmdText) {
            MessageBox.Show("query");
        }

        public void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, ref object pvaIn, ref object pvaOut) {
            MessageBox.Show("Exec");
        }

        private void TakeScreenshot() {
            int height = 1024; //webBrowser.Document.Body.ScrollRectangle.Height;
            int width = 1024;  //webBrowser.Document.Body.ScrollRectangle.Width;
            //int left = 0;      //webBrowser.Document.Body.ScrollRectangle.Left;
            //int right = 1024;  //webBrowser.Document.Body.ScrollRectangle.Right;
            System.Drawing.Size browserSize = new System.Drawing.Size(width, height);

            bmpScreenshot = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            gfxScreenshot = Graphics.FromImage(bmpScreenshot);
            gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
            bmpScreenshot.Save("c:\\temp.png", ImageFormat.Png);
        }

        public int SetSite(object site) {
            if(site != null) {
                webBrowser = (SHDocVw.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;
        }

        [ComRegisterFunction]
        public static void RegisterBHO(Type type) {
            Console.WriteLine("Registering the BHO");

            RegisterCiteNPLMenuEntry();
            RegisterBHOInWindowsRegistry(type);
        }

        [ComUnregisterFunction]
        public static void UnregisterBHO(Type type) {
            Console.WriteLine("Unregistering the BHO");
            UnRegisterBHOFromWindowsRegistry(type);
        }

        private static void UnRegisterBHOFromWindowsRegistry(Type type) {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);
            string guid = type.GUID.ToString("B");

            if(registryKey != null) {
                registryKey.DeleteSubKey(guid, false);
            }
        }

        private static void RegisterBHOInWindowsRegistry(Type type) {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(BHOKEYNAME, true);

            if(registryKey == null) {
                registryKey = Registry.LocalMachine.CreateSubKey(BHOKEYNAME);
            }

            string guid = type.GUID.ToString("B");
            RegistryKey ourKey = registryKey.OpenSubKey(guid);

            if(ourKey == null) {
                ourKey = registryKey.CreateSubKey(guid);
            }

            ourKey.SetValue("Alright", 1);
            registryKey.Close();
            ourKey.Close();
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T07:29:41+00:00Added an answer on June 9, 2026 at 7:29 am

    Since this is a managed dll, you cannot use either regsvr32 or “DLL Export Viewer”.

    To register your .Net dll for COM Interop, use Regasm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not totally sure but this looks wrong: I have a header file named
Firstly: I am totally a newbie for this kind of work. I have a
Not totally sure how to approach this. I've researched a bit but I've come
I am really a newbie to Moodle and I have got this advanced functionality
This is a totally newbie question, so please bear with me. I am learning
I am totally a newbie in windows programming and I have got a textbox
This is a totally newbie question. I'm running Eclipse on Ubuntu. I created a
This is a newbie C++ pointer question. I have no clue on why this
I'm totally newbie with postgresql but I have a good experience with mysql. I
I know this may be a totally newbie question (I haven't touched C in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.