Search in sources :

Example 21 with HKEY

use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.

the class Advapi32Util method registryGetValues.

/**
	 * Get a table of registry values.
	 *
	 * @param root
	 *            Registry root.
	 * @param keyPath
	 *            Regitry key path.
	 * @return Table of values.
	 */
public static TreeMap<String, Object> registryGetValues(HKEY root, String keyPath) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        return registryGetValues(phkKey.getValue());
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 22 with HKEY

use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.

the class Advapi32Util method registryValueExists.

/**
	 * Checks whether a registry value exists.
	 *
	 * @param root
	 *            HKEY_LOCAL_MACHINE, etc.
	 * @param key
	 *            Registry key path.
	 * @param value
	 *            Value name.
	 * @return True if the value exists.
	 */
public static boolean registryValueExists(HKEY root, String key, String value) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ, phkKey);
    try {
        switch(rc) {
            case W32Errors.ERROR_SUCCESS:
                break;
            case W32Errors.ERROR_FILE_NOT_FOUND:
                return false;
            default:
                throw new Win32Exception(rc);
        }
        IntByReference lpcbData = new IntByReference();
        IntByReference lpType = new IntByReference();
        rc = Advapi32.INSTANCE.RegQueryValueEx(phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData);
        switch(rc) {
            case W32Errors.ERROR_SUCCESS:
            case W32Errors.ERROR_MORE_DATA:
            case W32Errors.ERROR_INSUFFICIENT_BUFFER:
                return true;
            case W32Errors.ERROR_FILE_NOT_FOUND:
                return false;
            default:
                throw new Win32Exception(rc);
        }
    } finally {
        if (phkKey.getValue() != WinBase.INVALID_HANDLE_VALUE) {
            rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
            if (rc != W32Errors.ERROR_SUCCESS) {
                throw new Win32Exception(rc);
            }
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 23 with HKEY

use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.

the class NtDllUtil method getKeyName.

/**
	 * Retrieve the name of an opened registry key.
	 * @param hkey Opened registry key.
	 * @return Basic key name, not including node information.
	 */
public static String getKeyName(HKEY hkey) {
    IntByReference resultLength = new IntByReference();
    int rc = NtDll.INSTANCE.ZwQueryKey(hkey, KEY_INFORMATION_CLASS.KeyBasicInformation, null, 0, resultLength);
    if (rc != NTStatus.STATUS_BUFFER_TOO_SMALL || resultLength.getValue() <= 0) {
        throw new Win32Exception(rc);
    }
    KEY_BASIC_INFORMATION keyInformation = new KEY_BASIC_INFORMATION(resultLength.getValue());
    rc = NtDll.INSTANCE.ZwQueryKey(hkey, KEY_INFORMATION_CLASS.KeyBasicInformation, keyInformation, resultLength.getValue(), resultLength);
    if (rc != NTStatus.STATUS_SUCCESS) {
        throw new Win32Exception(rc);
    }
    return keyInformation.getName();
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) KEY_BASIC_INFORMATION(com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION)

Example 24 with HKEY

use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.

the class Advapi32UtilTest method registrySetEmptyValue.

private static void registrySetEmptyValue(HKEY root, String keyPath, String name, final int valueType) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    try {
        char[] data = new char[0];
        rc = Advapi32.INSTANCE.RegSetValueEx(phkKey.getValue(), name, 0, valueType, data, 0);
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    } finally {
        rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
        if (rc != W32Errors.ERROR_SUCCESS) {
            throw new Win32Exception(rc);
        }
    }
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 25 with HKEY

use of com.sun.jna.platform.win32.WinReg.HKEY in project jna by java-native-access.

the class Advapi32UtilTest method testRegistryGetEmptyValues.

public void testRegistryGetEmptyValues() {
    HKEY root = WinReg.HKEY_CURRENT_USER;
    String keyPath = "Software\\JNA";
    Advapi32Util.registryCreateKey(root, "Software", "JNA");
    doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_BINARY);
    doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_EXPAND_SZ);
    doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_MULTI_SZ);
    doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_NONE);
    doTestRegistryGetEmptyValues(root, keyPath, WinNT.REG_SZ);
    Advapi32Util.registryDeleteKey(root, "Software", "JNA");
}
Also used : HKEY(com.sun.jna.platform.win32.WinReg.HKEY)

Aggregations

HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)21 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)14 Advapi32 (com.sun.jna.platform.win32.Advapi32)12 IntByReference (com.sun.jna.ptr.IntByReference)8 SP_DEVINFO_DATA (com.sun.jna.platform.win32.SetupApi.SP_DEVINFO_DATA)1 KEY_BASIC_INFORMATION (com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION)1 WinBase (com.sun.jna.platform.win32.WinBase)1 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1