3

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.

theB
  • 6,450
  • 1
  • 28
  • 38
ChristianB
  • 153
  • 3
  • 13
  • The starting point in this example is `GetObject()` vbscript function. I always used Jacob starting with `CreateObject()` equivalent, so I don't know whether this is possible. There is one unanswered post about trying to access WMI, [here at SourceForge Jacob forum](https://sourceforge.net/projects/jacob-project/forums/forum/375946/topic/4069124) – Jarekczek Oct 03 '12 at 12:22
  • I answered the post even though it was almost a year old. I know how to access a single property but I would like to find a way to query for all available properties. – ChristianB Oct 03 '12 at 15:10
  • Please post here your code so far, so that we could see where the problem lies exactly. And that we could continue from that moment, not start from the scratch :) . Thanks. – Jarekczek Oct 03 '12 at 15:14
  • About `GetObject()`. Looking into `Dispatch.cpp` suggests that if the activex component name contains `:`, then it is treated as a monikier and `CoGetObject` call is made. That's the way your code works, so the problem must appear further. – Jarekczek Oct 03 '12 at 15:27
  • This code snippet doesn't help me much, because it's not the code that I could run and try to improve. I don't know how you get `item`. – Jarekczek Oct 04 '12 at 05:40

1 Answers1

4

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;
    }
  }
}
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
  • Would this apply to the class Win32_ComputerSystem? Could I ask it for a enumerator over the Properties defined here http://msdn.microsoft.com/en-us/library/windows/desktop/aa394102(v=vs.85).aspx#methods? Thanks for this, I hadn't seen anything similar to it. – ChristianB Oct 05 '12 at 17:01
  • This is no longer connected with Jacob, but this is how I understand it: When you have an instance, you can't get a list of properties. But you can get a class description and iterate over the properties and methods that apply to the instance. See [Retrieving Class or Instance Data](http://msdn.microsoft.com/en-us/library/windows/desktop/aa393246%28v=vs.85%29.aspx) – Jarekczek Oct 06 '12 at 08:04