Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

Java Source Code


Welcome to Dream.In.Code
Become a Java Expert!

Join 340,115 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 4,300 people online right now. Registration is fast and FREE... Join Now!




WMI Service Query with JACOB

use the JACOB COM bridge to query the WMI interface to determine which services are currently started on a computer

Submitted By: NickDMax
Actions:
Rating:
Views: 2,137

Language: Java

Last Modified: March 24, 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


  1. import com.jacob.activeX.ActiveXComponent;
  2. import com.jacob.com.Dispatch;
  3. import com.jacob.com.EnumVariant;
  4. import com.jacob.com.Variant;
  5.  
  6. /**
  7. * Quick little example of using JACOB (an open source Java-COM bridge) to access
  8. * the windows WMI interface.
  9. * <br>
  10. * To use this snippet you will need to download and install JACOB. This is relatively easy:
  11. * I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
  12. *
  13. * @author NickDMax (at) DreamInCode
  14. */
  15. public class ListServices {
  16.  
  17.         /**
  18.         * List the services currently running on the host computer.
  19.         *
  20.         * @param args
  21.         */
  22.         public static void main(String[] args) {
  23.                 String host = "localhost"; //Technically you should be able to connect to other hosts, but it takes setup
  24.                 String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
  25.                 String query = "SELECT * FROM Win32_Service WHERE started = 1"; //Started = 1 means the service is running.
  26.                 ActiveXComponent axWMI = new ActiveXComponent(connectStr);
  27.                 //Execute the query
  28.                 Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
  29.                
  30.                 //Our result is a collection, so we need to work though the.
  31.                 EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
  32.                 Dispatch item = null;
  33.                 while (enumVariant.hasMoreElements()) {
  34.                         item = enumVariant.nextElement().toDispatch();
  35.                         //Dispatch.call returns a Variant which we can convert to a java form.
  36.                         String serviceName = Dispatch.call(item,"Name").toString();
  37.                         String servicePath = Dispatch.call(item,"PathName").toString();
  38.                         int servicePID = Dispatch.call(item,"ProcessId").getInt();
  39.                         System.out.printf("[%5d] %-25s:\t%s\n",servicePID ,serviceName,servicePath);
  40.                 }
  41.         }
  42.        
  43. }

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month