Start/Stop services using JNA

Viewed 3430

I am writing a utility to start and stop windows services. The program will be distributed across many computers with differing levels of user privileges so I don't want to use the command line. I've tried using JNA,

import com.sun.jna.platform.win32.W32Service;
import com.sun.jna.platform.win32.W32ServiceManager;
import com.sun.jna.platform.win32.Winsvc;

/**
 *
 * @author 
 */
public class WindowsServices {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      try
      {

        // TODO code application logic here
         W32ServiceManager serviceManager = new W32ServiceManager();

        W32Service service = serviceManager.openService("uvnc_service", Winsvc.SERVICE_ACCEPT_STOP);
        service.stopService();
        service.close();   
      }
      catch (Exception ex)
      {
          ex.printStackTrace();
      }


    }
}

When I run the program I get the following error

com.sun.jna.platform.win32.Win32Exception: The handle is invalid. at com.sun.jna.platform.win32.W32ServiceManager.openService(W32ServiceManager.java:77) at windowsservices.WindowsServices.main(WindowsServices.java:26)

Any suggestions would be most helpful.

2 Answers
Related