CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2006
    Posts
    1

    Question interop issue: wrapping a library that implements an observer-pattern?

    Hi,

    I've got a problem with my wrapper class and my wrapped native object:
    How can the wrapper be notified if the native object inside is modified?

    Background: I want to wrap a library where an observer pattern is implemented. There the native observer observes a native subject where it was registered. If the subject notifies the registered observers about a change my observer-wrapper was not notified.
    What should I do to make this work? I thought about something like function pointers and delegates but I think I'm on the wrong track.

    Or is it possible to pass a managed object as a parameter to a native object's function?
    Example:

    class AbstractSubject
    {
    public:
    virtual ~AbstractSubject() {};
    virtual void Attach(IObserver*);
    virtual void Detach(IObserver*);
    virtual void Notify();
    protected:
    AbstractSubject() {};
    private:
    std::vector observers;
    };

    class IObserver
    {
    public:
    virtual ~IObserver() {};
    virtual void Update(AbstractSubject* theChangedSubject) = 0;
    protected:
    IObserver() {};
    };



    As you can see the IObserver is a interface class. This interface class is wrapped by a managed c++ interface-class. The native observer object registeres itself on the Attach(IObserver*)-method and is added to the observers-vector.
    If the Nofify()-method of the subject is called the subject iterates over the observers-vector and calls Update(this) (observerobject->Update(this)) on the observer object to notify the observer that something changed.

    But how can I add now my managed class or my managed object to the subject to be notified? Please correct me when I'm wrong but I think I cannot pass the managed-observer-handle to the native method to notify the managed observer.

    BTW: I don't have to source code for the native classes. I only have got a DLL-file, a LIB-file and some header-files to access the classes in the native lib.

    Any help would be appreciated.

    Thanks a lot for you help!

    Best regards,
    Ralf

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: interop issue: wrapping a library that implements an observer-pattern?

    You need to wrap your managed class in a native class to be passed through the native call.

    Have a look at my article here which shows something similar.

    The key to doing what you're trying to do is in the "CNativeDerived" class, which is a native class providing an interface to the managed class.

    The gcroot<> member of this class contains the managed reference, but in a native context.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured