diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java index 5bef66d737f..c2aa8c5fd80 100644 --- a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java @@ -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); + } + } } diff --git a/core/org.eclipse.cdt.core.win32/library/winreg.cpp b/core/org.eclipse.cdt.core.win32/library/winreg.cpp index 3f892b95fa7..8b0562ae7f1 100644 --- a/core/org.eclipse.cdt.core.win32/library/winreg.cpp +++ b/core/org.eclipse.cdt.core.win32/library/winreg.cpp @@ -2,48 +2,25 @@ #include #include -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" 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; + jstring result = NULL; HKEY key; LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, (const wchar_t *)csubkey, 0, KEY_READ, &key); - if (rc != ERROR_SUCCESS) { - result = getErrorMsg(env, L"RegOpenKeyEx"); - } else { + 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 = getErrorMsg(env, L"RegQueryValueEx"); - } else { + if (rc == ERROR_SUCCESS) { result = env->NewString((jchar *)buffer, wcslen(buffer)); } + RegCloseKey(key); } env->ReleaseStringChars(subkey, csubkey); diff --git a/core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll b/core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll index 2c113d73d3f..5290964e77d 100644 Binary files a/core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll and b/core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll differ diff --git a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java index bb811dfb7b8..b73e67dda89 100644 --- a/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java +++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java @@ -34,6 +34,14 @@ public class WindowsRegistry { 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); }