Search in sources :

Example 1 with HKEYByReference

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

the class Advapi32Test method testRegQueryValueEx.

public void testRegQueryValueEx() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, WinNT.KEY_READ, phKey));
    IntByReference lpcbData = new IntByReference();
    IntByReference lpType = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, (char[]) null, lpcbData));
    assertEquals(WinNT.REG_SZ, lpType.getValue());
    assertTrue(lpcbData.getValue() > 0);
    char[] buffer = new char[lpcbData.getValue()];
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phKey.getValue(), "User Agent", 0, lpType, buffer, lpcbData));
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 2 with HKEYByReference

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

the class Advapi32Test method testRegSetValueEx_DWORD.

public void testRegSetValueEx_DWORD() {
    HKEYByReference phKey = new HKEYByReference();
    // create parent key
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
    HKEYByReference phkTest = new HKEYByReference();
    IntByReference lpdwDisposition = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCreateKeyEx(phKey.getValue(), "JNAAdvapi32Test", 0, null, 0, WinNT.KEY_ALL_ACCESS, null, phkTest, lpdwDisposition));
    // write a REG_DWORD value
    int value = 42145;
    byte[] data = new byte[4];
    data[0] = (byte) (value & 0xff);
    data[1] = (byte) ((value >> 8) & 0xff);
    data[2] = (byte) ((value >> 16) & 0xff);
    data[3] = (byte) ((value >> 24) & 0xff);
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegSetValueEx(phkTest.getValue(), "DWORD", 0, WinNT.REG_DWORD, data, 4));
    // re-read the REG_DWORD value
    IntByReference lpType = new IntByReference();
    IntByReference lpcbData = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "DWORD", 0, lpType, (char[]) null, lpcbData));
    assertEquals(WinNT.REG_DWORD, lpType.getValue());
    assertEquals(4, lpcbData.getValue());
    IntByReference valueRead = new IntByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(phkTest.getValue(), "DWORD", 0, lpType, valueRead, lpcbData));
    assertEquals(value, valueRead.getValue());
    // delete the test key
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phkTest.getValue()));
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegDeleteKey(phKey.getValue(), "JNAAdvapi32Test"));
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference) IntByReference(com.sun.jna.ptr.IntByReference)

Example 3 with HKEYByReference

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

the class NtDllUtilTest method testGetKeyName.

public void testGetKeyName() {
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
    // Keys are case insensitive (https://msdn.microsoft.com/de-de/library/windows/desktop/ms724946(v=vs.85).aspx)
    assertEquals("software", NtDllUtil.getKeyName(phKey.getValue()).toLowerCase());
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
}
Also used : HKEYByReference(com.sun.jna.platform.win32.WinReg.HKEYByReference)

Example 4 with HKEYByReference

use of com.sun.jna.platform.win32.WinReg.HKEYByReference 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 5 with HKEYByReference

use of com.sun.jna.platform.win32.WinReg.HKEYByReference 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)

Aggregations

HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)32 IntByReference (com.sun.jna.ptr.IntByReference)9 Advapi32 (com.sun.jna.platform.win32.Advapi32)3 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)3 EnumKey (com.sun.jna.platform.win32.Advapi32Util.EnumKey)1 InfoKey (com.sun.jna.platform.win32.Advapi32Util.InfoKey)1 KEY_BASIC_INFORMATION (com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION)1 ArrayList (java.util.ArrayList)1