您的当前位置:首页正文

使用 J-Interop 在 Java 中调用WMI

2024-11-09 来源:个人技术集锦
 ()
??显示全文
使用 J-Interop 在 Java 中调用WMI
1评/817阅
发表于:2011-06-16 07:12

有关WMI的小知识

是微软对来自(DMTF)的(WBEM)和(CIM)标准的实现。WMI用于访问Windows系统、应用、网络、设备等组件,并管理它们。连接到一台机器通过DCOM进行管理。因此,有关DCOM的小知识将有助于本文的理解。你可以到MSDN了解有关WMI的更多细节。

J-Interop

市场上有一些在使用 JAVA 调用 WMI的好库,包括 、 和 。其中,我更喜欢J-Interop,因为它是完全免费和开源的API。它提供了没有任何依赖的纯DCOM桥,完全用Java编写的没有任何JNI代码。

使用WMI管理Windows服务

现在,来看一个使用JAVA调用WMI的例子。这个例子利用J-Interop的API使用Win32_Service类解释WMI操作,将启动和停止在这个例子中的窗口服务。

步骤1:连接到WBEM服务

下面的代码示例显示了使用J-Interop如何初始化DCOM会话,并连接到远程DCOM服务使。它使用SWbemLocator对象连接到SWbemServicesSWbemServices对象提供对本地或远程计算机WMI的访问,它调用“ConnectServer”方法连接到SWbemServices。在本例中,提供管理员级别的用户连接到远程计算机。

第2步:获取Win32_Service实例

一旦你获得对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{
 
         privatestaticStringdomainName="";
         privatestaticStringuserName="administrator";
         privatestaticStringpassword="";
         privatestaticStringhostIP="127.0.0.1";
         privatestaticfinalStringwin32_namespace="ROOT\\CIMV2";
 
         privatestaticfinalintSTOP_SERVICE=0;
         privatestaticfinalintSTART_SERVICE=1;
 
         privateJISessiondcomSession=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");
                          IJIComObjectenumComObject=newEnumvariant.getObjectAsComObject();
                   IJIEnumVariantenumVariant=(IJIEnumVariant)narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
 
                          Object[]elements=enumVariant.next(1);
                          JIArrayaJIArray=(JIArray)elements[0];
 
                          JIVariant[]array=(JIVariant[])aJIArray.getArrayInstance();
                          for(JIVariantvariant:array){
                                   IJIDispatchwbemObjectDispatch=(IJIDispatch)narrowObject(variant.getObjectAsComObject());
 
                                   //Printobjectastext.
                                   JIVariant[]v=wbemObjectDispatch.callMethodA("GetObjectText_",newObject[]{1});
      
Top