Search code examples
javawindowswinapiwmijacob

Java JACOB retrieve all the properties for a given Win32_* class object


I am currently working on a java-base cross-platform software distributor and I chose to use native OS mechanisms to prevent the users from having to do any setup before hand.

I chose JSCH for SSH2 and JACOB for Java. I realize that JACOB limits me to Windows as the starting host, but that is something I can live with.

I am writing a wrapper around JACOB to use some of the native mechanisms for talking to the target via WMI and I am running into a little issue. I am trying to retrieve a list of ALL the properties available for a given Win32_ class object and I haven't been able to do it.

This link shows you can do it in VB http://www.vbsedit.com/scripts/misc/wmi/scr_1333.asp and I was wondering if anyone had been able to figure it out when using JACOB.

EDIT : (code snippet)

item = enumVariant.nextElement().toDispatch();
            //Dispatch.class returns a variant which can convert to java form
            String serviceName = Dispatch.call(item, "Name").toString();
            String servicePath = Dispatch.call(item,"PathName").toString();
            int servicePID = Dispatch.call(item,"ProcessId").getInt();
            //System.out.println("Service: "+serviceName+" ServicePath: "+servicePath+" PID: "+servicePID);
            //System.out.println(serviceName+" "+servicePath+" "+servicePID);
            list.add(serviceName+" "+servicePID);

Code above shows that I can ask for individual properties but there isn't a good way to ask for ALL properties.


Solution

  • Here is the answer for your question. I hope so, because I don't know where exactly your problem lies. I still think that when asking you should provide the part of code instead of requiring the answerer to write everything.

    This may be also an answer to a more general problem, that I just learnt:
    How to enumerate all items in a collection, implementing For Each construct?
    One should use EnumVariant Jacob class.

    import com.jacob.activeX.*;
    import com.jacob.com.*;
    
    public class testJacob {
      public static void main(String args[]) {
        String sMoniker = "winmgmts:{impersonationLevel=impersonate}!" +
          "\\\\.\\root\\cimv2";
        Dispatch dServ = new Dispatch(sMoniker);
        Variant v = Dispatch.call(dServ, "SubclassesOf");
        // SWbemObjectSet object
        // http://msdn.microsoft.com/en-us/library/aa393762%28v=vs.85%29.aspx
        Dispatch objSet = v.getDispatch();
        System.out.println("object count: " + objSet.get(objSet, "count"));
        int cObj = 0;
        EnumVariant en = new EnumVariant(objSet);
        while (en.hasMoreElements()) {
          // SWbemObject object
          // http://msdn.microsoft.com/en-us/library/aa393741(v=vs.85).aspx
          Dispatch dItem = en.nextElement().getDispatch();
          Dispatch dPath = Dispatch.get(dItem, "Path_").getDispatch();
          String sClass = Dispatch.get(dPath, "Class").getString();
          System.out.println("path: " + sClass);
          Dispatch dObj = Dispatch.call(dServ, "get", sClass).getDispatch();
          Dispatch dProps = Dispatch.call(dObj, "Properties_").getDispatch();
          EnumVariant enProp = new EnumVariant(dProps);
          while (enProp.hasMoreElements()) {
            Dispatch dProp = enProp.nextElement().getDispatch();
            String sProp = Dispatch.get(dProp, "name").getString();
            System.out.println("property: " + sProp);
          }
          if (++cObj >= 5)
            break;
        }
      }
    }