What's Here?
- Members: 306,849
- Replies: 841,330
- Topics: 140,572
- Snippets: 4,465
- Tutorials: 1,166
- Total Online: 1,664
- Members: 126
- Guests: 1,538
|
use the JACOB COM bridge to query the WMI interface to determine which services are currently started on a computer
|
Submitted By: NickDMax
|
|
Rating:
   
|
|
Views: 1,614 |
Language: Java
|
|
Last Modified: March 23, 2009 |
|
Instructions: TO use this you must download and install JACOB form sourceforge.net. Then you must add the jacob.jar and the appropriate DLL file to you classpath. |
Snippet
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
/**
* Quick little example of using JACOB (an open source Java-COM bridge) to access
* the windows WMI interface.
* <br>
* To use this snippet you will need to download and install JACOB. This is relatively easy:
* I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
*
* @author NickDMax (at) DreamInCode
*/
public class ListServices {
/**
* List the services currently running on the host computer.
*
* @param args
*/
public static void main (String[] args ) {
String host = "localhost"; //Technically you should be able to connect to other hosts, but it takes setup
String connectStr = String. format("winmgmts:\\\\%s\\root\\CIMV2", host );
String query = "SELECT * FROM Win32_Service WHERE started = 1"; //Started = 1 means the service is running.
ActiveXComponent axWMI = new ActiveXComponent(connectStr);
//Execute the query
Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
//Our result is a collection, so we need to work though the.
EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
Dispatch item = null;
while (enumVariant.hasMoreElements()) {
item = enumVariant.nextElement().toDispatch();
//Dispatch.call returns a Variant which we can convert to a java form.
String serviceName = Dispatch. call(item, "Name"). toString();
String servicePath = Dispatch. call(item, "PathName"). toString();
int servicePID = Dispatch.call(item,"ProcessId").getInt();
System. out. printf("[%5d] %-25s:\t%s\n",servicePID ,serviceName,servicePath );
}
}
}
Copy & Paste
|
|
|
|