0 Replies Latest reply: Jan 31, 2007 7:40 AM by 843804 RSS

    passing and using object from C++ into javabean, packaged into activeX

    843804
      I want to set object from c++ program into java bean, packaged into activeX, and use it there.
      on the java-side it represents in sun.plugin.javascript.ocx.JSObject object, but i can not call any method from this object - sun.plugin.com.DispatchClient throws UnsatisfiedLinkError even toString()!!!
      all used dll are in PATH, all java class in CLASS_PATH, all other callings of methods of java-activeX from C++ works fine!

      I create simple java bean, based on swing component, with method for setting object into bean from outside, and method for testing this object by action from any java button from this component.
      package bean by packager.exe into activeX dll, and register it.
      create MFC project and insert this activeX into MFC form in this project.
      generate C++ class for this activeX.
      add MFC button into MFC form and add code into button click handler, for creating any COM object and setting it into java-activeX.
      build and run MFC programm, push MFC button and then push java button. and get:
      //////////////////////////////////
      
      liveconnect: Invoking method: public void com.db.test.jmodule.JModuleView.setComServObj(java.lang.Object)
      liveconnect: Needs conversion: sun.plugin.javascript.ocx.JSObject --> java.lang.Object
      java.lang.UnsatisfiedLinkError: sun.plugin.com.DispatchClient.nativeGetDispType(II)Ljava/lang/String;
                 at sun.plugin.com.DispatchClient.nativeGetDispType(Native Method)
                 at sun.plugin.com.DispatchClient.getDispType(Unknown Source)
                 at sun.plugin.javascript.ocx.JSObject.toString(Unknown Source)
                 at com.db.test.jmodule.JModuleView$1.actionPerformed(JModuleView.java:26)
                 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
                 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
                 at java.awt.Component.processMouseEvent(Unknown Source)
                 at javax.swing.JComponent.processMouseEvent(Unknown Source)
                 at java.awt.Component.processEvent(Unknown Source)
                 at java.awt.Container.processEvent(Unknown Source)
                 at java.awt.Component.dispatchEventImpl(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.EventQueue.dispatchEvent(Unknown Source)
                 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.run(Unknown Source)
      
      //////////////////////////////////
      or if comment 'System.out.println(comServObj.toString());' line in my example
      
      
      liveconnect: Invoking method: public void com.db.test.jmodule.JModuleView.setComServObj(java.lang.Object)
      liveconnect: Needs conversion: sun.plugin.javascript.ocx.JSObject --> java.lang.Object
      liveconnect: Invoking JS method: testcallback
      java.lang.UnsatisfiedLinkError: sun.plugin.com.DispatchClient.nativeInvoke(ILjava/lang/String;I[Ljava/lang/Object;II)Ljava/lang/Object;
                 at sun.plugin.com.DispatchClient.nativeInvoke(Native Method)
                 at sun.plugin.com.DispatchClient.invoke(Unknown Source)
                 at sun.plugin.javascript.ocx.JSObject.invoke(Unknown Source)
                 at com.db.test.jmodule.JModuleView$1.actionPerformed(JModuleView.java:28)
                 at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                 at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
                 at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
                 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
                 at java.awt.Component.processMouseEvent(Unknown Source)
                 at javax.swing.JComponent.processMouseEvent(Unknown Source)
                 at java.awt.Component.processEvent(Unknown Source)
                 at java.awt.Container.processEvent(Unknown Source)
                 at java.awt.Component.dispatchEventImpl(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                 at java.awt.Container.dispatchEventImpl(Unknown Source)
                 at java.awt.Component.dispatchEvent(Unknown Source)
                 at java.awt.EventQueue.dispatchEvent(Unknown Source)
                 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                 at java.awt.EventDispatchThread.run(Unknown Source)
      //////////////////////////////////
      I work with java 1.6.0(build 1.6.0-b105) and MS Visual Studio 2005, in Windows XP.
      this is my worked example for reproduction this problemm:
      //////////////////////////////////
      test java class:
      
      package com.db.test.jmodule;
      
      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.ActionListener;
      import java.awt.event.ActionEvent;
      
      import sun.plugin.javascript.ocx.JSObject;
      
      public class JModuleView extends JPanel {
      
          private boolean f = true;
          private JButton button;
          private JLabel label;
          private Object comServObj;
      
          public JModuleView() {
              super(new BorderLayout());
              setPreferredSize(new Dimension(200, 100));
      
              add(BorderLayout.NORTH, button = new JButton("test callback"));
              button.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      if(comServObj != null)
                          try {
                              System.out.println(comServObj.toString());//if comment this method, 'jso.invoke("testcallback", new Object[]{}, 1);' throws the same exception
                              JSObject jso = (JSObject)comServObj;
                              jso.invoke("testcallback", new Object[]{}, 1);//if call 'jso.invoke("testcallback", null, 1);' nothing varies
                          } catch (Throwable e1) {
                              e1.printStackTrace();
                          }
                      else
                          JOptionPane.showMessageDialog(JModuleView.this, "ComServObj is null", "JModuleView", JOptionPane.INFORMATION_MESSAGE);
                  }
              });
      
              add(label = new JLabel("! label !"));
              label.setOpaque(false);
          }
      
          public void setButtonBColor() {
              button.setBackground(f ? Color.RED : Color.GREEN);
              button.repaint();
              f = !f;
          }
      
          public void setLabelBColor(int i) {
              label.setBackground(new Color(i));
              label.repaint();
          }
      
          public void setLabelFColor(int i) {
              label.setForeground(new Color(i));
              label.repaint();
          }
      
          public void setLabelFont(String name, int size) {
              label.setFont(new Font(name, Font.PLAIN, size));
              label.repaint();
          }
      
          public void setComServObj(Object _comServObj) {
              comServObj = _comServObj;
          }
      
      }
      
      ///////////////////////////////////////////////////////////////////////////////////////////
      test java class bean info:
      
      package com.db.test.jmodule;
      
      import javax.swing.*;
      import java.beans.SimpleBeanInfo;
      import java.beans.BeanDescriptor;
      import java.beans.MethodDescriptor;
      
      public class JModuleViewBeanInfo extends SimpleBeanInfo {
          private static Class BEAN = JModuleView.class;
      
          public BeanDescriptor getBeanDescriptor() {
              BeanDescriptor beandescriptor = new BeanDescriptor(BEAN);
              beandescriptor.setDisplayName("JModuleViewBeanInfo. (c) gogi");
              beandescriptor.setShortDescription("This bean test java component like activeX");
              return beandescriptor;
          }
      
          public MethodDescriptor[] getMethodDescriptors() {
              try {
                  MethodDescriptor md1 = new MethodDescriptor(BEAN.getMethod("setComServObj", new Class[]{Object.class}));
                  MethodDescriptor md2 = new MethodDescriptor(BEAN.getMethod("setButtonBColor", null));
                  MethodDescriptor md3 = new MethodDescriptor(BEAN.getMethod("setLabelBColor", new Class[]{int.class}));
                  MethodDescriptor md4 = new MethodDescriptor(BEAN.getMethod("setLabelFColor", new Class[]{int.class}));
                  MethodDescriptor md5 = new MethodDescriptor(BEAN.getMethod("setLabelFont", new Class[]{String.class, int.class}));
                  return new MethodDescriptor[]{md1, md2, md3, md4, md5};
              } catch (NoSuchMethodException e) {
                  JOptionPane.showMessageDialog(null, e.toString(), "!!!", JOptionPane.ERROR_MESSAGE);
              }
              return null;
          }
      
      }
      
      //////////////////////////////////////////////////////////////////////////////
      test COM object idl:
      
      import "oaidl.idl";
      import "ocidl.idl";
      
      [
                 object,
                 uuid(1CD48CB8-EC6B-4DA8-82E7-7FAC06C512E6),
                 dual,
                 nonextensible,
                 helpstring("IComServObj Interface"),
                 pointer_default(unique)
      ]
      interface IComServObj : IDispatch{
                 [id(1), helpstring("method testcallback")] HRESULT testcallback(void);
      };
      [
                 uuid(D4EC9A9B-08C3-4058-A2B7-8E78CEC798A0),
                 version(1.0),
                 helpstring("comserv 1.0 Type Library")
      ]
      library comservLib
      {
                 importlib("stdole2.tlb");
                 [
                            uuid(FBDEDFA1-975A-4349-8890-F9A3C0768077),
                            helpstring("ComServObj Class")
                 ]
                 coclass ComServObj
                 {
                            [default] interface IComServObj;
                 };
      };
      
      /////////////////////////////////////////////////////
      c++ class for packaged activeX (JModuleView)
      
      class CJModuleViewDispatch : public CWnd
      {
      protected:
                 DECLARE_DYNCREATE(CJModuleViewDispatch)
      public:
                 CLSID const& GetClsid()
                 {
                            static CLSID const clsid
                                       = { 0x6CEC14D, 0xB493, 0x438D, { 0x80, 0x50, 0x47, 0x56, 0x4A, 0x6B, 0x12, 0x34 } };
                            return clsid;
                 }
                 virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle,
                                                                        const RECT& rect, CWnd* pParentWnd, UINT nID,
                                                                        CCreateContext* pContext = NULL)
                 {
                            return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID);
                 }
      
          BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd,
                                                  UINT nID, CFile* pPersist = NULL, BOOL bStorage = FALSE,
                                                  BSTR bstrLicKey = NULL)
                 {
                            return CreateControl(GetClsid(), lpszWindowName, dwStyle, rect, pParentWnd, nID,
                            pPersist, bStorage, bstrLicKey);
                 }
      
      // Attributes
      public:
      
      // Operations
      public:
      
                 void setButtonBColor()
                 {
                            InvokeHelper(0x8000, DISPATCH_METHOD, VT_EMPTY, NULL, NULL);
                 }
                 void setComServObj(LPDISPATCH arg0)
                 {
                            static BYTE parms[] = VTS_DISPATCH ;
                            InvokeHelper(0x8001, DISPATCH_METHOD, VT_EMPTY, NULL, parms, arg0);
                 }
                 void setLabelBColor(long arg0)
                 {
                            static BYTE parms[] = VTS_I4 ;
                            InvokeHelper(0x8002, DISPATCH_METHOD, VT_EMPTY, NULL, parms, arg0);
                 }
                 void setLabelFColor(long arg0)
                 {
                            static BYTE parms[] = VTS_I4 ;
                            InvokeHelper(0x8003, DISPATCH_METHOD, VT_EMPTY, NULL, parms, arg0);
                 }
                 void setLabelFont(LPCTSTR arg0, long arg1)
                 {
                            static BYTE parms[] = VTS_BSTR VTS_I4 ;
                            InvokeHelper(0x8004, DISPATCH_METHOD, VT_EMPTY, NULL, parms, arg0, arg1);
                 }
      
      };
      
      //////////////////////////////////////////////////////////////
      code for setting COM object into java-activeX from C++ code
      
       IComServObj *obj;
       HRESULT hRes = CoCreateInstance(CLSID_ComServObj, 0, CLSCTX_ALL, IID_IComServObj, (void**)&obj);
                 _ASSERTE(SUCCEEDED(hRes));
       www.setComServObj(obj);
      
      //////////////////////////////////