title: WMI through Java using J-Interop
body: <p>I am new to WMI and using J-interop lib for COM communication through Java...
  I have a requirement to get Physical memory data from remote system.</p>
  
  <p>I have following code : it provides some of the data but have some flaw in it like I have 2 GB memory in my system with two memory cards 1 GB each and this code only shows me 1 GB of memory:</p>
  
  <p>Please help me in rectifying the following code and any good reference to follow for J-Interop with good examples:</p>
  
  <pre><code>JISession dcomSession = null;
  try {
  dcomSession = init(domain, username, password);
  JIComServer comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"), hostname, dcomSession);
  IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));
  
  Object[] params = new Object[] { new JIString(hostname), new JIString("ROOT\\CIMV2"),
          JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),
          JIVariant.OPTIONAL_PARAM(), new Integer(0), JIVariant.OPTIONAL_PARAM() };
  JIVariant results[] = wbemLocator.callMethodA("ConnectServer", params);
  
  IJIDispatch wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());
  
  final int RETURN_IMMEDIATE = 0x10;
  final int FORWARD_ONLY = 0x20;
  
  params = new Object[] { new JIString("SELECT * FROM Win32_PhysicalMemory"), JIVariant.OPTIONAL_PARAM(),
          new JIVariant(new Integer(RETURN_IMMEDIATE)) };
  JIVariant[] servicesSet = wbemServices.callMethodA("ExecQuery", params);
  IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(servicesSet[0].getObjectAsComObject());
  
  JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
  IJIComObject enumComObject = newEnumvariant.getObjectAsComObject();
  IJIEnumVariant enumVariant = (IJIEnumVariant) narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
  
  Object[] elements = enumVariant.next(1);
  JIArray aJIArray = (JIArray) elements[0];
  
  JIVariant[] array = (JIVariant[]) aJIArray.getArrayInstance();
  for (JIVariant variant : array) {
      IJIDispatch wbemObjectDispatch = (IJIDispatch) narrowObject(variant.getObjectAsComObject());
  
      // Print object as text. Optional - comment if not needed
      JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });
      System.out.println(v1[0].getObjectAsString2());
      System.out.println(wbemObjectDispatch.get("DataWidth").getObjectAsInt());
      System.out.println(wbemObjectDispatch.get("Status").getObjectAsInt());
      System.out.println(wbemObjectDispatch.get("Speed").getObjectAsInt());
      System.out.println(wbemObjectDispatch.get("FormFactor").getObjectAsInt());
  }
  } catch (Exception e) {
  e.printStackTrace();
  } finally {
  if (dcomSession != null) {
      try {
          JISession.destroySession(dcomSession);
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  }
  </code></pre>