mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Windows registry access utility.
This commit is contained in:
parent
f4e3af2a4e
commit
84cef01e46
7 changed files with 128 additions and 0 deletions
|
@ -0,0 +1,3 @@
|
|||
#Sun Apr 02 23:10:10 EDT 2006
|
||||
eclipse.preferences.version=1
|
||||
indexerId=org.eclipse.cdt.core.nullindexer
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
8
core/org.eclipse.cdt.core.win32/library/nmake.mak
Normal file
8
core/org.eclipse.cdt.core.win32/library/nmake.mak
Normal file
|
@ -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
|
53
core/org.eclipse.cdt.core.win32/library/winreg.cpp
Normal file
53
core/org.eclipse.cdt.core.win32/library/winreg.cpp
Normal file
|
@ -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;
|
||||
}
|
2
core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore
Normal file
2
core/org.eclipse.cdt.core.win32/os/win32/x86/.cvsignore
Normal file
|
@ -0,0 +1,2 @@
|
|||
winreg.lib
|
||||
winreg.exp
|
BIN
core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll
Normal file
BIN
core/org.eclipse.cdt.core.win32/os/win32/x86/winreg.dll
Normal file
Binary file not shown.
|
@ -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);
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue