2011-09-06 48 views
1

我是WMI的新手,並且使用J-interop lib通過Java進行COM通信...我需要從遠程系統獲取物理內存數據。通過Java使用J-Interop的WMI

我有下面的代碼:它提供了一些數據,但有一些缺陷在它就像我在我的系統2 GB內存,兩個內存卡1個GB每這段代碼只顯示我的1 GB的內存:

請幫我整頓下面的代碼和任何很好的參考與很好的例子,以遵循J-互操作性:

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(); 
    } 
} 
} 

回答

1

你需要獲得枚舉中元素的個數。看看MSIWMI2.java中的j-interop示例。這裏是一個枚舉枚舉的例子。

... 
results = wbemServices_dispatch.callMethodA("ExecQuery", new Object[]{new JIString("select * from Win32_OperatingSystem where Primary=True"), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(),JIVariant.OPTIONAL_PARAM()}); 
IJIDispatch wbemObjectSet_dispatch = (IJIDispatch)JIObjectFactory.narrowObject((results[0]).getObjectAsComObject()); 
JIVariant variant = wbemObjectSet_dispatch.get("_NewEnum"); 
IJIComObject object2 = variant.getObjectAsComObject(); 

IJIEnumVariant enumVARIANT = (IJIEnumVariant)JIObjectFactory.narrowObject(object2.queryInterface(IJIEnumVariant.IID)); 

JIVariant Count = wbemObjectSet_dispatch.get("Count"); 
int count = Count.getObjectAsInt(); 
for (int i = 0; i < count; i++) 
{ 
     Object[] values = enumVARIANT.next(1); 
     JIArray array = (JIArray)values[0]; 
     Object[] arrayObj = (Object[])array.getArrayInstance(); 
     for (int j = 0; j < arrayObj.length; j++) 
     { 
      IJIDispatch wbemObject_dispatch = (IJIDispatch)JIObjectFactory.narrowObject(((JIVariant)arrayObj[j]).getObjectAsComObject()); 
      JIVariant variant2 = (JIVariant)(wbemObject_dispatch.callMethodA("GetObjectText_",new Object[]{new Integer(1)}))[0]; 
      System.out.println(variant2.getObjectAsString().getString()); 
      System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); 
     } 
} 
... 

此代碼是從MSWMI2.java在org.jinterop.dcom.test包。

親切的問候

比約恩

+0

非常感謝比約恩...它的工作... :) ... – PSisHere

+0

@PSisHere:我們可以遠程使用這個庫? – HashimR

+0

是的,我從一臺Linux機器上運行java代碼來查詢帶有wmi的窗口機器。 –