Bug 568079: Reuse existing way of fetching data from registry

Use the WindowsRegistry implementation rather than having yet another
implementation for reading the Windows registry in native code.

Change-Id: If12068319ea3b99934112208a0a21538c792909c
Signed-off-by: Torbjörn Svensson <azoff@svenskalinuxforeningen.se>
This commit is contained in:
Torbjörn Svensson 2020-11-04 23:01:45 +01:00 committed by Jonah Graham
parent 821c7e2277
commit 7d7d21cc67
5 changed files with 21 additions and 60 deletions

View file

@ -8,3 +8,4 @@ Bundle-RequiredExecutionEnvironment: JavaSE-11
Export-Package: org.eclipse.cdt.serial
Automatic-Module-Name: org.eclipse.cdt.native.serial
Bundle-Localization: plugin
Require-Bundle: org.eclipse.cdt.core.native;bundle-version="6.0.100"

View file

@ -50,13 +50,6 @@ JNIEXPORT void JNICALL Java_org_eclipse_cdt_serial_SerialPort_write0(JNIEnv *, j
*/
JNIEXPORT void JNICALL Java_org_eclipse_cdt_serial_SerialPort_write1(JNIEnv *, jobject, jlong, jbyteArray, jint, jint);
/*
* Class: org_eclipse_cdt_serial_SerialPort
* Method: getPortName
* Signature: (I)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_org_eclipse_cdt_serial_SerialPort_getPortName(JNIEnv *, jclass, jint);
#ifdef __cplusplus
}
#endif

View file

@ -520,38 +520,3 @@ JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jb
CloseHandle(olp.hEvent);
#endif
}
#ifdef __MINGW32__
JNIEXPORT jstring FUNC(getPortName)(JNIEnv *env, jclass cls, jint i) {
HKEY key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_READ, &key) != ERROR_SUCCESS) {
// There are none
return NULL;
}
wchar_t name[256];
DWORD len = sizeof(name);
LONG rc = RegEnumValue(key, (DWORD)i, name, &len, NULL, NULL, NULL, NULL);
if (rc != ERROR_SUCCESS) {
if (rc != ERROR_NO_MORE_ITEMS) {
throwIOException(env, "Can not enum value");
}
RegCloseKey(key);
return NULL;
}
wchar_t value[256];
DWORD type;
len = sizeof(value);
if (RegQueryValueEx(key, name, NULL, &type, (BYTE *)value, &len) != ERROR_SUCCESS) {
throwIOException(env, "Can not query value");
RegCloseKey(key);
return NULL;
}
jstring result = (*env)->NewString(env, (jchar *)value, (jsize)wcslen(value));
RegCloseKey(key);
return result;
}
#endif

View file

@ -28,6 +28,7 @@ import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.cdt.serial.internal.Messages;
import org.eclipse.cdt.utils.WindowsRegistry;
/**
* @since 5.8
@ -223,8 +224,6 @@ public class SerialPort {
private native void write1(long handle, byte[] b, int off, int len) throws IOException;
private static native String getPortName(int i) throws IOException;
private static String[] listDevs(final Pattern pattern) {
File dev = new File("/dev"); //$NON-NLS-1$
File[] files = dev.listFiles(new FilenameFilter() {
@ -257,24 +256,27 @@ public class SerialPort {
} else if (osName.equals("Linux")) { //$NON-NLS-1$
return listDevs(Pattern.compile("(ttyUSB|ttyACM|ttyS).*")); //$NON-NLS-1$
} else if (osName.startsWith("Windows")) { //$NON-NLS-1$
List<String> ports = new ArrayList<>();
int i = 0;
String name = null;
do {
try {
name = getPortName(i++);
if (name != null) {
ports.add(name);
final WindowsRegistry registry = WindowsRegistry.getRegistry();
if (registry != null) {
final String subKey = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; //$NON-NLS-1$
List<String> ports = new ArrayList<>();
int i = 0;
String valueName = null;
String value = null;
do {
valueName = registry.getLocalMachineValueName(subKey, i++);
if (valueName != null) {
value = registry.getLocalMachineValue(subKey, valueName);
if (value != null) {
ports.add(value);
}
}
} catch (IOException e) {
// TODO log the exception
e.printStackTrace();
}
} while (name != null);
return ports.toArray(new String[ports.size()]);
} else {
return new String[0];
} while (valueName != null && value != null);
return ports.toArray(new String[ports.size()]);
}
}
return new String[0];
}
/**