Search in sources :

Example 1 with HKEY

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

the class SetupApiTest method testSetupDiOpenDevRegKey.

/**
	 * Tests the mapping of {@link SetupApi#SetupDiOpenDevRegKey(HANDLE, SP_DEVINFO_DATA, int, int, int, int)} .
	 * <p>
	 * The test pass if SetupDiOpenDevRegKey(..) returns a valid {@link HKEY} pointing to the first found device on the current machine.
	 * <p>
	 * NOTE: We only test the mapping of SetupDiOpenDevRegKey(..), not it's functionality.
	 */
public void testSetupDiOpenDevRegKey() {
    // hDevInfoSet repesents a list of installed devices for all device
    // setup classes or all device interface classes
    HANDLE hDevInfoSet = SetupApi.INSTANCE.SetupDiGetClassDevs(null, null, null, DIGCF_ALLCLASSES);
    assertTrue(hDevInfoSet != INVALID_HANDLE_VALUE);
    SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
    // there must be least one device (drive,processor,pci,usb,...) on the
    // current machine
    assertTrue(SetupApi.INSTANCE.SetupDiEnumDeviceInfo(hDevInfoSet, FIRST_MEMBER, devInfo));
    HKEY hDeviceKey = SetupApi.INSTANCE.SetupDiOpenDevRegKey(hDevInfoSet, devInfo, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE);
    assertTrue(hDeviceKey != INVALID_HANDLE_VALUE);
    Advapi32.INSTANCE.RegCloseKey(hDeviceKey);
}
Also used : SP_DEVINFO_DATA(com.sun.jna.platform.win32.SetupApi.SP_DEVINFO_DATA) HKEY(com.sun.jna.platform.win32.WinReg.HKEY) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 2 with HKEY

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

the class Advapi32Util method registryGetKey.

/**
	 * Get a registry key, the caller is responsible to close the key after use.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to a registry key.
	 *
	 * @param samDesired
	 *            Access level (e.g. WinNT.KEY_READ)
	 *
	 * @return HKEYByReference.
	 */
public static HKEYByReference registryGetKey(HKEY root, String keyPath, int samDesired) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, samDesired, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
    }
    return phkKey;
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 3 with HKEY

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

the class Advapi32Util method registryDeleteValue.

/**
	 * Delete a registry value.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param valueName
	 *            Name of the value to delete.
	 */
public static void registryDeleteValue(HKEY root, String keyPath, String valueName) {
    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 {
        registryDeleteValue(phkKey.getValue(), valueName);
    } 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 4 with HKEY

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

the class Advapi32Util method registryDeleteKey.

/**
	 * Delete a registry key.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param keyName
	 *            Name of the key to delete.
	 */
public static void registryDeleteKey(HKEY root, String keyPath, String keyName) {
    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 {
        registryDeleteKey(phkKey.getValue(), keyName);
    } 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 5 with HKEY

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

the class Advapi32Util method registrySetIntValue.

/**
	 * Set an integer value in registry.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to an existing registry key.
	 * @param name
	 *            Value name.
	 * @param value
	 *            Value to write to registry.
	 */
public static void registrySetIntValue(HKEY root, String keyPath, String name, int value) {
    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 {
        registrySetIntValue(phkKey.getValue(), name, value);
    } 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)

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