Skip to content

Commit

Permalink
Sample based on sourceforge question and publcly posted code
Browse files Browse the repository at this point in the history
  • Loading branch information
clay_shooter committed Sep 27, 2007
1 parent cb2d44b commit a593a26
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions jacob/samples/com/jacob/samples/system/SystemMonitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.jacob.samples.system;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

/**
* Sample program that shows how to talk to WMI on local machine.
*
* This test program was derived from SourceForge question
* http://sourceforge.net/forum/forum.php?thread_id=1831650&forum_id=375946
* fold, spindled and mutilated by clay_shooter
*
* @author chris_knowles
*
*/
public class SystemMonitor {

public void runMonitor() {

ActiveXComponent wmi = null;
wmi = new ActiveXComponent("WbemScripting.SWbemLocator");
// no connection parameters means to connect to the local machine
Variant conRet = wmi.invoke("ConnectServer");
// the author liked the ActiveXComponent api style over the Dispatch style
ActiveXComponent wmiconnect = new ActiveXComponent(conRet.toDispatch());

// the WMI supports a query language.
String query = "select CategoryString, Message, TimeGenerated, User, Type "
+ "from Win32_NtLogEvent "
+ "where Logfile = 'Application' and TimeGenerated > '20070915000000.000000-***'";
Variant vCollection = wmiconnect
.invoke("ExecQuery", new Variant(query));

EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());

String resultString = "";
Dispatch item = null;

while (enumVariant.hasMoreElements()) {
resultString = "";
item = enumVariant.Next().toDispatch();
String categoryString = Dispatch.call(item, "CategoryString")
.toString();
String messageString = Dispatch.call(item, "Message").toString();
String timeGenerated = Dispatch.call(item, "TimeGenerated")
.toString();
String eventUser = Dispatch.call(item, "User").toString();
String eventType = Dispatch.call(item, "Type").toString();
resultString += "TimeGenerated: "+ timeGenerated
+ " Category: " + categoryString
+ " User: " + eventUser
+ " EventType: "+ eventType
+ " Message:" + messageString;
System.out.println(resultString);

}

}

public static void main(String[] args) {
SystemMonitor utilConnection = new SystemMonitor();
utilConnection.runMonitor();
}

}

0 comments on commit a593a26

Please sign in to comment.