Tuesday, June 7, 2011

WMI Code Using Java

The following example uses Java and WMI.

Additional examples can be found at:

http://www.planetmps.com/

import java.io.Console;
import org.jawin.DispatchPtr;
import org.jawin.UnknownPtr;
import org.jawin.IUnknown;
import org.jawin.IEnumVariant;
import org.jawin.win32.*;
import org.jawin.COMPtr;
import java.io.IOException;

        try
        {
           Ole32.CoInitialize();

           DispatchPtr locator = new DispatchPtr("WbemScripting.SWbemLocator");
           DispatchPtr service = (DispatchPtr)locator.invoke("ConnectServer", ".", "root\\aspnet");
           DispatchPtr ob = (DispatchPtr)service.invoke("Get", "ApplicationLifetimeEvent");
           DispatchPtr objs = (DispatchPtr)ob.invoke("Instances_");
           IUnknown oEnum = (IUnknown)objs.get("_NewEnum");
           IEnumVariant objEnum = (IEnumVariant)oEnum.queryInterface(IEnumVariant.class);
           int c =1;
           Object[] tempobj = new Object[1];
           while (objEnum.Next(c, tempobj) == 1)
           {

               DispatchPtr obj = (DispatchPtr)tempobj[0];
               DispatchPtr propSet = (DispatchPtr)obj.get("Properties_");
               IUnknown pEnum = (IUnknown)propSet.get("_NewEnum");
               IEnumVariant propEnum = (IEnumVariant)pEnum.queryInterface(IEnumVariant.class);
               int d =1;
               Object[] tempProp = new Object[1];
               while (propEnum.Next(d, tempProp) == 1)
               {

                  DispatchPtr prop = (DispatchPtr)tempProp[0];
                  String Name = prop.get("Name").toString();
                  Object Value = prop.get("Value");
                  if(Value == null)
                  {
                     println Name + ":";
                  }
                  else
                  {
                      if(prop.getClass().isArray() == true)
                      {

                        String tempstr = "";
                        Object[] a = new Object[1];
                        int l = prop.get("Value").toString().length()-1;
                        for(int x in 0..l)
                        {
                            a[0] = x;
                            tempstr = tempstr + prop.getN("Value", a).toString();
                        }
                      System.out.println(Name + ":" + tempstr);
                      }
                      Else
                      {

                        if(prop.get("CIMType") == 101)
                        {
                           println Name + ":" + prop.get("Value").toString().substring(4,6) + "/" + prop.get("Value").toString().substring(6,8) + "/" + prop.get("Value").toString().substring(0,4) + " " + + prop.get("Value").toString().substring(8,10) + ":" + prop.get("Value").toString().substring(10,12) + ":" + prop.get("Value").toString().substring(12,14);
            }
            else
            {
                           println Name + ":" + prop.get("Value").toString();
            }

                     }
                  }
               }
           break;
           }
        Ole32.CoUninitialize();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

No comments:

Post a Comment