mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-08-13 03:05:39 +02:00
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:
parent
821c7e2277
commit
7d7d21cc67
5 changed files with 21 additions and 60 deletions
|
@ -8,3 +8,4 @@ Bundle-RequiredExecutionEnvironment: JavaSE-11
|
||||||
Export-Package: org.eclipse.cdt.serial
|
Export-Package: org.eclipse.cdt.serial
|
||||||
Automatic-Module-Name: org.eclipse.cdt.native.serial
|
Automatic-Module-Name: org.eclipse.cdt.native.serial
|
||||||
Bundle-Localization: plugin
|
Bundle-Localization: plugin
|
||||||
|
Require-Bundle: org.eclipse.cdt.core.native;bundle-version="6.0.100"
|
||||||
|
|
|
@ -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);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -520,38 +520,3 @@ JNIEXPORT void JNICALL FUNC(write1)(JNIEnv *env, jobject jobj, jlong jhandle, jb
|
||||||
CloseHandle(olp.hEvent);
|
CloseHandle(olp.hEvent);
|
||||||
#endif
|
#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
|
|
||||||
|
|
Binary file not shown.
|
@ -28,6 +28,7 @@ import java.util.Map;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.eclipse.cdt.serial.internal.Messages;
|
import org.eclipse.cdt.serial.internal.Messages;
|
||||||
|
import org.eclipse.cdt.utils.WindowsRegistry;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 5.8
|
* @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 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) {
|
private static String[] listDevs(final Pattern pattern) {
|
||||||
File dev = new File("/dev"); //$NON-NLS-1$
|
File dev = new File("/dev"); //$NON-NLS-1$
|
||||||
File[] files = dev.listFiles(new FilenameFilter() {
|
File[] files = dev.listFiles(new FilenameFilter() {
|
||||||
|
@ -257,24 +256,27 @@ public class SerialPort {
|
||||||
} else if (osName.equals("Linux")) { //$NON-NLS-1$
|
} else if (osName.equals("Linux")) { //$NON-NLS-1$
|
||||||
return listDevs(Pattern.compile("(ttyUSB|ttyACM|ttyS).*")); //$NON-NLS-1$
|
return listDevs(Pattern.compile("(ttyUSB|ttyACM|ttyS).*")); //$NON-NLS-1$
|
||||||
} else if (osName.startsWith("Windows")) { //$NON-NLS-1$
|
} else if (osName.startsWith("Windows")) { //$NON-NLS-1$
|
||||||
List<String> ports = new ArrayList<>();
|
final WindowsRegistry registry = WindowsRegistry.getRegistry();
|
||||||
int i = 0;
|
if (registry != null) {
|
||||||
String name = null;
|
final String subKey = "HARDWARE\\DEVICEMAP\\SERIALCOMM"; //$NON-NLS-1$
|
||||||
do {
|
|
||||||
try {
|
List<String> ports = new ArrayList<>();
|
||||||
name = getPortName(i++);
|
int i = 0;
|
||||||
if (name != null) {
|
String valueName = null;
|
||||||
ports.add(name);
|
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) {
|
} while (valueName != null && value != null);
|
||||||
// TODO log the exception
|
return ports.toArray(new String[ports.size()]);
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
|
||||||
} while (name != null);
|
|
||||||
return ports.toArray(new String[ports.size()]);
|
|
||||||
} else {
|
|
||||||
return new String[0];
|
|
||||||
}
|
}
|
||||||
|
return new String[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue