4

リモート コンピューターに関する WMI 情報を取得するために、java と Jacob を使用してリモート コンピューターに接続しようとしています。

localhost の場合、以下のコードを使用していますが、正常に動作します。

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

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

しかし、localhost を別の ip/hostname に変更すると、次のエラーが発生しました。

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

編集

を使用してユーザー名/パスワードを渡そうとしましたWbemScripting

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

1 に答える 1

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;
于 2013-07-18T09:44:31.183 に答える