Re: Using .NET callback via COM

From: Daniel Petersson (DanielPetersson_at_discussions.microsoft.com)
Date: 01/27/05


Date: Thu, 27 Jan 2005 05:25:04 -0800

A __nogc clas smay have a ref to a __gc class through a gcroot<> template
pointer. gcroot<> encapsules the GCHandle class but it works somewhat like a
transparent smartpointer such as CComPtr<>.

However there are some issues with implementations like this, your callback
will always end up in the default AppDomain, see my question below about COM
callbacks and AppDomains. On this I still have no answe but I think a
possible solution is to "redefine" the interface as a managed __interface and
decorate it with GuidAttribute And InterfaceType attribute to make it "act"
as the old interface, then you need to get your hands on the CCW for your
object, for this there are some methods on the Marshal class but I not sure
of how they are intended to be used.

regards
Daniel Petersson

"Nauman" wrote:

> I'm in a similar situation to the one described above. I haven't been
> able to find a solution to this on the newsgroups so far. I'm using
> VC++ .NET 2003. Here are some details of my problem:
> I have a DLL that exports a method I need to call. This method takes an
> COM interface as one of its arguments, and provides callbacks through
> this. The interface is
> class IMyCallback : public IUnknown
> {
> HResult __stdcall OnEventOccurred(....) = 0;
> }
>
> My unmanaged C++ implementation is:
> class CallbackImpl : public IMyCallback {
> public:
> HResult __stdcall QueryInterface(REFIID iid, void** ppvObj);
> unsigned long __stdcall _(uInt32) AddRef();
> unsigned long __stdcall uInt32) Release();
>
> CallbackImpl ();
> ~CallbackImpl ();
>
> HResult __stdcall OnEventOccurred(....) { ... }
> }
>
>
> My managed C++ class has the unmanaged class as a member:
> public __gc class ManagedDllWrapper {
> ....
> ....
> private:
> CallbackImpl __nogc* m_myCallback;
> }
>
> I call the dll method in ManagedDllWrapper and pass it the m_myCallback
> member.
>
> I can implement this interface in an unmanaged class and pass this
> successfully into the method. The callback occurs successfully. What I
> wanted to do was to implement the OnEventOccurred method so that it
> calls a managed class method -- I can thus expose an event for this on
> my managed class. However, since an unmanaged class cannot have a
> managed class as a member, how do I let my managed class know when the
> callback is called?
>
> Thanks in advance for your help!
> Nauman
>
>



Relevant Pages

  • Re: Problem with imported callback to C#
    ... Usually it happens when callback declaration is using non-standard ... exposes interface, which enables to set a callback, which is called ... DirectShow graph with mentioned face detector filter and calls its ... int BufferCB; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Function pointers (Callback functions) in Java ?
    ... object to a local var called 'callback'. ... > In the above I don't see the Opps interface. ... The code was a very simple example of creating a named inner class ... Its may be worth knowing that you can create anonymous inner classes ...
    (comp.lang.java.programmer)
  • Re: Callback Interface functions NOT being Called
    ... a callback interface interface ICallbackInterface which implements the ... callback functions of the buttons I create in Ribbon interface. ... The ribbon callbacks use IDispatch, always, so you must return ... You need COM_INTERFACE_ENTRY2(IDispatch, ICallbackInterface) ...
    (microsoft.public.office.developer.com.add_ins)
  • Re: NT service and connection points
    ... A callback interface is an interface you specify in your server ... Connection points are just a standard ... Microsoft MVP, MCSD ...
    (microsoft.public.vc.atl)
  • Re: CEPTCL: Doesnt RX multicast, has anyone seen it work before?
    ... proc udpEventHandler {sock interface} { ... decodePdu $datapacket RX $interface ... ## callback should optionally rebuild the hello to use dynamic ...
    (comp.lang.tcl)