Bug 549913 - [win32][extract method] Extract "readRegistryDword" method
from Display to OS

Change-Id: I2c0a08dcb8aef2e34ad43ffee618369f182654ed
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java b/bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java
index 3d77d71..007cb05 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java
@@ -2248,6 +2248,23 @@
 	return OpenPrinter (pPrinterName1, phPrinter, pDefault);
 }
 
+public static final int readRegistryDword(int hkeyLocation, String key, String valueName) throws Exception {
+	if (key == null || valueName == null) throw new Exception("Registry key/valueName is null.");
+	long [] phkResult = new long [1];
+	TCHAR regKey = new TCHAR (0, key, true);
+	TCHAR lpValueName = new TCHAR (0, valueName, true);
+	if (OS.RegOpenKeyEx(hkeyLocation, regKey, 0, OS.KEY_READ, phkResult) == 0) {
+		int[] lpcbData = new int[] { 4 };
+		int[] lpData = new int[1];
+		int result = OS.RegQueryValueEx(phkResult[0], lpValueName, 0, null, lpData, lpcbData);
+		OS.RegCloseKey(phkResult[0]);
+		if (result == 0) {
+			return lpData[0];
+		}
+	}
+	throw new Exception("Registry entry not found.");
+}
+
 public static final int RegCreateKeyEx (long hKey, TCHAR lpSubKey, int Reserved, TCHAR lpClass, int dwOptions, int samDesired, long lpSecurityAttributes, long[] phkResult, long[] lpdwDisposition) {
 	char [] lpClass1 = lpClass == null ? null : lpClass.chars;
 	char [] lpSubKey1 = lpSubKey == null ? null : lpSubKey.chars;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
index 8cdf2c1..3dff3a5 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
@@ -2038,17 +2038,12 @@
 	 * Win10 onwards we can read the Dark Theme from the OS registry.
 	 */
 	if (OS.WIN32_VERSION >= OS.VERSION (10, 0)) {
-		TCHAR key = new TCHAR (0, "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", true); //$NON-NLS-1$
-		long [] phkResult = new long [1];
-		if (OS.RegOpenKeyEx(OS.HKEY_CURRENT_USER, key, 0, OS.KEY_READ, phkResult) == 0) {
-			int[] lpcbData = new int[] { 4 };
-			TCHAR buffer = new TCHAR(0, "AppsUseLightTheme", true); //$NON-NLS-1$
-			int[] lpData = new int[1];
-			int result = OS.RegQueryValueEx(phkResult[0], buffer, 0, null, lpData, lpcbData);
-			if (result == 0) {
-				isDarkTheme = (lpData[0] == 0);
-			}
-			OS.RegCloseKey(phkResult[0]);
+		try {
+			int result = OS.readRegistryDword(OS.HKEY_CURRENT_USER,
+					"Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize", "AppsUseLightTheme");
+			isDarkTheme = (result == 0);
+		} catch (Exception e) {
+			// Registry entry not found
 		}
 	}
 	return isDarkTheme;
@@ -2783,15 +2778,11 @@
 	* NOTE: X-Mouse is active when bit 1 of the UserPreferencesMask is set.
 	*/
 	boolean xMouseActive = false;
-	TCHAR key = new TCHAR (0, "Control Panel\\Desktop", true); //$NON-NLS-1$
-	long [] phKey = new long [1];
-	int result = OS.RegOpenKeyEx (OS.HKEY_CURRENT_USER, key, 0, OS.KEY_READ, phKey);
-	if (result == 0) {
-		TCHAR lpValueName = new TCHAR (0, "UserPreferencesMask", true); //$NON-NLS-1$
-		int [] lpcbData = new int [] {4}, lpData = new int [1];
-		result = OS.RegQueryValueEx (phKey [0], lpValueName, 0, null, lpData, lpcbData);
-		if (result == 0) xMouseActive = (lpData [0] & 0x01) != 0;
-		OS.RegCloseKey (phKey [0]);
+	try {
+		int result = OS.readRegistryDword(OS.HKEY_CURRENT_USER, "Control Panel\\Desktop", "UserPreferencesMask");
+		xMouseActive = (result & 0x01) != 0;
+	} catch (Exception e) {
+		// Registry entry not found
 	}
 	return xMouseActive;
 }