__hook and __unhook of C# event in DLL from unmanaged C++ 
Author Message
 __hook and __unhook of C# event in DLL from unmanaged C++

To hook and unhook a C# event in a dll from an unmanaged C++ class  requires
writing a managed C++ class that does the actual hooking and unhooking.  The
managed C++ class can then forward the event to the unmanaged C++ class.
Using this arrangement, the unmanaged C++ class must also hold a gcroot<
CPPHandler* >.

For example, assume:
    [Note:  this obviously doesn't compile]
    C# CSharpEventSource class in the dll has an event CSharpEvent:

    The managed C++ handler:
    __gc public class CPPHandler
    {
    public:
        CUnmangedClass * cl;
        CPPHandler( CUnmanagedClass * cucl) : cl(cucl)    {    }
        HookEvent( CSharpEventSource * cses)
            {    __hook( CSharpEventSource::CSharpEvent, cses,
CPPHandler::CSharpEventHandler, this);    }

        void CSharpEventHandler(System::Object * o, CSharpEventArgs * e)
            {       cl->EventHandler(o, e);    }
        ...  /*__unhook will also have a member func, etc */
        };

    Code in the unmanaged C++ class (compiled with /CLR):

        __nogc class CUnmanagedClass {
             ...
        gcroot<CPPHandler *> handler;        //member var
            ...
        //code in some member funct hooking up the event...
        handler= new CPPHandler(this);
        handler->HookEvent( CSharpEventSource * ptr);
        //    ... etc.

        void EventHandler( System::Object *o, CSharpEventArgs *e)    {
//do something with the event ...    }
    };

This seems like a lot of scaffolding to handle the event.  I suppose a C++
class derived from gcroot<> is an alternative approach that would allow one
to automatically build the required scaffolding with a template declaration.
Has anyone come up with a different, or better, approach to handling this?

Thanks in advance,
Billw



Fri, 06 May 2005 04:33:43 GMT  
 __hook and __unhook of C# event in DLL from unmanaged C++

Quote:
>This seems like a lot of scaffolding to handle the event.  I suppose a C++
>class derived from gcroot<> is an alternative approach that would allow one
>to automatically build the required scaffolding with a template
declaration.
>Has anyone come up with a different, or better, approach to handling this?

I am not aware of a better solution than this.

Thank you,
Bobby Mattappally
Microsoft VC++/C# Team

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------

Quote:

>Subject: __hook and __unhook of C# event in DLL from unmanaged C++
>Date: Sun, 17 Nov 2002 12:33:43 -0800
>To hook and unhook a C# event in a dll from an unmanaged C++ class  
requires
>writing a managed C++ class that does the actual hooking and unhooking.  
The
>managed C++ class can then forward the event to the unmanaged C++ class.
>Using this arrangement, the unmanaged C++ class must also hold a gcroot<
>CPPHandler* >.

>For example, assume:
>    [Note:  this obviously doesn't compile]
>    C# CSharpEventSource class in the dll has an event CSharpEvent:

>    The managed C++ handler:
>    __gc public class CPPHandler
>    {
>    public:
>        CUnmangedClass * cl;
>        CPPHandler( CUnmanagedClass * cucl) : cl(cucl)    {    }
>        HookEvent( CSharpEventSource * cses)
>            {    __hook( CSharpEventSource::CSharpEvent, cses,
>CPPHandler::CSharpEventHandler, this);    }

>        void CSharpEventHandler(System::Object * o, CSharpEventArgs * e)
>            {       cl->EventHandler(o, e);    }
>        ...  /*__unhook will also have a member func, etc */
>        };

>    Code in the unmanaged C++ class (compiled with /CLR):

>        __nogc class CUnmanagedClass {
>             ...
>        gcroot<CPPHandler *> handler;        //member var
>            ...
>        //code in some member funct hooking up the event...
>        handler= new CPPHandler(this);
>        handler->HookEvent( CSharpEventSource * ptr);
>        //    ... etc.

>        void EventHandler( System::Object *o, CSharpEventArgs *e)    {
>//do something with the event ...    }
>    };

>This seems like a lot of scaffolding to handle the event.  I suppose a C++
>class derived from gcroot<> is an alternative approach that would allow one
>to automatically build the required scaffolding with a template
declaration.
>Has anyone come up with a different, or better, approach to handling this?

>Thanks in advance,
>Billw



Sun, 08 May 2005 06:03:35 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Bug in the implementation of __hook and __unhook for COM events

2. hook / unhook event

3. __hook and __unhook problems in c++

4. Call C# dll from unmanaged C++

5. Unmanaged C++ dll's in C#

6. Unmanaged C++ calling managed C# dll

7. Unified Event Model - VC7.0 C++ Unmanaged Events

8. C# client crashs when calling into Managed C++ which calls unmanaged c++ function

9. Interoperability UnManaged C++, Managed C++, C#

10. Calling __unhook when no __hook has been called

11. Calling __hook and __unhook

12. ShellExecute() seems to unhook system wide hook...

 

 
Powered by phpBB® Forum Software