Adding C# handler to COM Event (VB6)

Posted on 2006-06-03
Last Modified: 2008-02-01
Hi everyone,

In my C# project I have a reference to a COM DLL (com interop). From that library I am using an object that has an event which I am trying associate a C# handler to.

Typically in C#, I believe a class would have a delegate you could use to associate method to event but this COM object was intended for VB6.

How do I declare a function (in C#) to handle this object's event?

Thanks in advance.
Question by:djshorty
    LVL 10

    Expert Comment

    Add a reference to that COM object and use the object browser to find out the even signature. Then create a function which matches that signature. You can then add a new event sink which points to this new function.

    LVL 19

    Accepted Solution

    It's simpler than that.  The Runtime Callable Wrapper let's you access the COM events directly.  Just create an Event of that type and use it.  For example:

    For example, my C# user control hosts the COM object from ShDocVw.DLL known as the Web Browser Control (IWebBrowser2).  Here's the declaration of the event NavigateComplet2:

    this.wbControl.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.wbControl_NavigateComplete2);

    Later, the event function is defined:

    private void wbControl_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)

    Note, this was all done for me.  All I had to do was double-click on the event in the event properties for the control in the Visual Studio IDE.
    LVL 1

    Author Comment

    Thank you Dan7el.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now