Package jnacontrib.jna

Examples of jnacontrib.jna.Advapi32$HandlerEx


   *            key name
   * @return array with all sub key names
   */
  public static String[] getSubKeys(REGISTRY_ROOT_KEY rootKey, String parent)
  {
    Advapi32 advapi32;
    int handle = 0, dwIndex;
    char[] lpName;
    IntByReference lpcName;
    WINBASE.FILETIME lpftLastWriteTime;
    TreeSet<String> subKeys = new TreeSet<String>();

    advapi32 = Advapi32.INSTANCE;
    handle = openKey(rootKey, parent, WINNT.KEY_READ);
    lpName = new char[256];
    lpcName = new IntByReference(256);
    lpftLastWriteTime = new WINBASE.FILETIME();

    if (handle != 0)
    {
      dwIndex = 0;

      while (advapi32.RegEnumKeyEx(handle, dwIndex, lpName, lpcName, null, null, null, lpftLastWriteTime) == WINERROR.ERROR_SUCCESS)
      {
        subKeys.add(new String(lpName, 0, lpcName.getValue()));
        lpcName.setValue(256);
        dwIndex++;
      }
      advapi32.RegCloseKey(handle);
    }

    return (subKeys.toArray(new String[]
    {}));
  }
View Full Code Here


   *             on error
   * @return TreeMap with name and value pairs
   */
  public static TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) throws UnsupportedEncodingException
  {
    Advapi32 advapi32;
    int handle = 0, dwIndex, result = 0;
    char[] lpValueName;
    byte[] lpData;
    IntByReference lpcchValueName, lpType, lpcbData;
    String name;
    TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);

    advapi32 = Advapi32.INSTANCE;
    handle = openKey(rootKey, key, WINNT.KEY_READ);
    lpValueName = new char[16384];
    lpcchValueName = new IntByReference(16384);
    lpType = new IntByReference();
    lpData = new byte[1];
    lpcbData = new IntByReference();

    if (handle != 0)
    {
      dwIndex = 0;

      do
      {
        lpcbData.setValue(0);
        result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);

        if (result == WINERROR.ERROR_MORE_DATA)
        {
          lpData = new byte[lpcbData.getValue()];
          lpcchValueName = new IntByReference(16384);
          result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);

          if (result == WINERROR.ERROR_SUCCESS)
          {
            name = new String(lpValueName, 0, lpcchValueName.getValue());

            switch (lpType.getValue())
            {
            case WINNT.REG_SZ:
              values.put(name, convertBufferToString(lpData));
              break;
            case WINNT.REG_DWORD:
              values.put(name, convertBufferToInt(lpData));
              break;
            default:
              break;
            }
          }
        }
        dwIndex++;
      }
      while (result == WINERROR.ERROR_SUCCESS);

      advapi32.RegCloseKey(handle);
    }
    return (values);
  }
View Full Code Here

   * @throws java.lang.Exception
   */
  public boolean install(String displayName, String description, String[] dependencies, String account, String password, String command,
      String startType, boolean interactive)
  {
    Advapi32 advapi32;
    Advapi32.SERVICE_DESCRIPTION desc;
    Pointer serviceManager, service;
    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);

    int winStartType = "DEMAND_START".equals(startType) ? WINSVC.SERVICE_DEMAND_START : WINSVC.SERVICE_AUTO_START;
    int dwServiceType = WINSVC.SERVICE_WIN32_OWN_PROCESS;
    if (interactive)
      dwServiceType |= WINSVC.SERVICE_INTERACTIVE_PROCESS;
    if (serviceManager != null)
    {
      service = advapi32.CreateService(serviceManager, serviceName, displayName, WINSVC.SERVICE_ALL_ACCESS, dwServiceType, winStartType,
          WINSVC.SERVICE_ERROR_NORMAL, command, null, null, dep, account, password);

      if (service != null)
      {
        success = advapi32.ChangeServiceConfig2(service, WINSVC.SERVICE_CONFIG_DESCRIPTION, desc);
        advapi32.CloseServiceHandle(service);
      }
      else
      {
        int err = Kernel32.INSTANCE.GetLastError();
        System.out.println("error during install " + err);
        System.out.println(Kernel32Util.formatMessageFromLastErrorCode(err));
      }

      advapi32.CloseServiceHandle(serviceManager);
    }
    return (success);
  }
View Full Code Here

   * @throws java.lang.Exception
   * @return true on success
   */
  public boolean uninstall()
  {
    Advapi32 advapi32;
    Pointer 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);
  }
View Full Code Here

  public static ServiceInfo serviceInfo(String name)
  {
    ServiceInfoImpl result = new ServiceInfoImpl();
    result.setName(name);

    Advapi32 advapi32;
    Pointer serviceManager, service;
    int state = Service.STATE_UNKNOWN;

    advapi32 = Advapi32.INSTANCE;

    serviceManager = openServiceControlManager(null, WINNT.GENERIC_READ);

    if (serviceManager != null)
    {
      state = 0;
      service = advapi32.OpenService(serviceManager, name, WINNT.GENERIC_READ);

      if (service != null)
      {
        IntByReference pcbBytesNeeded = new IntByReference();
        state |= Service.STATE_INSTALLED;
        // get size required
        if (!advapi32.QueryServiceConfig(service, null, 0, pcbBytesNeeded))
        {
          // now get the data
          int cbBufSize = pcbBytesNeeded.getValue();
          Memory buffer = new Memory(cbBufSize);
          buffer.clear();
          if (advapi32.QueryServiceConfig(service, buffer, cbBufSize, pcbBytesNeeded))
          {
            QUERY_SERVICE_CONFIG lpServiceConfig = new QUERY_SERVICE_CONFIG();
            lpServiceConfig.init(buffer);
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DISABLED)
              state |= Service.STATE_DISABLED;
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_BOOT_START | lpServiceConfig.dwStartType == Advapi32.SERVICE_SYSTEM_START
                | lpServiceConfig.dwStartType == Advapi32.SERVICE_AUTO_START)
              state |= Service.STATE_AUTOMATIC;
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DEMAND_START)
              state |= Service.STATE_MANUAL;
            if ((lpServiceConfig.dwServiceType & Advapi32.SERVICE_INTERACTIVE_PROCESS) != 0)
              state |= Service.STATE_INTERACTIVE;
            result.setAccount(lpServiceConfig.lpServiceStartName);
            result.setCommand(lpServiceConfig.lpBinaryPathName);
            result.setDependencies(lpServiceConfig.getDependencies());
            result.setDisplayName(lpServiceConfig.lpDisplayName);

          }
          else
          {
            state |= Service.STATE_UNKNOWN;
            System.out.println("Error in QueryServiceConfig: " + Native.getLastError());
          }
        }
        else
        {
          state |= Service.STATE_UNKNOWN;
          System.out.println("Error in QueryServiceConfig: " + Native.getLastError());
        }
        if (!advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, null, 0, pcbBytesNeeded))
        {
          // now get the data
          int cbBufSize = pcbBytesNeeded.getValue();
          Memory buffer = new Memory(cbBufSize);
          buffer.clear();
          if (advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, buffer, cbBufSize, pcbBytesNeeded))
          {
            SERVICE_STATUS_PROCESS lpBuffer = new SERVICE_STATUS_PROCESS();
            lpBuffer.init(buffer);
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_RUNNING)
              state |= Service.STATE_RUNNING;
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_PAUSED)
              state |= Service.STATE_PAUSED;
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_START_PENDING)
              state |= Service.STATE_STARTING;
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_STOP_PENDING)
              state |= Service.STATE_STOPPING;
            result.setPid(lpBuffer.dwProcessId);
          }
          else
          {
            state |= Service.STATE_UNKNOWN;
            System.out.println("Error in QueryServiceStatusEx: " + Native.getLastError());
          }
        }
        if (!advapi32.QueryServiceConfig2(service, (byte) advapi32.SERVICE_CONFIG_DESCRIPTION, null, 0, pcbBytesNeeded))
        {
          // now get the data
          int cbBufSize = pcbBytesNeeded.getValue();
          Memory buffer = new Memory(cbBufSize);
          buffer.clear();
          if (advapi32.QueryServiceConfig2(service, (byte) advapi32.SERVICE_CONFIG_DESCRIPTION, buffer, cbBufSize, pcbBytesNeeded))
          {
            SERVICE_DESCRIPTION lpBuffer = new SERVICE_DESCRIPTION();
            lpBuffer.init(buffer);
            result.setDescription(lpBuffer.lpDescription);
          }
          else
          {
            state |= Service.STATE_UNKNOWN;
            System.out.println("Error in QueryServiceStatusEx: " + Native.getLastError());
          }
        }

        else
        {
          state |= Service.STATE_UNKNOWN;
          System.out.println("Error in QueryServiceStatusEx: " + Native.getLastError());
        }

        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }
    result.setState(state);

    return result;
View Full Code Here

  }

  public int state()
  {
    Advapi32 advapi32;
    Pointer serviceManager, service;
    int result = Service.STATE_UNKNOWN;

    advapi32 = Advapi32.INSTANCE;

    serviceManager = openServiceControlManager(null, WINNT.GENERIC_READ);
    // System.out.println("Win32Service.state() serviceManager "+serviceManager);

    if (serviceManager != null)
    {
      result = 0;
      service = advapi32.OpenService(serviceManager, serviceName, WINNT.GENERIC_READ);
      // System.out.println("Win32Service.state() service "+service);

      if (service != null)
      {
        IntByReference pcbBytesNeeded = new IntByReference();
        result |= Service.STATE_INSTALLED;
        // get size required
        if (!advapi32.QueryServiceConfig(service, null, 0, pcbBytesNeeded))
        {
          // now get the data
          int cbBufSize = pcbBytesNeeded.getValue();
          if (cbBufSize > 8192)
            cbBufSize = 8192;
          Memory buffer = new Memory(cbBufSize);
          buffer.clear();
          if (advapi32.QueryServiceConfig(service, buffer, cbBufSize, pcbBytesNeeded))
          {
            QUERY_SERVICE_CONFIG lpServiceConfig = new QUERY_SERVICE_CONFIG();
            lpServiceConfig.init(buffer);
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DISABLED)
              result |= Service.STATE_DISABLED;
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_BOOT_START | lpServiceConfig.dwStartType == Advapi32.SERVICE_SYSTEM_START
                | lpServiceConfig.dwStartType == Advapi32.SERVICE_AUTO_START)
              result |= Service.STATE_AUTOMATIC;
            if (lpServiceConfig.dwStartType == Advapi32.SERVICE_DEMAND_START)
              result |= Service.STATE_MANUAL;
            if ((lpServiceConfig.dwServiceType & Advapi32.SERVICE_INTERACTIVE_PROCESS) != 0)
              result |= Service.STATE_INTERACTIVE;

          }
          else
          {
            result |= Service.STATE_UNKNOWN;
            int error = Native.getLastError();
            System.out.println("Error getting buffer size in QueryServiceConfig: " + error + " " + Kernel32Util.formatMessageFromLastErrorCode(error));
          }
        }
        else
        {
          result |= Service.STATE_UNKNOWN;
          int error = Native.getLastError();
          System.out.println("Error in QueryServiceConfig: " + error + " " + Kernel32Util.formatMessageFromLastErrorCode(error));
        }
        if (!advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, null, 0, pcbBytesNeeded))
        {
          // now get the data
          int cbBufSize = pcbBytesNeeded.getValue();
          Memory buffer = new Memory(cbBufSize);
          buffer.clear();
          if (advapi32.QueryServiceStatusEx(service, (byte) advapi32.SC_STATUS_PROCESS_INFO, buffer, cbBufSize, pcbBytesNeeded))
          {
            SERVICE_STATUS_PROCESS lpBuffer = new SERVICE_STATUS_PROCESS();
            lpBuffer.init(buffer);
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_RUNNING)
              result |= Service.STATE_RUNNING;
            if (lpBuffer.dwCurrentState == advapi32.SERVICE_PAUSED)
              result |= Service.STATE_PAUSED;
            // System.out.println("Win32Service.state() dwCurrentState "+lpBuffer.dwCurrentState);

          }
          else
          {
            result |= Service.STATE_UNKNOWN;
            int error = Native.getLastError();
            System.out.println("Error getting buffer size in QueryServiceStatusEx: " + error + " " + Kernel32Util.formatMessageFromLastErrorCode(error));
          }
        }
        else
        {
          result |= Service.STATE_UNKNOWN;
          int error = Native.getLastError();
          System.out.println("Error in QueryServiceStatusEx: " + error + " " + Kernel32Util.formatMessageFromLastErrorCode(error));
        }

        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }

    return result;

  }
View Full Code Here

   *
   * @return true on success
   */
  public boolean start()
  {
    Advapi32 advapi32;
    Pointer serviceManager, service;
    boolean success = false;

    advapi32 = Advapi32.INSTANCE;

    serviceManager = openServiceControlManager(null, WINNT.GENERIC_EXECUTE);
    // System.out.println("service.start() serviceManager "+serviceManager);

    if (serviceManager != null)
    {
      service = advapi32.OpenService(serviceManager, serviceName, WINNT.GENERIC_EXECUTE);
      // System.out.println("service.start() service "+service);

      if (service != null)
      {
        success = advapi32.StartService(service, 0, null);
        // System.out.println("service.start() StartService "+success);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }

    return (success);
  }
View Full Code Here

   *
   * @return true on success
   */
  public boolean stop() throws Exception
  {
    Advapi32 advapi32;
    Pointer serviceManager, service;
    Advapi32.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 Advapi32.SERVICE_STATUS();
        success = advapi32.ControlService(service, WINSVC.SERVICE_CONTROL_STOP, serviceStatus);
        advapi32.CloseServiceHandle(service);
      }
      advapi32.CloseServiceHandle(serviceManager);
    }

    return (success);
  }
View Full Code Here

  /**
   * Initialize the service, connect to the ServiceControlManager.
   */
  public void init()
  {
    Advapi32 advapi32;
    // Advapi32.SERVICE_TABLE_ENTRY[] entries = new
    // Advapi32.SERVICE_TABLE_ENTRY[2];
    Advapi32.SERVICE_TABLE_ENTRY entry;

    serviceMain = new ServiceMain();
    advapi32 = Advapi32.INSTANCE;
    entry = new Advapi32.SERVICE_TABLE_ENTRY();
    entry.size();
    entry.lpServiceName = serviceName;
    entry.lpServiceProc = serviceMain;
    entry.write();

    if (!advapi32.StartServiceCtrlDispatcher(entry.toArray(2)))
    {
      log("error in StartServiceCtrlDispatcher");
      int err = Native.getLastError();
      log(err + ":" + Kernel32Util.formatMessageFromLastErrorCode(err));
    }
View Full Code Here

   * @return handle to ServiceControlManager or null when failed
   */
  static private Pointer openServiceControlManager(String machine, int access)
  {
    Pointer handle = null;
    Advapi32 advapi32;

    advapi32 = Advapi32.INSTANCE;
    handle = advapi32.OpenSCManager(machine, null, access);
    if (handle == null)
    {
      int err = Native.getLastError();
      System.out.println("Error in OpenSCManager: " + Integer.toHexString(err));
      if (err == 5)
View Full Code Here

TOP

Related Classes of jnacontrib.jna.Advapi32$HandlerEx

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.