From c58603dfbecb00d7636de51e19e9083c575694b0 Mon Sep 17 00:00:00 2001 From: Torbjörn Svensson Date: Fri, 26 Jun 2020 19:22:07 +0200 Subject: Bug 521515 - Access Windows registry using JNA There is no need to have custom JNI implementation any more in java to be able to access native functions. JNA solves this just fine with the benefit that the code is easier to debug and maintain. Change-Id: Ia9d36981cb10fa7348bf0a5f0549b3e96bd4c982 Signed-off-by: Torbjörn Svensson --- core/org.eclipse.cdt.core.native/build.properties | 6 +- core/org.eclipse.cdt.core.native/plugin.properties | 2 + core/org.eclipse.cdt.core.native/plugin.xml | 5 + .../schema/WindowsRegistry.exsd | 66 ++++++++ .../cdt/internal/core/natives/CNativePlugin.java | 37 +++++ .../src/org/eclipse/cdt/utils/WindowsRegistry.java | 44 +++--- .../os/win32/x86_64/winreg.dll | Bin 39936 -> 0 bytes core/org.eclipse.cdt.core.win32/fragment.xml | 7 + .../library/Makefile_x86_64.mk | 14 +- .../library/cdt-win32.sln | 33 ---- .../library/winreg/.gitignore | 1 - .../library/winreg/dllmain.cpp | 31 ---- .../library/winreg/stdafx.cpp | 17 -- .../library/winreg/stdafx.h | 25 --- .../library/winreg/targetver.h | 21 --- .../library/winreg/winreg.cpp | 154 ------------------ .../library/winreg/winreg.vcxproj | 175 --------------------- .../library/winreg/winreg.vcxproj.filters | 36 ----- .../internal/core/win32/WindowsRegistryImpl.java | 112 +++++++++++++ 19 files changed, 255 insertions(+), 531 deletions(-) create mode 100644 core/org.eclipse.cdt.core.native/plugin.xml create mode 100644 core/org.eclipse.cdt.core.native/schema/WindowsRegistry.exsd delete mode 100755 core/org.eclipse.cdt.core.win32.x86_64/os/win32/x86_64/winreg.dll delete mode 100644 core/org.eclipse.cdt.core.win32/library/cdt-win32.sln delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/.gitignore delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/dllmain.cpp delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/stdafx.cpp delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/stdafx.h delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/targetver.h delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/winreg.cpp delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj delete mode 100644 core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj.filters create mode 100644 core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/WindowsRegistryImpl.java diff --git a/core/org.eclipse.cdt.core.native/build.properties b/core/org.eclipse.cdt.core.native/build.properties index c065e6858b0..2e7a449116e 100644 --- a/core/org.eclipse.cdt.core.native/build.properties +++ b/core/org.eclipse.cdt.core.native/build.properties @@ -19,8 +19,10 @@ bin.includes = plugin.properties,\ about.ini,\ about.mappings,\ cdt_logo_icon32.png,\ - about.properties -src.includes = about.html + about.properties,\ + plugin.xml +src.includes = about.html,\ + schema/ javadoc.packages = org.eclipse.cdt.utils.*,\ org.eclipse.cdt.utils.pty.*,\ org.eclipse.cdt.utils.spawner.* diff --git a/core/org.eclipse.cdt.core.native/plugin.properties b/core/org.eclipse.cdt.core.native/plugin.properties index 40d7fc25cea..1e8e0166d60 100755 --- a/core/org.eclipse.cdt.core.native/plugin.properties +++ b/core/org.eclipse.cdt.core.native/plugin.properties @@ -16,6 +16,8 @@ pluginName=C/C++ Development Tools Core Native Utilities providerName=Eclipse CDT +WindowsRegistry.name=Windows Registry + fragmentName.linux = C/C++ Development Tools Core Native Utilities for Linux fragmentName.linux.ppc64le = C/C++ Development Tools Core Native Utilities for Linux (ppc64le) fragmentName.linux.x86_64 = C/C++ Development Tools Core Native Utilities for Linux (x86_64) diff --git a/core/org.eclipse.cdt.core.native/plugin.xml b/core/org.eclipse.cdt.core.native/plugin.xml new file mode 100644 index 00000000000..174452ec9f4 --- /dev/null +++ b/core/org.eclipse.cdt.core.native/plugin.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/core/org.eclipse.cdt.core.native/schema/WindowsRegistry.exsd b/core/org.eclipse.cdt.core.native/schema/WindowsRegistry.exsd new file mode 100644 index 00000000000..eabfd78bfe0 --- /dev/null +++ b/core/org.eclipse.cdt.core.native/schema/WindowsRegistry.exsd @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/internal/core/natives/CNativePlugin.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/internal/core/natives/CNativePlugin.java index 976fa95387e..db2a72763b3 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/internal/core/natives/CNativePlugin.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/internal/core/natives/CNativePlugin.java @@ -14,7 +14,13 @@ *******************************************************************************/ package org.eclipse.cdt.internal.core.natives; +import org.eclipse.cdt.utils.WindowsRegistry; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Plugin; import org.eclipse.core.runtime.Status; @@ -92,4 +98,35 @@ public class CNativePlugin extends Plugin { getDefault().getLog().log(status); } + /** + * Get the WindowsRegistry contributed class for the platform. + */ + public WindowsRegistry getWindowsRegistry() throws CoreException { + IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, "WindowsRegistry"); //$NON-NLS-1$ + if (extension != null) { + IExtension[] extensions = extension.getExtensions(); + IConfigurationElement defaultContributor = null; + for (IExtension extension2 : extensions) { + IConfigurationElement[] configElements = extension2.getConfigurationElements(); + for (IConfigurationElement configElement : configElements) { + if (configElement.getName().equals("windowsRegistry")) { //$NON-NLS-1$ + String platform = configElement.getAttribute("platform"); //$NON-NLS-1$ + if (platform == null) { // first contributor found with + // not platform will be default. + if (defaultContributor == null) { + defaultContributor = configElement; + } + } else if (platform.equals(Platform.getOS())) { + // found explicit contributor for this platform. + return (WindowsRegistry) configElement.createExecutableExtension("class"); //$NON-NLS-1$ + } + } + } + } + if (defaultContributor != null) { + return (WindowsRegistry) defaultContributor.createExecutableExtension("class"); //$NON-NLS-1$ + } + } + return null; + } } diff --git a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/WindowsRegistry.java b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/WindowsRegistry.java index 31d31cd6d93..f2054512232 100644 --- a/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/WindowsRegistry.java +++ b/core/org.eclipse.cdt.core.native/src/org/eclipse/cdt/utils/WindowsRegistry.java @@ -11,38 +11,31 @@ * Contributors: * QNX Software Systems - initial API and implementation *******************************************************************************/ -/** - * - */ package org.eclipse.cdt.utils; -import org.eclipse.core.runtime.Platform; +import org.eclipse.cdt.internal.core.natives.CNativePlugin; +import org.eclipse.core.runtime.CoreException; /** * @author DSchaefer * @noextend This class is not intended to be subclassed by clients. - * @noinstantiate This class is not intended to be instantiated by clients. */ -public class WindowsRegistry { +public abstract class WindowsRegistry { - private static boolean failed = false; private static WindowsRegistry registry; - private WindowsRegistry() { + /** + * @since 6.0 + */ + protected WindowsRegistry() { } public static WindowsRegistry getRegistry() { - if (registry == null && !failed) { - if (Platform.getOS().equals(Platform.OS_WIN32)) { - try { - System.loadLibrary("winreg"); //$NON-NLS-1$ - registry = new WindowsRegistry(); - } catch (UnsatisfiedLinkError e) { - failed = true; - return null; - } - } else - failed = true; + if (registry == null) { + try { + registry = CNativePlugin.getDefault().getWindowsRegistry(); + } catch (CoreException e) { + } } return registry; @@ -56,7 +49,7 @@ public class WindowsRegistry { * @param name name of the registry value * @return registry value or null if not found */ - public native String getLocalMachineValue(String subkey, String name); + public abstract String getLocalMachineValue(String subkey, String name); /** * Given a subkey of HKEY_LOCAL_MACHINE, and an index (starting from 0) @@ -68,7 +61,7 @@ public class WindowsRegistry { * @param index index to the subkey's array of values, starting from 0. * @return name of registry value or null if not found */ - public native String getLocalMachineValueName(String subkey, int index); + public abstract String getLocalMachineValueName(String subkey, int index); /** * Given a subkey of HKEY_LOCAL_MACHINE, and an index (starting from 0) @@ -80,7 +73,7 @@ public class WindowsRegistry { * @param index index to the subkey's array of values, starting from 0. * @return name of registry value or null if not found */ - public native String getLocalMachineKeyName(String subkey, int index); + public abstract String getLocalMachineKeyName(String subkey, int index); /** * Gets the registry value for the subkey of HKEY_CURRENT_USER with the @@ -90,7 +83,7 @@ public class WindowsRegistry { * @param name name of the registry value * @return registry value or null if not found */ - public native String getCurrentUserValue(String subkey, String name); + public abstract String getCurrentUserValue(String subkey, String name); /** * Given a subkey of HKEY_CURRENT_USER, and an index (starting from 0) @@ -102,7 +95,7 @@ public class WindowsRegistry { * @param index index to the subkey's array of values, starting from 0. * @return name of registry value or null if not found */ - public native String getCurrentUserValueName(String subkey, int index); + public abstract String getCurrentUserValueName(String subkey, int index); /** * Given a subkey of HKEY_CURRENT_USER, and an index (starting from 0) @@ -114,6 +107,5 @@ public class WindowsRegistry { * @param index index to the subkey's array of values, starting from 0. * @return name of registry value or null if not found */ - public native String getCurrentUserKeyName(String subkey, int index); - + public abstract String getCurrentUserKeyName(String subkey, int index); } diff --git a/core/org.eclipse.cdt.core.win32.x86_64/os/win32/x86_64/winreg.dll b/core/org.eclipse.cdt.core.win32.x86_64/os/win32/x86_64/winreg.dll deleted file mode 100755 index a8e74e65993..00000000000 Binary files a/core/org.eclipse.cdt.core.win32.x86_64/os/win32/x86_64/winreg.dll and /dev/null differ diff --git a/core/org.eclipse.cdt.core.win32/fragment.xml b/core/org.eclipse.cdt.core.win32/fragment.xml index f06998bc1fd..a1807605b60 100644 --- a/core/org.eclipse.cdt.core.win32/fragment.xml +++ b/core/org.eclipse.cdt.core.win32/fragment.xml @@ -9,4 +9,11 @@ class="org.eclipse.cdt.internal.core.win32.ProcessList"> + + + + diff --git a/core/org.eclipse.cdt.core.win32/library/Makefile_x86_64.mk b/core/org.eclipse.cdt.core.win32/library/Makefile_x86_64.mk index 1d8d016f8c3..aeb5c889190 100644 --- a/core/org.eclipse.cdt.core.win32/library/Makefile_x86_64.mk +++ b/core/org.eclipse.cdt.core.win32/library/Makefile_x86_64.mk @@ -30,9 +30,6 @@ INSTALL_DIR = ..\..\org.eclipse.cdt.core.$(OS).$(ARCH)\os\$(OS)\$(ARCH) DLL_SPAWNER = spawner.dll OBJS_SPAWNER=StdAfx.obj Win32ProcessEx.obj iostream.obj raise.obj spawner.obj -DLL_WINREG = winreg.dll -OBJS_WINREG=winreg/winreg.obj - EXE_STARTER = starter.exe OBJS_STARTER=starter/starter.obj @@ -45,22 +42,19 @@ OBJS_STARTER=starter/starter.obj spawner: $(OBJS_SPAWNER) link /dll /nologo /out:$(DLL_SPAWNER) $(OBJS_SPAWNER) User32.lib -winreg: $(OBJS_WINREG) - link /dll /nologo /out:$(DLL_WINREG) $(OBJS_WINREG) Advapi32.lib - starter: $(OBJS_STARTER) link /nologo /out:$(EXE_STARTER) $(OBJS_STARTER) Psapi.Lib Shell32.lib - -all: spawner winreg starter + +all: spawner starter clean: - del *.obj *.lib *.exp *.exe *.dll winreg\*.obj starter\*.obj + del *.obj *.lib *.exp *.exe *.dll starter\*.obj rebuild: clean all install: all copy *.dll $(INSTALL_DIR) copy *.exe $(INSTALL_DIR) - + uninstall: del $(INSTALL_DIR)\*.dll $(INSTALL_DIR)\*.exe diff --git a/core/org.eclipse.cdt.core.win32/library/cdt-win32.sln b/core/org.eclipse.cdt.core.win32/library/cdt-win32.sln deleted file mode 100644 index 01d10abe31e..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/cdt-win32.sln +++ /dev/null @@ -1,33 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "winreg", "winreg\winreg.vcxproj", "{4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Mixed Platforms = Debug|Mixed Platforms - Debug|Win32 = Debug|Win32 - Debug|x64 = Debug|x64 - Release|Mixed Platforms = Release|Mixed Platforms - Release|Win32 = Release|Win32 - Release|x64 = Release|x64 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Debug|Mixed Platforms.Build.0 = Debug|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Debug|Win32.ActiveCfg = Debug|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Debug|Win32.Build.0 = Debug|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Debug|x64.ActiveCfg = Debug|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|Mixed Platforms.ActiveCfg = Release|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|Mixed Platforms.Build.0 = Release|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|Win32.ActiveCfg = Release|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|Win32.Build.0 = Release|Win32 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|x64.ActiveCfg = Release|x64 - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363}.Release|x64.Build.0 = Release|x64 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/.gitignore b/core/org.eclipse.cdt.core.win32/library/winreg/.gitignore deleted file mode 100644 index 7d9b212a579..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/winreg.tlog/ diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/dllmain.cpp b/core/org.eclipse.cdt.core.win32/library/winreg/dllmain.cpp deleted file mode 100644 index 1464319edac..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/dllmain.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 QNX Software Systems and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * QNX Software Systems - Initial API and implementation - *******************************************************************************/ -#include "stdafx.h" - -BOOL APIENTRY DllMain( HMODULE hModule, - DWORD ul_reason_for_call, - LPVOID lpReserved - ) -{ - switch (ul_reason_for_call) - { - case DLL_PROCESS_ATTACH: - case DLL_THREAD_ATTACH: - case DLL_THREAD_DETACH: - case DLL_PROCESS_DETACH: - break; - } - return TRUE; -} - diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.cpp b/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.cpp deleted file mode 100644 index 4033266f9a1..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.cpp +++ /dev/null @@ -1,17 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 QNX Software Systems and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * QNX Software Systems - Initial API and implementation - *******************************************************************************/ -#include "stdafx.h" - -// TODO: reference any additional headers you need in STDAFX.H -// and not in this file diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.h b/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.h deleted file mode 100644 index 506fec98540..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/stdafx.h +++ /dev/null @@ -1,25 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 QNX Software Systems and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * QNX Software Systems - Initial API and implementation - *******************************************************************************/ -#pragma once - -#include "targetver.h" - -#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers -// Windows Header Files: -#include -#include - - - -// TODO: reference additional headers your program requires here diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/targetver.h b/core/org.eclipse.cdt.core.win32/library/winreg/targetver.h deleted file mode 100644 index 080979afe73..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/targetver.h +++ /dev/null @@ -1,21 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 QNX Software Systems and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * Contributors: - * QNX Software Systems - Initial API and implementation - *******************************************************************************/ -#pragma once - -// Including SDKDDKVer.h defines the highest available Windows platform. - -// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and -// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. - -#include diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.cpp b/core/org.eclipse.cdt.core.win32/library/winreg/winreg.cpp deleted file mode 100644 index 25a77124217..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2015 QNX Software Systems and others. - * - * This program and the accompanying materials - * are made available under the terms of the Eclipse Public License 2.0 - * which accompanies this distribution, and is available at - * https://www.eclipse.org/legal/epl-2.0/ - * - * SPDX-License-Identifier: EPL-2.0 - * - * 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); -} diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj b/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj deleted file mode 100644 index 0c82aec90f9..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj +++ /dev/null @@ -1,175 +0,0 @@ - - - - - Debug - Win32 - - - Debug - x64 - - - Release - Win32 - - - Release - x64 - - - - {4CA57EA3-42F2-4CC1-8E95-5C707A8E7363} - Win32Proj - winreg - - - - DynamicLibrary - true - v120 - Unicode - - - DynamicLibrary - true - v120 - Unicode - - - DynamicLibrary - false - v120 - true - Unicode - - - DynamicLibrary - false - v120 - true - Unicode - - - - - - - - - - - - - - - - - - - true - - - true - - - false - $(SolutionDir)..\..\org.eclipse.cdt.core.win32.x86\os\win32\x86\ - - - - false - $(SolutionDir)..\..\org.eclipse.cdt.core.win32.x86_64\os\win32\x86_64\ - - - - - Use - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;WINREG_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - Use - Level3 - Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;WINREG_EXPORTS;%(PreprocessorDefinitions) - - - Windows - true - - - - - Level3 - Use - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;WINREG_EXPORTS;%(PreprocessorDefinitions) - C:\Program Files\Java\jdk1.8.0_31\include;C:\Program Files\Java\jdk1.8.0_31\include\win32 - MultiThreaded - - - Windows - true - true - true - - - - - Level3 - Use - MaxSpeed - true - true - WIN32;NDEBUG;_WINDOWS;_USRDLL;WINREG_EXPORTS;%(PreprocessorDefinitions) - C:\Program Files\Java\jdk1.8.0_31\include;C:\Program Files\Java\jdk1.8.0_31\include\win32 - MultiThreaded - - - Windows - true - true - true - - - - - - - - - false - false - - - - - false - false - - - - - - - Create - Create - Create - Create - - - - - - - \ No newline at end of file diff --git a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj.filters b/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj.filters deleted file mode 100644 index afa4a0f572f..00000000000 --- a/core/org.eclipse.cdt.core.win32/library/winreg/winreg.vcxproj.filters +++ /dev/null @@ -1,36 +0,0 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - - - Header Files - - - Header Files - - - - - Source Files - - - Source Files - - - Source Files - - - \ No newline at end of file diff --git a/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/WindowsRegistryImpl.java b/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/WindowsRegistryImpl.java new file mode 100644 index 00000000000..dcb48a8fc7b --- /dev/null +++ b/core/org.eclipse.cdt.core.win32/src/org/eclipse/cdt/internal/core/win32/WindowsRegistryImpl.java @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2020 Torbjörn Svensson + * + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License 2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Torbjörn Svensson - Initial implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.win32; + +import org.eclipse.cdt.internal.core.natives.CNativePlugin; +import org.eclipse.cdt.utils.WindowsRegistry; + +import com.sun.jna.Native; +import com.sun.jna.platform.win32.Advapi32; +import com.sun.jna.platform.win32.Advapi32Util; +import com.sun.jna.platform.win32.Advapi32Util.EnumKey; +import com.sun.jna.platform.win32.W32Errors; +import com.sun.jna.platform.win32.Win32Exception; +import com.sun.jna.platform.win32.WinNT; +import com.sun.jna.platform.win32.WinReg; +import com.sun.jna.platform.win32.WinReg.HKEY; +import com.sun.jna.platform.win32.WinReg.HKEYByReference; +import com.sun.jna.ptr.IntByReference; + +/** + * @author Torbjörn Svensson + */ +public class WindowsRegistryImpl extends WindowsRegistry { + + @Override + public String getLocalMachineValue(String subkey, String name) { + return getValue(WinReg.HKEY_LOCAL_MACHINE, subkey, name); + } + + @Override + public String getLocalMachineValueName(String subkey, int index) { + return getValueName(WinReg.HKEY_LOCAL_MACHINE, subkey, index); + } + + @Override + public String getLocalMachineKeyName(String subkey, int index) { + return getKeyName(WinReg.HKEY_LOCAL_MACHINE, subkey, index); + } + + @Override + public String getCurrentUserValue(String subkey, String name) { + return getValue(WinReg.HKEY_CURRENT_USER, subkey, name); + } + + @Override + public String getCurrentUserValueName(String subkey, int index) { + return getValueName(WinReg.HKEY_CURRENT_USER, subkey, index); + } + + @Override + public String getCurrentUserKeyName(String subkey, int index) { + return getKeyName(WinReg.HKEY_CURRENT_USER, subkey, index); + } + + private String getValue(HKEY key, String subkey, String name) { + try { + return Advapi32Util.registryGetStringValue(key, subkey, name); + } catch (Win32Exception e) { + CNativePlugin.log(String.format("Unable to get value for %s in %s", name, subkey), e); //$NON-NLS-1$ + return null; + } + } + + private String getKeyName(HKEY key, String subkey, int index) { + try { + HKEYByReference phkKey = Advapi32Util.registryGetKey(key, subkey, WinNT.KEY_READ); + try { + EnumKey enumKey = Advapi32Util.registryRegEnumKey(phkKey.getValue(), index); + return Native.toString(enumKey.lpName); + } finally { + Advapi32Util.registryCloseKey(phkKey.getValue()); + } + } catch (Win32Exception e) { + CNativePlugin.log(String.format("Unable to get keyname for %s at index %d", subkey, index), e); //$NON-NLS-1$ + return null; + } + } + + private String getValueName(HKEY key, String subkey, int index) { + try { + HKEYByReference phkKey = Advapi32Util.registryGetKey(key, subkey, WinNT.KEY_READ); + try { + char[] lpValueName = new char[Advapi32.MAX_KEY_LENGTH]; + IntByReference lpcchValueName = new IntByReference(Advapi32.MAX_KEY_LENGTH); + int rc = Advapi32.INSTANCE.RegEnumValue(phkKey.getValue(), index, lpValueName, lpcchValueName, null, + null, null, null); + + if (rc != W32Errors.ERROR_SUCCESS) { + throw new Win32Exception(rc); + } + + return Native.toString(lpValueName); + } finally { + Advapi32Util.registryCloseKey(phkKey.getValue()); + } + } catch (Win32Exception e) { + CNativePlugin.log(String.format("Unable to get valuename for %s at index %d", subkey, index), e); //$NON-NLS-1$ + return null; + } + } +} -- cgit v1.2.3