Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c64a43ae8708ef018e513ac0932912bb325cc013 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
#include "stdafx.h"

static jstring getValue(JNIEnv * env, HKEY key, jstring subkey, jstring name) {
	const jchar * csubkey = env->GetStringChars(subkey, NULL);
	const jchar * cname = env->GetStringChars(name, NULL);
	jstring result = NULL;

	HKEY skey;
	LONG rc = RegOpenKeyEx(key, (const wchar_t *)csubkey, 0, KEY_READ, &skey);
	if (rc == ERROR_SUCCESS) {
		DWORD type;
		wchar_t buffer[256];
		DWORD len = sizeof(buffer);
		rc = RegQueryValueEx(skey, (const wchar_t *)cname, NULL, &type, (BYTE *)&buffer, &len);
		if (rc == ERROR_SUCCESS) {
			result = env->NewString((jchar *) buffer, (jsize) wcslen(buffer));
		}
		RegCloseKey(skey);
	}

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

	return result;
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValue(
JNIEnv * env, jobject obj, jstring subkey, jstring name)
{
	return getValue(env, HKEY_LOCAL_MACHINE, subkey, name);
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getCurrentUserValue(
JNIEnv * env, jobject obj, jstring subkey, jstring name)
{
	return getValue(env, HKEY_CURRENT_USER, subkey, name);
}

/*
* 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.
*/

static jstring getValueName(JNIEnv * env, HKEY key, jstring subkey, jint index) {
	const jchar * csubkey = env->GetStringChars(subkey, NULL);
	jstring 	result = NULL;

	HKEY skey;
	LONG rc = RegOpenKeyEx(key, (const wchar_t *)csubkey, 0, KEY_READ, &skey);
	if (rc != ERROR_SUCCESS)
		return NULL;

	wchar_t valueName[256];
	DWORD 	nameSize = sizeof(valueName) + 2;

	rc = RegEnumValue(skey, 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(skey);

	env->ReleaseStringChars(subkey, csubkey);

	return result;
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValueName(
JNIEnv * env, jobject obj, jstring subkey, jint index)
{
	return getValueName(env, HKEY_LOCAL_MACHINE, subkey, index);
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getCurrentUserValueName(
JNIEnv * env, jobject obj, jstring subkey, jint index)
{
	return getValueName(env, HKEY_CURRENT_USER, subkey, index);
}

/*
* Given a subkey (string) under HKEY_LOCAL_MACHINE, and an index (starting from 0)
* to the key's array of keys, return the name of the indexed key.
* The return value is null on any error or when the index is invalid.
*/

static jstring getKeyName(JNIEnv * env, HKEY key, jstring subkey, jint index) {
	const jchar * csubkey = env->GetStringChars(subkey, NULL);
	jstring 	result = NULL;

	HKEY skey;
	LONG rc = RegOpenKeyEx(key, (const wchar_t *)csubkey, 0, KEY_READ, &skey);
	if (rc != ERROR_SUCCESS)
		return NULL;

	wchar_t keyName[256];
	DWORD 	nameSize = sizeof(keyName) + 2;

	rc = RegEnumKeyEx(skey, index,
		keyName, 		// UNICODE string
		&nameSize,
		NULL, NULL,
		NULL,
		NULL);			// size in BYTE of data.

	if (rc == ERROR_SUCCESS)
	{
		result = env->NewString((jchar *)keyName, nameSize);
	}

	RegCloseKey(skey);

	env->ReleaseStringChars(subkey, csubkey);

	return result;
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineKeyName(
JNIEnv * env, jobject obj, jstring subkey, jint index)
{
	return getKeyName(env, HKEY_LOCAL_MACHINE, subkey, index);
}

extern "C"
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_utils_WindowsRegistry_getCurrentUserKeyName(
JNIEnv * env, jobject obj, jstring subkey, jint index)
{
	return getKeyName(env, HKEY_CURRENT_USER, subkey, index);
}

Back to the top