我正在尝试获取硬件 ID Win32_SoundDevice
,Win32_VideoController
但获取 Win32_SoundDevice 实例会引发未知名称异常
这是我的代码
package com.az;
import static org.jinterop.dcom.core.JIProgId.valueOf;
import static org.jinterop.dcom.impls.JIObjectFactory.narrowObject;
import static org.jinterop.dcom.impls.automation.IJIDispatch.IID;
import java.awt.Label;
import java.util.logging.Level;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.common.JISystem;
import org.jinterop.dcom.core.IJIComObject;
import org.jinterop.dcom.core.JIArray;
import org.jinterop.dcom.core.JIComServer;
import org.jinterop.dcom.core.JISession;
import org.jinterop.dcom.core.JIString;
import org.jinterop.dcom.core.JIVariant;
import org.jinterop.dcom.impls.automation.IJIDispatch;
import org.jinterop.dcom.impls.automation.IJIEnumVariant;
public class PerformanceMonitor extends JApplet {
String domain = "az.com.us";
String hostname = "mc-us-az";
String username = "az";
String password = "test_az_";
public JIComServer comServer = null;
public JISession dcomSession = null;
public IJIDispatch wbemServices = null;
public Object[] params = null;
public static final String soundCardInfo = "Win32_SoundDevice";
public void setWindowsCredentials(String domain, String hostname, String username, String password) {
try {
dcomSession = init(domain, username, password);
comServer = new JIComServer(valueOf("WbemScripting.SWbemLocator"), hostname, dcomSession);
IJIDispatch wbemLocator = (IJIDispatch) narrowObject(comServer.createInstance().queryInterface(IID));
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);
wbemServices = (IJIDispatch) narrowObject(results[0].getObjectAsComObject());
getWMISoundCard(getJIVariants(wbemLocator, soundCardInfo));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dcomSession != null) {
try {
JISession.destroySession(dcomSession);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
private void getWMISoundCard(JIVariant[] diskMonit) {
try {
IJIDispatch wbemObjectSet = (IJIDispatch) narrowObject(diskMonit[0].getObjectAsComObject());
JIVariant newEnumvariant = wbemObjectSet.get("_NewEnum");
IJIComObject object2 = newEnumvariant.getObjectAsComObject();
IJIEnumVariant enumVARIANT = (IJIEnumVariant) narrowObject(object2.queryInterface(IJIEnumVariant.IID));
JIVariant countVariant = wbemObjectSet.get("Count");
int numberOfServices = countVariant.getObjectAsInt();
for (int i = 0; i < numberOfServices; i++) {
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());
JIVariant[] v = wbemObjectDispatch.callMethodA("GetObjectText_", new Object[] { 1 });
JIVariant deviceID = (JIVariant) (wbemObjectDispatch.get("DeviceID"));
System.out.println("SoundCard Device ID =" + deviceID.getObjectAsString2());
}
}
} catch (JIException e) {
e.printStackTrace();
}
}
public JIVariant[] getJIVariants(IJIDispatch wbemServices, String task) {
JIVariant[] arrJIVariant = null;
try {
params = new Object[] { new JIString(task), new Integer(0), JIVariant.OPTIONAL_PARAM() };
arrJIVariant = wbemServices.callMethodA("InstancesOf", params);
} catch (JIException e) {
e.printStackTrace();
}
return arrJIVariant;
}
private static JISession init(String domain, String user, String pass) throws Exception {
JISystem.getLogger().setLevel(Level.OFF);
JISystem.setAutoRegisteration(true);
JISession dcomSession = JISession.createSession(domain, user, pass);
dcomSession.useSessionSecurity(false);
return dcomSession;
}
public static void main(String[] args) {
PerformanceMonitor performanceMonitor = new PerformanceMonitor();
performanceMonitor.setWindowsCredentials(performanceMonitor.domain, performanceMonitor.hostname, performanceMonitor.username, performanceMonitor.password);
}
}
堆栈跟踪
org.jinterop.dcom.common.JIException: Unknown name. [0x80020006]
at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:910)
at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:856)
at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:266)
at org.jinterop.dcom.core.JIComObjectImpl.call(JIComObjectImpl.java:153)
at org.jinterop.dcom.impls.automation.JIDispatchImpl.getIDsOfNames(JIDispatchImpl.java:109)
at org.jinterop.dcom.impls.automation.JIDispatchImpl.callMethodA(JIDispatchImpl.java:477)
at com.az.PerformanceMonitor.getJIVariants(PerformanceMonitor.java:682)
at com.az.PerformanceMonitor.setWindowsCredentials(PerformanceMonitor.java:123)
at com.az.PerformanceMonitor.init(PerformanceMonitor.java:80)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.jinterop.dcom.common.JIRuntimeException: Unknown name. [0x80020006]
at org.jinterop.dcom.core.JICallBuilder.readResult(JICallBuilder.java:1079)
at org.jinterop.dcom.core.JICallBuilder.read(JICallBuilder.java:957)
at ndr.NdrObject.decode(NdrObject.java:36)
at rpc.ConnectionOrientedEndpoint.call(ConnectionOrientedEndpoint.java:137)
at rpc.Stub.call(Stub.java:113)
at org.jinterop.dcom.core.JIComServer.call(JIComServer.java:901)
... 10 more