2013-02-20 29 views
2

我正在嘗試使用j-interop來實現以下wmic命令。jinterop Win32_Process創建

wmic /NODE:192.168.0.195 /USER:Test /PASSWORD:password123 process call create "calc.exe" 

我有我的代碼寫在我的方法這樣。我有另外兩種創建會話並連接到WMI服務的方法,以便部分處理。

public void wmiExecute() throws JIException { 

    // Obtain Win32_Process and narrow it as IJIDispatch 
    Object[] params = new Object[] { 
     new JIString("Win32_Process"), 
     new Integer(0), 
     JIVariant.OPTIONAL_PARAM() 
    }; 
    JIVariant[] servicesSet = this._wbemServices.callMethodA("InstancesOf", params); 
    IJIDispatch wbemObjectSet = (IJIDispatch) JIObjectFactory.narrowObject(servicesSet[0].getObjectAsComObject()); 

    params = new Object[] { 
      "calc.exe", 
      JIVariant.OPTIONAL_PARAM(), 
      JIVariant.OPTIONAL_PARAM(), 
      new Integer(0), 
    }; 
    wbemObjectSet.callMethodA("Create", params); 
} 

我一直得到的

Caught Throwable: org.jinterop.dcom.common.JIException: Unknown name. [0x80020006] 
org.jinterop.dcom.common.JIException: Unknown name. [0x80020006] 

任何想法可能是錯誤的例外呢?提前致謝!

回答

3

這裏的解決方案......

你不應該使用instanceof來獲得Win32_Process的,因爲你會剛開當前運行的進程的列表。相反,您應該使用「獲取」來獲取默認的Win32_Process。

public void wmiExecute() throws JIException { 

    // Obtain Win32_Process and narrow it as IJIDispatch 
    Object[] params = new Object[] { 
     new JIString("Win32_Process"), 
     JIVariant.OPTIONAL_PARAM(), 
     JIVariant.OPTIONAL_PARAM() 
    }; 

    // Obtain the default Win32_Process 
    JIVariant[] service = this._wbemServices.callMethodA("Get", params); 

    // Convert it to a IJIDispatch object 
    IJIDispatch wbemObject = (IJIDispatch) JIObjectFactory.narrowObject(service[0].getObjectAsComObject()); 

    // Create input params 
    Object[] paramsCalc = new Object[] { 
      new JIString("calc.exe"), 
      JIVariant.OPTIONAL_PARAM(), 
      JIVariant.OPTIONAL_PARAM() 
    }; 

    // Create the calculator process 
    JIVariant[] results = wbemObject.callMethodA("Create", paramsCalc); 
}