Search in sources :

Example 1 with KEY_BASIC_INFORMATION

use of com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION 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 2 with KEY_BASIC_INFORMATION

use of com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION in project jna by java-native-access.

the class NtDllTest method testZwQueryKey.

public void testZwQueryKey() {
    // open a key
    HKEYByReference phKey = new HKEYByReference();
    assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
    // query key info
    IntByReference resultLength = new IntByReference();
    assertEquals(NTStatus.STATUS_BUFFER_TOO_SMALL, NtDll.INSTANCE.ZwQueryKey(phKey.getValue(), KEY_INFORMATION_CLASS.KeyBasicInformation, null, 0, resultLength));
    assertTrue(resultLength.getValue() > 0);
    KEY_BASIC_INFORMATION keyInformation = new KEY_BASIC_INFORMATION(resultLength.getValue());
    assertEquals(NTStatus.STATUS_SUCCESS, NtDll.INSTANCE.ZwQueryKey(phKey.getValue(), Wdm.KEY_INFORMATION_CLASS.KeyBasicInformation, keyInformation, resultLength.getValue(), resultLength));
    // show
    // Keys are case insensitive (https://msdn.microsoft.com/de-de/library/windows/desktop/ms724946(v=vs.85).aspx)
    assertEquals("software", keyInformation.getName().toLowerCase());
    // close key
    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) KEY_BASIC_INFORMATION(com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION)

Aggregations

KEY_BASIC_INFORMATION (com.sun.jna.platform.win32.Wdm.KEY_BASIC_INFORMATION)2 IntByReference (com.sun.jna.ptr.IntByReference)2 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)1