public class JIntegra {
private static final String WMI_DEFAULT_NAMESPACE = "ROOT\\CIMV2";
private static JISession configAndConnectDCom( String domain, String user, String pass ) throws Exception
{
JISystem.getLogger().setLevel( Level.OFF );
try
{
JISystem.setInBuiltLogHandler( false );
}
catch ( IOException ignored )
{
}
JISystem.setAutoRegisteration( true );
JISession dcomSession = JISession.createSession( domain, user, pass );
dcomSession.useSessionSecurity( true );
return dcomSession;
}
private static IJIDispatch getWmiLocator( String host, JISession dcomSession ) throws Exception
{
JIComServer wbemLocatorComObj = new JIComServer(JIProgId.valueOf("WbemScripting.SWbemLocator"), host, dcomSession);
return (IJIDispatch) JIObjectFactory.narrowObject( wbemLocatorComObj.createInstance().queryInterface( IJIDispatch.IID ) );
}
private static IJIDispatch toIDispatch( JIVariant comObjectAsVariant ) throws JIException
{
return (IJIDispatch) JIObjectFactory.narrowObject( comObjectAsVariant.getObjectAsComObject() );
}
public static void main( String[] args )
{
String domain = "syntbots.com";
// String host = "syncgdc3244";
String host = "192.168.175.65";
String user = "VK5017063";
String pass = "apr@2016";
JISession dcomSession = null;
try
{
dcomSession = configAndConnectDCom( domain, user, pass );
IJIDispatch wbemLocator = getWmiLocator( host, dcomSession );
JIVariant results[] =
wbemLocator.callMethodA( "ConnectServer", new Object[] { new JIString( host ), new JIString( WMI_DEFAULT_NAMESPACE ),
JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), new Integer( 0 ),
JIVariant.OPTIONAL_PARAM() } );
IJIDispatch wbemServices = toIDispatch( results[ 0 ] );
final String QUERY_FOR_ALL_LOG_EVENTS = "SELECT * FROM Win32_OperatingSystem";
final int RETURN_IMMEDIATE = 16;
final int FORWARD_ONLY = 32;
JIVariant[] eventSourceSet =
wbemServices.callMethodA( "ExecNotificationQuery", new Object[] { new JIString( QUERY_FOR_ALL_LOG_EVENTS ), new JIString( "WQL" ),
new JIVariant( new Integer( RETURN_IMMEDIATE + FORWARD_ONLY ) ) } );
IJIDispatch wbemEventSource = (IJIDispatch) JIObjectFactory.narrowObject( ( eventSourceSet[ 0 ] ).getObjectAsComObject() );
while ( true )
{
JIVariant eventAsVariant = (JIVariant) ( wbemEventSource.callMethodA( "NextEvent", new Object[] { JIVariant.OPTIONAL_PARAM() } ) )[ 0 ];
IJIDispatch wbemEvent = toIDispatch( eventAsVariant );
JIVariant objTextAsVariant = (JIVariant) ( wbemEvent.callMethodA( "GetObjectText_", new Object[] { new Integer( 1 ) } ) )[ 0 ];
String asText = objTextAsVariant.getObjectAsString().getString();
System.out.println( asText );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
finally
{
if ( null != dcomSession )
{
try
{
JISession.destroySession( dcomSession );
}
catch ( Exception ex )
{
ex.printStackTrace();
}
}
}
}
}
请找到以下错误
org.jinterop.dcom.common.JIException: Message not found for errorCode: 0xC0000070
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(JIWinRegStub.java:115)
at org.jinterop.dcom.core.JIProgId.getIdFromWinReg(JIProgId.java:130)
at org.jinterop.dcom.core.JIProgId.getCorrespondingCLSID(JIProgId.java:162)
at org.jinterop.dcom.core.JIComServer.<init>(JIComServer.java:429)
at JIntegra.getWmiLocator(JIntegra.java:43)
at JIntegra.main(JIntegra.java:94)
Caused by: jcifs.smb.SmbAuthException: Logon failure: user not allowed to log on to this computer.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:510)
at jcifs.smb.SmbTransport.send(SmbTransport.java:622)
at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:280)
at jcifs.smb.SmbSession.send(SmbSession.java:233)
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:154)
at jcifs.smb.SmbFile.doConnect(SmbFile.java:847)
at jcifs.smb.SmbFile.connect(SmbFile.java:890)
at jcifs.smb.SmbFile.connect0(SmbFile.java:816)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:62)
at jcifs.smb.SmbFile.getInputStream(SmbFile.java:2765)
at rpc.ncacn_np.RpcTransport.attach(RpcTransport.java:91)
at rpc.Stub.attach(Stub.java:106)
at rpc.Stub.call(Stub.java:110)
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(JIWinRegStub.java:113)
答案 0 :(得分:0)
我找到了雅各布的方式
ActiveXComponent wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
Variant variantParameters[] = new Variant[4];
variantParameters[0] = new Variant("hostname");
variantParameters[1] = new Variant("root\\cimv2");
variantParameters[2] = new Variant("domain\\username");
variantParameters[3] = new Variant("password");
ActiveXComponent axWMI;
try
{
com.jacob.com.Variant conRet = wmi.invoke("ConnectServer", variantParameters);
axWMI = new ActiveXComponent(conRet.toDispatch());
}catch(ComFailException e)
{
e.printStackTrace();
axWMI = null;
}
if (axWMI == null)
{
System.out.println("Connection not established");
}
else
{
System.out.println("Connection established");
String query = "Select Name from Win32_ComputerSystem";
Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
String resultString = "";
Dispatch item = null;
item = enumVariant.nextElement().toDispatch();
String name = Dispatch.call(item, "Name").toString();
resultString += ( " name " + name);
System.out.println(resultString);
}