Jacob connect to Remote Computer for WMI support

青春壹個敷衍的年華 提交于 2019-12-10 15:15:48

问题


I'm trying to connect to a remote computer using java and Jacob in order to get some WMI Information about the remote computer.

For localhost I'm using the code below and it works fine.

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

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

But if I change localhost to another ip/hostname I got the following error:

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)

and the row that throws the exception is:

ActiveXComponent axWMI = new ActiveXComponent(connectStr);

EDIT

I tried passing username/password using WbemScripting

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

but I got this error:

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

How can I solve it? How can I pass username/password and if is needed the domain???

I'm using Windows 8 and I'm trying to connect to win8/win7/winxp/win2003server computers.


回答1:


After some searches I managed to solve my problem...

Here's the code if anyone need it.

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;


来源:https://stackoverflow.com/questions/17573374/jacob-connect-to-remote-computer-for-wmi-support

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!