Package com.sun.jna.ptr

Examples of com.sun.jna.ptr.IntByReference


   * @param value
   *            Name of the value to retrieve.
   * @return Integer value.
   */
  public static int registryGetIntValue(HKEY hKey, String value) {
    IntByReference lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    int rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
        lpType, (char[]) null, lpcbData);
    if (rc != W32Errors.ERROR_SUCCESS
        && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
      throw new Win32Exception(rc);
    }
    if (lpType.getValue() != WinNT.REG_DWORD) {
      throw new RuntimeException("Unexpected registry type "
          + lpType.getValue() + ", expected REG_DWORD");
    }
    IntByReference data = new IntByReference();
    rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
        lpType, data, lpcbData);
    if (rc != W32Errors.ERROR_SUCCESS
        && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
      throw new Win32Exception(rc);
    }
    return data.getValue();
  }
View Full Code Here


   * @param value
   *            Name of the value to retrieve.
   * @return Integer value.
   */
  public static long registryGetLongValue(HKEY hKey, String value) {
    IntByReference lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    int rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
        lpType, (char[]) null, lpcbData);
    if (rc != W32Errors.ERROR_SUCCESS
        && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
      throw new Win32Exception(rc);
    }
    if (lpType.getValue() != WinNT.REG_QWORD) {
      throw new RuntimeException("Unexpected registry type "
          + lpType.getValue() + ", expected REG_QWORD");
    }
    LongByReference data = new LongByReference();
    rc = Advapi32.INSTANCE.RegQueryValueEx(hKey, value, 0,
        lpType, data, lpcbData);
    if (rc != W32Errors.ERROR_SUCCESS
View Full Code Here

   * @return Object value.
   */
  public static Object registryGetValue(HKEY hkKey, String subKey,
      String lpValueName) {
    Object result = null;
    IntByReference lpType = new IntByReference();
    byte[] lpData = new byte[Advapi32.MAX_VALUE_NAME];
    IntByReference lpcbData = new IntByReference(Advapi32.MAX_VALUE_NAME);

    int rc = Advapi32.INSTANCE.RegGetValue(hkKey, subKey, lpValueName,
        Advapi32.RRF_RT_ANY, lpType, lpData, lpcbData);

    // if lpType == 0 then the value is empty (REG_NONE)!
    if (lpType.getValue() == WinNT.REG_NONE)
      return null;

    if (rc != W32Errors.ERROR_SUCCESS
        && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) {
      throw new Win32Exception(rc);
    }

    Memory byteData = new Memory(lpcbData.getValue());
    byteData.write(0, lpData, 0, lpcbData.getValue());

    if (lpType.getValue() == WinNT.REG_DWORD) {
      result = new Integer(byteData.getInt(0));
    } else if (lpType.getValue() == WinNT.REG_QWORD) {
      result = new Long(byteData.getLong(0));
    } else if (lpType.getValue() == WinNT.REG_BINARY) {
      result = byteData.getByteArray(0, lpcbData.getValue());
    } else if ((lpType.getValue() == WinNT.REG_SZ)
        || (lpType.getValue() == WinNT.REG_EXPAND_SZ)) {
      result = byteData.getWideString(0);
    }

View Full Code Here

   *            Key name.
   * @return True if the key was created, false otherwise.
   */
  public static boolean registryCreateKey(HKEY hKey, String keyName) {
    HKEYByReference phkResult = new HKEYByReference();
    IntByReference lpdwDisposition = new IntByReference();
    int rc = Advapi32.INSTANCE.RegCreateKeyEx(hKey, keyName, 0, null,
        WinNT.REG_OPTION_NON_VOLATILE, WinNT.KEY_READ, null, phkResult,
        lpdwDisposition);
    if (rc != W32Errors.ERROR_SUCCESS) {
      throw new Win32Exception(rc);
    }
    rc = Advapi32.INSTANCE.RegCloseKey(phkResult.getValue());
    if (rc != W32Errors.ERROR_SUCCESS) {
      throw new Win32Exception(rc);
    }
    return WinNT.REG_CREATED_NEW_KEY == lpdwDisposition.getValue();
  }
View Full Code Here

   * @param hKey
   *            Registry key.
   * @return Array of registry key names.
   */
  public static String[] registryGetKeys(HKEY hKey) {
    IntByReference lpcSubKeys = new IntByReference();
    IntByReference lpcMaxSubKeyLen = new IntByReference();
    int rc = Advapi32.INSTANCE
        .RegQueryInfoKey(hKey, null, null, null, lpcSubKeys,
            lpcMaxSubKeyLen, null, null, null, null, null, null);
    if (rc != W32Errors.ERROR_SUCCESS) {
      throw new Win32Exception(rc);
    }
    ArrayList<String> keys = new ArrayList<String>(lpcSubKeys.getValue());
    char[] name = new char[lpcMaxSubKeyLen.getValue() + 1];
    for (int i = 0; i < lpcSubKeys.getValue(); i++) {
      IntByReference lpcchValueName = new IntByReference(
          lpcMaxSubKeyLen.getValue() + 1);
      rc = Advapi32.INSTANCE.RegEnumKeyEx(hKey, i, name, lpcchValueName,
          null, null, null, null);
      if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
View Full Code Here

   * @param hKey
   *            Registry key.
   * @return Table of values.
   */
  public static TreeMap<String, Object> registryGetValues(HKEY hKey) {
    IntByReference lpcValues = new IntByReference();
    IntByReference lpcMaxValueNameLen = new IntByReference();
    IntByReference lpcMaxValueLen = new IntByReference();
    int rc = Advapi32.INSTANCE.RegQueryInfoKey(hKey, null, null, null,
        null, null, null, lpcValues, lpcMaxValueNameLen,
        lpcMaxValueLen, null, null);
    if (rc != W32Errors.ERROR_SUCCESS) {
      throw new Win32Exception(rc);
    }
    TreeMap<String, Object> keyValues = new TreeMap<String, Object>();
    char[] name = new char[lpcMaxValueNameLen.getValue() + 1];
    byte[] data = new byte[lpcMaxValueLen.getValue()];
    for (int i = 0; i < lpcValues.getValue(); i++) {
      IntByReference lpcchValueName = new IntByReference(
          lpcMaxValueNameLen.getValue() + 1);
      IntByReference lpcbData = new IntByReference(
          lpcMaxValueLen.getValue());
      IntByReference lpType = new IntByReference();
      rc = Advapi32.INSTANCE.RegEnumValue(hKey, i, name, lpcchValueName,
          null, lpType, data, lpcbData);
      if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
      }

      String nameString = Native.toString(name);

      if (lpcbData.getValue() == 0) {
        switch (lpType.getValue()) {
        case WinNT.REG_BINARY: {
          keyValues.put(nameString, new byte[0]);
          break;
        }
        case WinNT.REG_SZ:
        case WinNT.REG_EXPAND_SZ: {
          keyValues.put(nameString, new char[0]);
          break;
        }
        case WinNT.REG_MULTI_SZ: {
          keyValues.put(nameString, new String[0]);
          break;
        }
        case WinNT.REG_NONE: {
          keyValues.put(nameString, null);
          break;
        }
        default:
          throw new RuntimeException("Unsupported empty type: "
              + lpType.getValue());
        }
        continue;
      }

      Memory byteData = new Memory(lpcbData.getValue());
      byteData.write(0, data, 0, lpcbData.getValue());

      switch (lpType.getValue()) {
      case WinNT.REG_QWORD: {
        keyValues.put(nameString, byteData.getLong(0));
        break;
      }
      case WinNT.REG_DWORD: {
        keyValues.put(nameString, byteData.getInt(0));
        break;
      }
      case WinNT.REG_SZ:
      case WinNT.REG_EXPAND_SZ: {
        keyValues.put(nameString, byteData.getWideString(0));
        break;
      }
      case WinNT.REG_BINARY: {
        keyValues.put(nameString,
            byteData.getByteArray(0, lpcbData.getValue()));
        break;
      }
      case WinNT.REG_MULTI_SZ: {
        Memory stringData = new Memory(lpcbData.getValue());
        stringData.write(0, data, 0, lpcbData.getValue());
        ArrayList<String> result = new ArrayList<String>();
        int offset = 0;
        while (offset < stringData.size()) {
          String s = stringData.getWideString(offset);
          offset += s.length() * Native.WCHAR_SIZE;
          offset += Native.WCHAR_SIZE;
          if (s.length() == 0 && offset == stringData.size()) {
            // skip the final NULL
          } else {
            result.add(s);
          }
        }
        keyValues.put(nameString, result.toArray(new String[0]));
        break;
      }
      default:
        throw new RuntimeException("Unsupported type: "
            + lpType.getValue());
      }
    }
    return keyValues;
  }
View Full Code Here

    Memory memory = null;

    do {
      repeat = false;
      memory = new Memory(nLength);
      IntByReference lpnSize = new IntByReference();
      boolean succeded = Advapi32.INSTANCE.GetFileSecurity(new WString(
          fileName), infoType, memory, nLength, lpnSize);

      if (!succeded) {
        int lastError = Kernel32.INSTANCE.GetLastError();
        memory.clear();
        if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
          throw new Win32Exception(lastError);
        }
      }
      int lengthNeeded = lpnSize.getValue();
      if (nLength < lengthNeeded) {
        repeat = true;
        nLength = lengthNeeded;
        memory.clear();
      }
View Full Code Here

    private static Memory getSecurityDescriptorForFile(final String absoluteFilePath) {
        final int infoType = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
                DACL_SECURITY_INFORMATION;

        final IntByReference lpnSize = new IntByReference();
        boolean succeeded = Advapi32.INSTANCE.GetFileSecurity(
                new WString(absoluteFilePath),
                infoType,
                null,
                0, lpnSize);

        if (!succeeded) {
            final int lastError = Kernel32.INSTANCE.GetLastError();
            if (W32Errors.ERROR_INSUFFICIENT_BUFFER != lastError) {
                throw new Win32Exception(lastError);
            }
        }

        final int nLength = lpnSize.getValue();
        final Memory securityDescriptorMemoryPointer = new Memory(nLength);
        succeeded = Advapi32.INSTANCE.GetFileSecurity(new WString(
                absoluteFilePath), infoType, securityDescriptorMemoryPointer, nLength, lpnSize);

        if (!succeeded) {
View Full Code Here

    public InfoKey() {
    }

    public InfoKey(HKEY hKey, int securityDescriptor) {
      this.hKey = hKey;
      this.lpcbSecurityDescriptor = new IntByReference(securityDescriptor);
    }
View Full Code Here

      // finished or bytes remain, don't read any new data
      if (_done || _dwRead > 0) {
        return false;
      }

      IntByReference pnBytesRead = new IntByReference();
      IntByReference pnMinNumberOfBytesNeeded = new IntByReference();

      if (!Advapi32.INSTANCE
          .ReadEventLog(_h, WinNT.EVENTLOG_SEQUENTIAL_READ | _flags,
              0, _buffer, (int) _buffer.size(), pnBytesRead,
              pnMinNumberOfBytesNeeded)) {

        int rc = Kernel32.INSTANCE.GetLastError();

        // not enough bytes in the buffer, resize
        if (rc == W32Errors.ERROR_INSUFFICIENT_BUFFER) {
          _buffer = new Memory(pnMinNumberOfBytesNeeded.getValue());

          if (!Advapi32.INSTANCE.ReadEventLog(_h,
              WinNT.EVENTLOG_SEQUENTIAL_READ | _flags, 0,
              _buffer, (int) _buffer.size(), pnBytesRead,
              pnMinNumberOfBytesNeeded)) {
View Full Code Here

TOP

Related Classes of com.sun.jna.ptr.IntByReference

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.