8.46. jy.dist.nt.Advapi32GetCurrentVersion(.java)

Module for the registry access of the Jython implementation on WindowsNT [Jython].

8.46.1. Source

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
 *
 * Requires in classpath:
 *
 *  jna.jar
 *  jna-platform.jar
 *  win32-x86-64.jar / win32-x86.jar 
 *
 * see: https://github.com/java-native-access/jna/tree/master/dist
 *
 */

package platformids.dist.nt;

import java.util.TreeMap;
import com.sun.jna.platform.win32.Advapi32;
import com.sun.jna.platform.win32.WinReg.HKEY;
import com.sun.jna.platform.win32.WinReg.HKEYByReference;
import com.sun.jna.platform.win32.WinNT;
import com.sun.jna.platform.win32.W32Errors;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.Advapi32Util;

import static com.sun.jna.platform.win32.WinReg.HKEY_LOCAL_MACHINE;

/**
 * @author Arno-Can Uestuensoez <acue_sf2 @ sourceforge.net>
 * @version 0.1.1
 * @since 0.1.30
 */
public class Advapi32GetCurrentVersion {

	/**
	 * Get attribute names and values of registry key.
	 *
	 * @param root
	 *            Root key.
	 * @param keyPath
	 *            Path to a registry key.
	 * @return TreMap<String, Object> of registry name value pairs.
	 */
	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 {
			vals =Advapi32Util.registryGetValues(phkKey.getValue());
		} finally {
			rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
			if (rc != W32Errors.ERROR_SUCCESS) {
				throw new Win32Exception(rc);
			}
		}
		return vals;

	}

	/**
	 * Get key-value pairs of the registry key's attributes:
	 *
	 * "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"
	 *
	 * @return Map of of registry names and values for CurrentVersion.
	 */
	public static TreeMap<String, Object> getCurrentVersionValues() {
		return registryGetValues(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
	}

}