Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDoug Schaefer2006-04-03 01:41:36 -0400
committerDoug Schaefer2006-04-03 01:41:36 -0400
commit84cef01e4612302e984862c5c91d07b8073fbb33 (patch)
tree394cf337dc511e2f7cf1937cacba9fbe8bebe96d
parentf4e3af2a4e99d18fb90205f7367ac02f86c3e211 (diff)
downloadorg.eclipse.cdt-84cef01e4612302e984862c5c91d07b8073fbb33.tar.gz
org.eclipse.cdt-84cef01e4612302e984862c5c91d07b8073fbb33.tar.xz
org.eclipse.cdt-84cef01e4612302e984862c5c91d07b8073fbb33.zip
Windows registry access utility.
-rw-r--r--core/org.eclipse.cdt.core.tests/.settings/org.eclipse.cdt.core.prefs3
-rw-r--r--core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java23
-rw-r--r--core/org.eclipse.cdt.core.win32/library/nmake.mak8
-rw-r--r--core/org.eclipse.cdt.core.win32/library/winreg.cpp53
-rw-r--r--core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore2
-rw-r--r--core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dllbin0 -> 49152 bytes
-rw-r--r--core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java39
7 files changed, 128 insertions, 0 deletions
diff --git a/core/org.eclipse.cdt.core.tests/.settings/org.eclipse.cdt.core.prefs b/core/org.eclipse.cdt.core.tests/.settings/org.eclipse.cdt.core.prefs
new file mode 100644
index 0000000000..0e9287b8de
--- /dev/null
+++ b/core/org.eclipse.cdt.core.tests/.settings/org.eclipse.cdt.core.prefs
@@ -0,0 +1,3 @@
+#Sun Apr 02 23:10:10 EDT 2006
+eclipse.preferences.version=1
+indexerId=org.eclipse.cdt.core.nullindexer
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
new file mode 100644
index 0000000000..5bef66d737
--- /dev/null
+++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/winreg/tests/WinRegTests.java
@@ -0,0 +1,23 @@
+package org.eclipse.cdt.core.winreg.tests;
+
+import org.eclipse.cdt.utils.WindowsRegistry;
+import org.eclipse.core.runtime.Platform;
+
+import junit.framework.TestCase;
+
+public class WinRegTests extends TestCase {
+
+ public void test1() {
+ WindowsRegistry registry = WindowsRegistry.getRegistry();
+ if (Platform.getOS().equals(Platform.OS_WIN32)) {
+ assertNotNull(registry);
+ String value = registry.getLocalMachineValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion", "ProgramFilesDir");
+ // Not sure how you set this to anything else so it seems safe.
+ assertEquals("C:\\Program Files", value);
+ } else {
+ // Should be null on non-Windows platforms
+ assertNotNull(registry);
+ }
+ }
+
+}
diff --git a/core/org.eclipse.cdt.core.win32/library/nmake.mak b/core/org.eclipse.cdt.core.win32/library/nmake.mak
new file mode 100644
index 0000000000..a3ce6c74cc
--- /dev/null
+++ b/core/org.eclipse.cdt.core.win32/library/nmake.mak
@@ -0,0 +1,8 @@
+TARGET = ..\os\win32\x86\winreg.dll
+
+OBJS = winreg.obj
+
+CPPFLAGS = /nologo /I C:\Java\jdk1.5.0_06\include /I C:\Java\jdk1.5.0_06\include\win32 /DUNICODE
+
+$(TARGET): $(OBJS)
+ link /nologo /dll /out:$(TARGET) $(OBJS) advapi32.lib user32.lib
diff --git a/core/org.eclipse.cdt.core.win32/library/winreg.cpp b/core/org.eclipse.cdt.core.win32/library/winreg.cpp
new file mode 100644
index 0000000000..3f892b95fa
--- /dev/null
+++ b/core/org.eclipse.cdt.core.win32/library/winreg.cpp
@@ -0,0 +1,53 @@
+#include <windows.h>
+#include <jni.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"
+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;
+
+ 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 {
+ 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 {
+ result = env->NewString((jchar *)buffer, wcslen(buffer));
+ }
+ }
+
+ env->ReleaseStringChars(subkey, csubkey);
+ env->ReleaseStringChars(name, cname);
+
+ return result;
+}
diff --git a/core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore b/core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore
new file mode 100644
index 0000000000..d3879971ee
--- /dev/null
+++ b/core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore
@@ -0,0 +1,2 @@
+winreg.lib
+winreg.exp
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
new file mode 100644
index 0000000000..2c113d73d3
--- /dev/null
+++ b/core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll
Binary files 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
new file mode 100644
index 0000000000..bb811dfb7b
--- /dev/null
+++ b/core/org.eclipse.cdt.core/utils/org/eclipse/cdt/utils/WindowsRegistry.java
@@ -0,0 +1,39 @@
+/**
+ *
+ */
+package org.eclipse.cdt.utils;
+
+import org.eclipse.core.runtime.Platform;
+
+/**
+ * @author DSchaefer
+ *
+ */
+public class WindowsRegistry {
+
+ private static boolean failed = false;
+ private static WindowsRegistry registry;
+
+ private WindowsRegistry() {
+ }
+
+ public static WindowsRegistry getRegistry() {
+ if (registry == null && !failed) {
+ if (Platform.getOS().equals(Platform.OS_WIN32)) {
+ try {
+ System.loadLibrary("winreg");
+ registry = new WindowsRegistry();
+ } catch (UnsatisfiedLinkError e) {
+ failed = true;
+ return null;
+ }
+ } else
+ failed = true;
+ }
+
+ return registry;
+ }
+
+ public native String getLocalMachineValue(String subkey, String name);
+
+}

Back to the top