6 Replies Last post: Sep 30, 2005 2:27 PM by Serge Piletsky  
Pierre-Yves SINOU   7 posts since
Apr 24, 2006
Currently Being Moderated

Sep 20, 2005 9:48 PM

Registering callbacks - Events handling

Hello,

 

some time ago I contacted you about a problem I had concerning message loops for COM components.

You suggested me, and you were right, to use a dedicated instance of OleMessageLoop for every instance of OleContainer I need.

The suggested code is as follows :

 

        OleMessageLoop messageLoop = new OleMessageLoop(getClass().getName() + hashCode());

        messageLoop.doStart();

        _container = new OleContainer(messageLoop);

        _container.createObject(ocxName);

        add(_container, BorderLayout.CENTER);

 

Each container now receives only the messages that are sent to it.

 

I now try to register callbacks for each container that are instanciated using this method, and I get an exception c0000005.

The callbacks worked well before, but now, on the second attempt to register the callbacks, I get this exception.

 

Here is the class involved:

 

public class UserControl1Container extends JPanel implements UserControl1ContainerListener {

     protected Frame frameParent;

    protected OleContainer _container = null;

    protected IConnectionPoint connectionPoint = null;

    protected Int32 iHandler = null;

    protected UserControl1Listener ocxListener = null;

 

public UserControl1Container(Frame frame) {

          super();

          frameParent = frame;

          setBackground(Color.GRAY);

          setPreferredSize(new Dimension(600, 400));

 

          final BorderLayout layout = new BorderLayout();

          this.setLayout(layout);

     }

 

private void LoadActiveX(String ocxName) {

        OleMessageLoop messageLoop = new OleMessageLoop(getClass().getName() + hashCode());

        messageLoop.doStart();

        _container = new OleContainer(messageLoop);

        _container.createObject(ocxName);

        add(_container, BorderLayout.CENTER);

     }

     

public void active() {

          _container.doVerb(OleVerbs.INPLACEACTIVATE);

     }

 

private void setupListener()

     {

         IClassFactoryServer server = new IClassFactoryServer(UserControl1Listener.class);

         server.registerInterface(IDispatch.class, new IDispatchVTBL(server));

         server.registerInterface(__UserControl1.class, new IDispatchVTBL(server));

         server.setDefaultInterface(IDispatch.class);

     

         IClassFactory classFactory = server.createIClassFactory();

         IDispatchImpl handler = new IDispatchImpl();

         classFactory.createInstance(null, handler.getIID(), handler);

     

         IOleObjectImpl oleObject = _container.getOleObject();

         IConnectionPointContainer connectionPointContainer = new IConnectionPointContainerImpl(oleObject);

         connectionPoint = connectionPointContainer.findConnectionPoint(new IID(__UserControl1.INTERFACE_IDENTIFIER));

     

         IEnumConnections enumConnections = connectionPoint.enumConnections();

         enumConnections.reset();

     

         ocxListener = (UserControl1Listener)server.getInstances().pop();

         ocxListener.setOCXPanel(this);

         ocxListener.addContainerListener(this);

     

         iHandler = connectionPoint.advise(handler);

     }

 

public void FullLoad()

     {

          //this.setVisible(false);

          LoadActiveX("OcxWithEchap.UserControl1");

          OleMessageLoop.addAction(new Runnable()

          {

              public void run()

              {

                  setupListener();          

              }

          });

          this.setVisible(true);

     }

 

public void ocxClosed(EventObject e) {

          connectionPoint.unadvise(iHandler);

          _container.inPlaceDeactivate();

          _container.destroyObject();

          ocxListener.removeContainerListener(this);

          this.setVisible(false);

     }

 

public void myEvent1(EventObject e) {

          System.out.println("   UserControl1Container");

          System.out.println("   -


");

          System.out.println("      myEvent1");

     }

}

 

The exception occurs when this line of code is executed (in the setupListener method) :

    IConnectionPointContainer connectionPointContainer = new IConnectionPointContainerImpl(oleObject);

 

The setupListerner method is called like this:

          OleMessageLoop.addAction(new Runnable()

          {

              public void run()

              {

                  setupListener();          

              }

          });

 

Does the problem come from here, or....

 

Thanks for your help.

 

Pierre-Yves

 

PS: I attach the whole code to this message, in case you want to reproduce the problem.

Attachments:
Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
1. Sep 21, 2005 1:47 PM in response to: Pierre-Yves SINOU
Re: Registering callbacks - Events handling

Hello,

 

In order to solve this problem you will also need to use that dedicated instance of

OleMessageLoop of the OleContainer to setup the listener.

 

In your code you are using the common OleMessageLoop instance to setup the listener:

 

OleMessageLoop.addAction(new Runnable() {

        public void run() {

                setupListener();

});

 

But if you correct it as it is shown below, the problem will be fixed:

 

In the UserControl1Container.java file:

 

public void FullLoad() {

        LoadActiveX("OcxWithEchap.UserControl1");

        try {

            _container.getOleMessageLoop().doInvokeAndWait(new Runnable() {

                public void run() {

                    setupListener();

                }

            });

        }

        catch (Exception e) {

            e.printStackTrace();

        }

        this.setVisible(true);

}

 

 

An in the UserControl2Container.java file:

 

public void FullLoad() {

        this.LoadActiveX("OcxWithEchap.UserControl2");

        try {

            _container.getOleMessageLoop().doInvokeAndWait(new Runnable() {

                public void run() {

                    setupListener();

                }

            });

        }

        catch (Exception e) {

            e.printStackTrace();

        }

        this.setVisible(true);

}

 

After these modifications you sample worked without any problem.

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
4. Sep 28, 2005 3:56 PM in response to: Pierre-Yves SINOU
Re: Re: Re: Re: Registering callbacks - Events handling

Unfortunately we cannot reproduce this problem in our environment. We have tried various scenarios to reproduce it, but your sample worked without such error.

 

Can you please try to destroy an embedded into OleContainer object using the destroyObject() method before closing a container window?

 

Also what version of JNIWrapper do you use?

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
6. Sep 30, 2005 2:27 PM in response to: Pierre-Yves SINOU
Re: Re: Re: Re: Re: Re: Registering callbacks - Events handling

Hello,

 

To solve this problem you just need to change default close operation of the main window (TestMain class from your sample) from EXIT_ON_CLOSE to DISPOSE_ON_CLOSE.

 

The DISPOSE_ON_CLOSE option, in opposite to EXIT_ON_CLOSE, does not just call System.exit(0); to terminate JVM, but correctly frees opened window and only then a process is terminated.

 

After this fix native errors do not appear any more in your sample application.

 

More Like This

  • Retrieving data ...