mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-01 06:05:24 +02:00
Make the Windows registry code a little more robust.
This commit is contained in:
parent
3df6744baa
commit
0c247b1d5f
4 changed files with 24 additions and 27 deletions
|
@ -20,4 +20,16 @@ public class WinRegTests extends TestCase {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void test2() {
|
||||||
|
WindowsRegistry registry = WindowsRegistry.getRegistry();
|
||||||
|
if (Platform.getOS().equals(Platform.OS_WIN32)) {
|
||||||
|
assertNotNull(registry);
|
||||||
|
String value = registry.getLocalMachineValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "Nothing");
|
||||||
|
// Not sure how you set this to anything else so it seems safe.
|
||||||
|
assertNull(value);
|
||||||
|
} else {
|
||||||
|
// Should be null on non-Windows platforms
|
||||||
|
assertNotNull(registry);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,48 +2,25 @@
|
||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
jstring getErrorMsg(JNIEnv * env, wchar_t * name) {
|
|
||||||
wchar_t msg[256];
|
|
||||||
wchar_t * msgBuff;
|
|
||||||
DWORD err = GetLastError();
|
|
||||||
|
|
||||||
FormatMessage(
|
|
||||||
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
|
|
||||||
NULL,
|
|
||||||
err,
|
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
|
||||||
(LPTSTR) &msgBuff,
|
|
||||||
0, NULL );
|
|
||||||
|
|
||||||
wsprintf(msg, L"%s failed with error %d: %s", name, err, msgBuff);
|
|
||||||
|
|
||||||
LocalFree(msgBuff);
|
|
||||||
|
|
||||||
return env->NewString((jchar *)msg, wcslen(msg));
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
JNIEXPORT jstring Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValue(
|
JNIEXPORT jstring Java_org_eclipse_cdt_utils_WindowsRegistry_getLocalMachineValue(
|
||||||
JNIEnv * env, jobject obj, jstring subkey, jstring name)
|
JNIEnv * env, jobject obj, jstring subkey, jstring name)
|
||||||
{
|
{
|
||||||
const jchar * csubkey = env->GetStringChars(subkey, NULL);
|
const jchar * csubkey = env->GetStringChars(subkey, NULL);
|
||||||
const jchar * cname = env->GetStringChars(name, NULL);
|
const jchar * cname = env->GetStringChars(name, NULL);
|
||||||
jstring result;
|
jstring result = NULL;
|
||||||
|
|
||||||
HKEY key;
|
HKEY key;
|
||||||
LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (const wchar_t *)csubkey, 0, KEY_READ, &key);
|
LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (const wchar_t *)csubkey, 0, KEY_READ, &key);
|
||||||
if (rc != ERROR_SUCCESS) {
|
if (rc == ERROR_SUCCESS) {
|
||||||
result = getErrorMsg(env, L"RegOpenKeyEx");
|
|
||||||
} else {
|
|
||||||
DWORD type;
|
DWORD type;
|
||||||
wchar_t buffer[256];
|
wchar_t buffer[256];
|
||||||
DWORD len = sizeof(buffer);
|
DWORD len = sizeof(buffer);
|
||||||
rc = RegQueryValueEx(key, (const wchar_t *)cname, NULL, &type, (BYTE *)&buffer, &len);
|
rc = RegQueryValueEx(key, (const wchar_t *)cname, NULL, &type, (BYTE *)&buffer, &len);
|
||||||
if (rc != ERROR_SUCCESS) {
|
if (rc == ERROR_SUCCESS) {
|
||||||
result = getErrorMsg(env, L"RegQueryValueEx");
|
|
||||||
} else {
|
|
||||||
result = env->NewString((jchar *)buffer, wcslen(buffer));
|
result = env->NewString((jchar *)buffer, wcslen(buffer));
|
||||||
}
|
}
|
||||||
|
RegCloseKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
env->ReleaseStringChars(subkey, csubkey);
|
env->ReleaseStringChars(subkey, csubkey);
|
||||||
|
|
Binary file not shown.
|
@ -34,6 +34,14 @@ public class WindowsRegistry {
|
||||||
return registry;
|
return registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the registry value for the subkey of HKEY_LOCAL_MACHINE with the
|
||||||
|
* given name. If problems occur, like the name is not found, null is returned.
|
||||||
|
*
|
||||||
|
* @param subkey subkey of HKEY_LOCAL_MACHINE
|
||||||
|
* @param name name of the registry value
|
||||||
|
* @return registry value or null if not found
|
||||||
|
*/
|
||||||
public native String getLocalMachineValue(String subkey, String name);
|
public native String getLocalMachineValue(String subkey, String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue