Menu

ClassCastException getting IDispatch handle

Alvin
2009-06-10
2012-11-29
  • Alvin

    Alvin - 2009-06-10

    Hello Vikram!

    I am using your latest release of j-interop (2.06) running on Windows XP SP3. I can't find ComFactory anymore, so I used JIObjectFactory. But I get an exception when getting a handle to IDispatch. But this is similar to your code on http://www.j-interop.org/sampleusage.html:

    JISession session = JISession.createSession("localhost", "username", "password");
    JIComServer comServer = new JIComServer(JIClsid.valueOf("429FD8AA-3D35-4F09-9B0B-0732DE6E4EAE"), session);
    IJIComObject comObject = comServer.createInstance();
    IJIDispatch dispatch = (IJIDispatch)JIObjectFactory.narrowObject(comObject.queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883"));

    The exception was:

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: org.jinterop.dcom.core.JIComObjectImpl cannot be cast to org.jinterop.dcom.impls.automation.IJIDispatch
        at com.checkpt.eas.device.Evolve.initialize(Evolve.java:34)
        at com.checkpt.eas.device.Evolve.<init>(Evolve.java:26)
        at com.checkpt.eas.simulator.EvolveTester.<init>(EvolveTester.java:53)
        at com.checkpt.eas.simulator.EvolveTester$5.run(EvolveTester.java:384)
        at java.awt.event.InvocationEvent.dispatch(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)

    Hope you can help me. Thanks!

     
    • Vikram Roopchand

      Hi,
          I need to see your code.

      best regards,
      Vikram

       
    • Alvin

      Alvin - 2009-06-11

      Hi Vikram!

      Thanks for your immediate reply. Here's my code:

      public class Evolve {
         
          private JISession session;
          private JIComServer comServer;
          private IJIComObject comObject;
          private IJIDispatch dispatch;
             
          public Evolve() {
              initialize();
          }
         
          public void initialize() {
              try {
                  session = JISession.createSession("localhost", "username", "password");
                  comServer = new JIComServer(JIClsid.valueOf("429FD8AA-3D35-4F09-9B0B-0732DE6E4EAE"), session);
                  comObject = comServer.createInstance().queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883");
                  dispatch = (IJIDispatch)JIObjectFactory.narrowObject(
                          comObject.queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883"));
              } catch(UnknownHostException unknownHostException) {
                  unknownHostException.printStackTrace();
              } catch(JIException jiException) {
                  jiException.printStackTrace();
              }
          }
         
          public long Open(String techID, String portName, short timeout) {
              long ret = 0;
                     
              Object[] results = null;
              try {
                  results = dispatch.callMethodA("Open", new Object[]{
                          techID, portName, new Short(timeout), new JIVariant(0, true)});
              } catch (JIException e) {
                  e.printStackTrace();
              }
             
              for (int i = 0; i < results.length; i++)
                  System.out.println(results[i]);
             
              return ret;
          }
      }

      And this was based from the IDL snippets:

          // EmeraldObject Interface Object
          [
              object,
              uuid(0AE6649D-9DA8-479D-9AC1-20E8F7E0C883),
              dual,
              helpstring("IEmeraldInterface Interface"),
              pointer_default(unique)
          ]
          interface IEmeraldInterface : IDispatch
          {
              [id(1), helpstring("method Open")]                        HRESULT Open([in] BSTR TechID, [in] BSTR PortName, [in] SHORT TimeOut, [out, retval] LONG* rc);
      ...

       
      • Vikram Roopchand

        Hi,
            You have not asked for a IDispatch interface but you are type casting it to IDispatch.

        comObject = comServer.createInstance().queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883");

        The interface returned here is not IDispatch. In the next line it will throw an exception.

        best regards,
        Vikram

         
    • Alvin

      Alvin - 2009-06-11

      Ooops sorry, wrong code.. That's just an edited one. It should be the same as my first post:

      comObject = comServer.createInstance();
      dispatch = (IJIDispatch)JIObjectFactory.narrowObject(comObject.queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883"));

      Is the second line above incorrect for requesting an IDispatch? What should be the code then? Where will I put the uuid of the interface object, if I will be doing what you did on the sample usage page:

      //Get a handle to it's IDispatch
      IJIDispatch dispatch = (IJIDispatch)JIObjectFactory.narrowObject((IJIComObject)excelObject.queryInterface(IJIDispatch.IID));

      Thank you!

       
    • Alvin

      Alvin - 2009-06-15

      Hello!

      The snippets below still didn't work. I still get ClassCastException. By the way, was the ComFactory obsoleted?

      public class Evolve {

      private JISession session;
      private JIComServer comServer;
      private IJIComObject comObject;
      private IJIDispatch dispatch;

      public Evolve() {
      initialize();
      }

      public void initialize() {
      try {
      session = JISession.createSession("localhost", "username", "password");
      comServer = new JIComServer(JIClsid.valueOf("429FD8AA-3D35-4F09-9B0B-0732DE6E4EAE"), session);
      comObject = comServer.createInstance();
      dispatch = (IJIDispatch)JIObjectFactory.narrowObject(
      comObject.queryInterface("0AE6649D-9DA8-479D-9AC1-20E8F7E0C883"));
      } catch(UnknownHostException unknownHostException) {
      unknownHostException.printStackTrace();
      } catch(JIException jiException) {
      jiException.printStackTrace();
      }
      }

      public long Open(String techID, String portName, short timeout) {
      long ret = 0;

      Object[] results = null;
      try {
      results = dispatch.callMethodA("Open", new Object[]{
      techID, portName, new Short(timeout), new JIVariant(0, true)});
      } catch (JIException e) {
      e.printStackTrace();
      }

      for (int i = 0; i < results.length; i++)
      System.out.println(results[i]);

      return ret;
      }
      }

       

Log in to post a comment.