Пример #1
0
  /**
   * Install the service.
   *
   * @return true on success
   * @param displayName visible name
   * @param description description
   * @param dependencies array of other services to depend on or null
   * @param account service account or null for LocalSystem
   * @param password password for service account or null
   * @param command command line to start the service
   * @throws java.lang.Exception
   */
  public boolean install(
      String displayName,
      String description,
      String[] dependencies,
      String account,
      String password,
      String command) {
    Advapi32 advapi32;
    Advapi32.SERVICE_DESCRIPTION desc;
    SC_HANDLE service, serviceManager;
    boolean success = false;
    String dep = "";

    if (dependencies != null) {
      for (String s : dependencies) {
        dep += s + "\0";
      }
    }
    dep += "\0";

    desc = new Advapi32.SERVICE_DESCRIPTION();
    desc.lpDescription = description;

    advapi32 = Advapi32.INSTANCE;
    serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);

    if (serviceManager != null) {
      service =
          advapi32.CreateService(
              serviceManager,
              serviceName,
              displayName,
              Winsvc.SERVICE_ALL_ACCESS,
              WinNT.SERVICE_WIN32_OWN_PROCESS,
              WinNT.SERVICE_DEMAND_START,
              WinNT.SERVICE_ERROR_NORMAL,
              command,
              null,
              null,
              dep,
              account,
              password);

      if (service != null) {
        success = advapi32.ChangeServiceConfig2(service, Winsvc.SERVICE_CONFIG_DESCRIPTION, desc);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }
    return (success);
  }
Пример #2
0
  /**
   * Uninstall the service.
   *
   * @throws java.lang.Exception
   * @return true on success
   */
  public boolean uninstall() {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    boolean success = false;

    advapi32 = Advapi32.INSTANCE;
    serviceManager = openServiceControlManager(null, Winsvc.SC_MANAGER_ALL_ACCESS);

    if (serviceManager != null) {
      service = advapi32.OpenService(serviceManager, serviceName, Winsvc.SERVICE_ALL_ACCESS);

      if (service != null) {
        success = advapi32.DeleteService(service);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }
    return (success);
  }
Пример #3
0
  /**
   * Ask the ServiceControlManager to start the service.
   *
   * @return true on success
   */
  public boolean start() {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    boolean success = false;

    advapi32 = Advapi32.INSTANCE;

    serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE);

    if (serviceManager != null) {
      service = advapi32.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE);

      if (service != null) {
        success = advapi32.StartService(service, 0, null);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }

    return (success);
  }
Пример #4
0
  /**
   * Ask the ServiceControlManager to stop the service.
   *
   * @return true on success
   */
  public boolean stop() throws Exception {
    Advapi32 advapi32;
    SC_HANDLE serviceManager, service;
    SERVICE_STATUS serviceStatus;
    boolean success = false;

    advapi32 = Advapi32.INSTANCE;

    serviceManager = openServiceControlManager(null, WinNT.GENERIC_EXECUTE);

    if (serviceManager != null) {
      service = advapi32.OpenService(serviceManager, serviceName, WinNT.GENERIC_EXECUTE);

      if (service != null) {
        serviceStatus = new SERVICE_STATUS();
        success = advapi32.ControlService(service, Winsvc.SERVICE_CONTROL_STOP, serviceStatus);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }

    return (success);
  }