Re: Using .NET callback via COM
From: Daniel Petersson (DanielPetersson_at_discussions.microsoft.com)
Date: 01/27/05
- Next message: Tony Jones: "Re: ? How to strong name a 3rd Party Assembly ?"
- Previous message: Andrew Wiles: "Circular conflict using ADOMD in Visual Studio"
- In reply to: Nauman: "Re: Using .NET callback via COM"
- Next in thread: Nauman: "Re: Using .NET callback via COM"
- Reply: Nauman: "Re: Using .NET callback via COM"
- Messages sorted by: [ date ] [ thread ]
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
>
>
- Next message: Tony Jones: "Re: ? How to strong name a 3rd Party Assembly ?"
- Previous message: Andrew Wiles: "Circular conflict using ADOMD in Visual Studio"
- In reply to: Nauman: "Re: Using .NET callback via COM"
- Next in thread: Nauman: "Re: Using .NET callback via COM"
- Reply: Nauman: "Re: Using .NET callback via COM"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|