2013-07-10 90 views
4

我試圖連接到遠程計算機使用java和雅各布,以獲得有關遠程計算機的一些WMI信息。雅各布連接到遠程計算機的WMI支持

對於本地主機我使用下面的代碼,它工作正常。

String host = "localhost"; 
    String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host); 

    ActiveXComponent axWMI = new ActiveXComponent(connectStr); 
    // other code to get system information 

但是,如果我本地主機切換到另一個IP /主機名,我得到了以下錯誤:

Exception in thread "main" com.jacob.com.ComFailException: Can't find moniker 
    at com.jacob.com.Dispatch.createInstanceNative(Native Method) 
    at com.jacob.com.Dispatch.<init>(Dispatch.java:99) 
    at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58) 
    at easyticket.classes.WmiExtended.main(WmiExtended.java:28) 

,並拋出異常的行是:

ActiveXComponent axWMI = new ActiveXComponent(connectStr); 

編輯

我嘗試通過使用傳遞用戶名/密碼

String host = "192.168.7.106"; 
ActiveXComponent axWMI = new ActiveXComponent("WbemScripting.SWbemLocator"); 
axWMI.invoke("ConnectServer", new Variant(host+",\"root\\cimv2\",\"username\",\"password\"")); 

,但我得到這個錯誤:

Exception in thread "main" com.jacob.com.ComFailException: Invoke of: ConnectServer 
Source: SWbemLocator 
Description: The RPC server is unavailable. 

我怎樣才能解決呢?我如何通過用戶名/密碼,如果需要域?

我使用的是Windows 8,我試圖連接到win8/win7/winxp/win2003server電腦。

回答

4

某些搜索我設法解決我的問題後...

下面的代碼,如果有人需要它。

ActiveXComponent wmi = new ActiveXComponent("WbemScripting.SWbemLocator");   

    Variant variantParameters[] = new Variant[4]; 
    variantParameters[0] = new Variant(_IPADDRESS); 
    variantParameters[1] = new Variant("root\\cimv2"); 
    variantParameters[2] = new Variant("username"); 
    variantParameters[3] = new Variant("password");  
    ActiveXComponent axWMI; 
try 
{ 
    Variant conRet = wmi.invoke("ConnectServer", variantParameters);   
    axWMI = new ActiveXComponent(conRet.toDispatch()); 
}catch(ComFailException e) 
{ 
    axWMI = null; 
} 
if (axWMI == null) 
    return false; 
相關問題