Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2feeeb9ad48d75812869528373ab49d6fa325abf (plain) (blame)
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
#include <windows.h>
#include <jni.h>
#include <string.h>

extern "C"
JNIEXPORT jstring Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValue(
	JNIEnv * env, jobject obj, jstring subkey, jstring name)
{
	const jchar * csubkey = env->GetStringChars(subkey, NULL);
	const jchar * cname = env->GetStringChars(name, NULL);
	jstring result = NULL;
	
	HKEY key;
	LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (const wchar_t *)csubkey, 0, KEY_READ, &key);
	if (rc == ERROR_SUCCESS) {
		DWORD type;
		wchar_t buffer[256];
		DWORD len = sizeof(buffer);
		rc = RegQueryValueEx(key, (const wchar_t *)cname, NULL, &type, (BYTE *)&buffer, &len);
		if (rc == ERROR_SUCCESS) {
			result = env->NewString((jchar *)buffer, wcslen(buffer));
		}
		RegCloseKey(key);
	}

	env->ReleaseStringChars(subkey, csubkey);
	env->ReleaseStringChars(name, cname);

	return result;
}

/*
 * Given a subkey (string) under HKEY_LOCAL_MACHINE, and an index (starting from 0)
 * to the key's array of values, return the name of the indexed value. 
 * The return value is null on any error or when the index is invalid.
 */
 
extern "C"
JNIEXPORT jstring Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValueName(
	JNIEnv * env, jobject obj, jstring subkey, jint index)
{
	const jchar * csubkey = env->GetStringChars(subkey, NULL);
	jstring 	result = NULL;

	HKEY key;
	LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (const wchar_t *)csubkey, 0, KEY_READ, &key);
	if (rc != ERROR_SUCCESS)
		return NULL;
	
	wchar_t valueName[256];
	DWORD 	nameSize = sizeof(valueName) + 2;
	
	rc = RegEnumValue(key, index, 
			valueName, 		// UNICODE string
			&nameSize, 
			NULL, NULL, 
			NULL, 			// data string
			NULL);			// size in BYTE of data.
	
	if (rc == ERROR_SUCCESS)
	{
		result = env->NewString((jchar *)valueName, nameSize);
	}
	
	RegCloseKey(key);
	
	env->ReleaseStringChars(subkey, csubkey);

	return result;
}

Back to the top