Menu

How can I create an instance of a class in the specified namespace?

Mike J
2013-08-07
2013-10-16
  • Mike J

    Mike J - 2013-08-07

    Hi,
    I am working on DCOM testing using j-interop. It's good to make a direct connection to the WMI device in a pure java way. But what I need to subscribe to events on the remote COM server. In order to do so, a bunch of classes' instances should be created in the specified namespace. Since j-interop implements the Scripting API for WMI, I checked the API and found that most of methods in SWbemServices are for get/select instances of the classes. One method is for deleting the instances. But there is no methods for creating instances. I was wondering if there is any way for j-interop (narrow down to SWbemServices further) to create instances in a specified namespace?

     
    • Vikram Roopchand

      Hi,

      Do you have an example in a parallel language (such as VB or C++/COM) ? We
      can try creating something similar ...

      best regards,
      Vikram

      On Thu, Aug 8, 2013 at 1:21 AM, Mike J mikej1688@users.sf.net wrote:

      Hi,
      I am working on DCOM testing using j-interop. It's good to make a direct
      connection to the WMI device in a pure java way. But what I need to
      subscribe to events on the remote COM server. In order to do so, a bunch of
      classes' instances should be created in the specified namespace. Since
      j-interop implements the Scripting API for WMI, I checked the API and found
      that most of methods in SWbemServices are for get/select instances of the
      classes. One method is for deleting the instances. But there is no methods
      for creating instances. I was wondering if there is any way for j-interop
      (narrow down to SWbemServices further) to create instances in a specified
      namespace?


      How can I create an instance of a class in the specified namespace?https://sourceforge.net/p/j-interop/discussion/600729/thread/c1feb5c6/?limit=25#1531

      Sent from sourceforge.net because you indicated interest in
      https://sourceforge.net/p/j-interop/discussion/600729/

      To unsubscribe from further messages, please visit
      https://sourceforge.net/auth/subscriptions/

      --
      The Mind is a place of its own. It can make a heaven out of hell or a hell
      out of heaven. Attitude is everything. No matter how adverse conditions
      maybe, one has the capacity to turn things around by one's Determination,
      Perseverance and Hardwork.

      John Milton
      (Paradise Lost)

       
      • Mike J

        Mike J - 2013-08-19

        Hi Vikram,
        I think I have found a way to create instances of the classes in a specified namespace. For instance, the namespace is "root\cimv2". In order to create an instance of the class, "__Win32Provider", it can be done as follows,

        Object[] params = new Object[] { new JIString("Win32Peovider"), new Integer(0x20000), JIVariant.OPTIONAL_PARAM() };
        results = wbemServices_dispatch.callMethodA("Get", params);
        IJIComObject comObject = JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
        IJIDispatch wbemObject_dispatch = (IJIDispatch) comObject;
        results = wbemObject_dispatch.callMethodA("
        Spawn", new Object[] { new Integer(0) });
        comObject = JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
        IJIDispatch win32Provider_dispatch = (IJIDispatch) comObject;
        //here the map contains the properties for the class. they are name/value pairs.
        for (Entry entry : map.entrySet())
        {
        String key = (String) entry.getKey();
        String value = (String) entry.getValue();
        win32Provider_dispatch.put(key, new JIVariant(value));
        }
        results = win32Provider_dispatch.callMethodA("__Put", new Object[] { new Integer(0), JIVariant.OPTIONAL_PARAM() });
        comObject = JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());

        I checked and found that the instance was created in the namespace. But I am not sure if it's the correct way for instance creation. Here I share the code with you and please let me know if it's not right.

        But I have another problem now. How can I create a derived class from a base class through j-interop? Then get an instance of the derived class? For example, I have a base class, "EventConsumer". I can create an instance of the class with no problem, by using the above method. But here it's not my purpose.Instead of creating an instance of "EventConsumer", I am interested in first creating a derived class of "__EventConsumer", such as "MyCustomizedEventConsumer", and then create an instance of the derived class. Can j-interop have a way to create a derived class from a base class?

        Thanks,

        Mike

         
        • Anonymous

          Anonymous - 2013-10-16

          Hi Mike ,

          I tried with your sample to create a new instance of a class but it did not work and gave an exception. Did you find any other way to get this solve.

           
  • Mike J

    Mike J - 2013-08-19

    Sorry, __EventConsumer is an abstract class, cannot be instantiated. I need to create a derived class of it first.

     

Log in to post a comment.