有关WMI的小知识
是微软对来自(DMTF)的(WBEM)和(CIM)标准的实现。WMI用于访问Windows系统、应用、网络、设备等组件,并管理它们。连接到一台机器通过DCOM进行管理。因此,有关DCOM的小知识将有助于本文的理解。你可以到MSDN了解有关WMI的更多细节。
市场上有一些在使用 JAVA 调用 WMI的好库,包括
现在,来看一个使用JAVA调用WMI的例子。这个例子利用J-Interop的API使用Win32_Service类解释WMI操作,将启动和停止在这个例子中的窗口服务。
下面的代码示例显示了使用J-Interop如何初始化DCOM会话,并连接到远程DCOM服务使。它使用SWbemLocator对象连接到SWbemServices,SWbemServices对象提供对本地或远程计算机WMI的访问,它调用“ConnectServer”方法连接到SWbemServices。在本例中,提供管理员级别的用户连接到远程计算机。
一旦你获得对SWbemServices对象的引用,就可以调用这个类的任何方法。其中WbemServices.InstancesOf方法获得任何Win32类的实例。
也可以使用WMI查询语言(WQL)达到同样的目的,如下所示:
finalintRETURN_IMMEDIATE=0x10;finalintFORWARD_ONLY=0x20;Object[]params=newObject[]{newJIString("SELECT*FROMWin32_Service"),JIVariant.OPTIONAL_PARAM(),newJIVariant(newInteger(RETURN_IMMEDIATE+FORWARD_ONLY))};JIVariant[]servicesSet=wbemServices.callMethodA("ExecQuery",params);IJIDispatchwbemObjectSet=(IJIDispatch)narrowObject(servicesSet[0].getObjectAsComObject());
现在,已得到Win32_Service类的实例,可采用下述代码来调用同一类的方法,因为,它返回多个服务实例,要列举它们以便获取IJIDispatcher服务。
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()); JIVariant returnStatus = wbemObjectDispatch.callMethodA("StopService"); System.out.println(returnStatus.getObjectAsInt()); }
现在,下面的代码显示了一个使用WMI启动和停止Windows服务的完整Java类。
packagecom.wmi.windows; importstaticorg.jinterop.dcom.core.JIProgId.valueOf;importstaticorg.jinterop.dcom.impls.JIObjectFactory.narrowObject;importstaticorg.jinterop.dcom.impls.automation.IJIDispatch.IID;importjava.util.logging.Level;importorg.jinterop.dcom.common.JIException;importorg.jinterop.dcom.common.JIRuntimeException;importorg.jinterop.dcom.common.JISystem;importorg.jinterop.dcom.core.IJIComObject;importorg.jinterop.dcom.core.JIArray;importorg.jinterop.dcom.core.JIComServer;importorg.jinterop.dcom.core.JISession;importorg.jinterop.dcom.core.JIString;importorg.jinterop.dcom.core.JIVariant;importorg.jinterop.dcom.impls.automation.IJIDispatch;importorg.jinterop.dcom.impls.automation.IJIEnumVariant; publicclassServiceManager{ privatestaticStringdomai nName=""; privatestaticStringuserN ame="administrator"; privatestaticStringpassw ord=""; privatestaticStringhostI P="127.0.0.1"; privatestaticfinalString win32_namespace="ROOT\\CIMV2"; privatestaticfinalintSTO P_SERVICE=0; privatestaticfinalintSTA RT_SERVICE=1; privateJISessiondcomSess ion=null; publicstaticvoidmain(String[]args){ ServiceManagermanager=newServiceManager(); manager.stopService(domainName,hostIP,userName,password,"MySql");//stopsaservicenamedMySql } publicvoidstartService(StringdomainName,Stringhostname,Stringusername,Stringpassword,StringserviceName){ execute(domainName,hostname,username,password,serviceName,START_SERVICE); } publicvoidstopService(StringdomainName,Stringhostname,Stringusername,Stringpassword,StringserviceName){ execute(domainName,hostname,username,password,serviceName,STOP_SERVICE); } publicvoidexecute(StringdomainName,Stringhostname,Stringusername,Stringpassword,StringserviceName,intaction){try{ IJIDispatchwbemServices=createCOMServer(); finalintRETURN_IMMEDIATE=0x10; finalintFORWARD_ONLY=0x20; Object[]params=newObject[]{ newJIString("SELECT*FROMWin32_ServiceWHEREName='"+serviceName+"'"), JIVariant.OPTIONAL_PARAM(), newJIVariant(newInteger(RETURN_IMMEDIATE+FORWARD_ONLY)) }; JIVariant[]servicesSet=wbemServices.callMethodA("ExecQuery",params); IJIDispatchwbemObjectSet =(IJIDispatch)narrowObject(servicesSet[0].getObjectAsComObject()); JIVariantnewEnumvariant=wbemObjectSet.get("_NewEnum"); IJIComObjectenumComObjec t=newEnumvariant.getObjectAsComObject(); IJIEnumVariantenumVarian t=(IJIEnumVariant)narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID)); Object[]elements=enumVariant.next(1); JIArrayaJIArray=(JIArray)elements[0]; JIVariant[]array=(JIVariant[])aJIArray.getArrayInstance(); for(JIVariantvariant:array){ IJIDispatchwbemObjectDis patch=(IJIDispatch)narrowObject(variant.getObjectAsComObject()); //Printobjectastext. JIVariant[]v=wbemObjectDispatch.callMethodA("GetObjectText_",newObject[]{1});