-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample based on sourceforge question and publcly posted code
- Loading branch information
clay_shooter
committed
Sep 27, 2007
1 parent
cb2d44b
commit a593a26
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |