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>
diff --git a/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
index 2c90f72..52b48f6 100644
--- a/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
+++ b/native/org.eclipse.cdt.native.serial/META-INF/MANIFEST.MF
@@ -8,3 +8,4 @@
 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"
diff --git a/native/org.eclipse.cdt.native.serial/native_src/include/org_eclipse_cdt_serial_SerialPort.h b/native/org.eclipse.cdt.native.serial/native_src/include/org_eclipse_cdt_serial_SerialPort.h
index d5da82f..44505c1 100644
--- a/native/org.eclipse.cdt.native.serial/native_src/include/org_eclipse_cdt_serial_SerialPort.h
+++ b/native/org.eclipse.cdt.native.serial/native_src/include/org_eclipse_cdt_serial_SerialPort.h
@@ -50,13 +50,6 @@
  */
 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
diff --git a/native/org.eclipse.cdt.native.serial/native_src/serial.c b/native/org.eclipse.cdt.native.serial/native_src/serial.c
index ec42c9b..91943f5 100644
--- a/native/org.eclipse.cdt.native.serial/native_src/serial.c
+++ b/native/org.eclipse.cdt.native.serial/native_src/serial.c
@@ -520,38 +520,3 @@
     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
diff --git a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
index c4cc3e4..a37480b 100755
--- a/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
+++ b/native/org.eclipse.cdt.native.serial/os/win32/x86_64/serial.dll
Binary files differ
diff --git a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
index ef18b12..80970da 100644
--- a/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
+++ b/native/org.eclipse.cdt.native.serial/src/org/eclipse/cdt/serial/SerialPort.java
@@ -28,6 +28,7 @@
 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 @@
 
 	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 @@
 		} 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];
 	}
 
 	/**