private String getVlcInstallDir() { try { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, VLC_REGISTRY_KEY, VLC_INSTALL_DIR_KEY); } catch(Exception e) { return null; } }
private String getChromeVersionFromRegistry() { try { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome", "version"); } catch (Win32Exception e) { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Google\\Chrome\\BLBeacon", "version"); } }
private String getIeVersionFromRegistry() { try { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Internet Explorer", "svcVersion"); } catch (Win32Exception e) { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Internet Explorer", "version"); } }
private String getChromeVersionFromRegistry() { try { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Google Chrome", "version"); } catch (Win32Exception e) { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Google\\Chrome\\BLBeacon", "version"); } }
private String getIeVersionFromRegistry() { try { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Internet Explorer", "svcVersion"); } catch (Win32Exception e) { return Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Internet Explorer", "version"); } }
/** * Get a registry REG_SZ value. * * @param root * Root key. * @param key * Registry path. * @param value * Name of the value to retrieve. * @return String value. */ public static String registryGetStringValue(HKEY root, String key, String value) { return registryGetStringValue(root, key, value, 0); }
private List<String> searchFirefoxVersions() { List<String> firefoxInstallations = new ArrayList<>(); String out = OSCommand.executeCommandAndWait(new String[] {"REG", "QUERY", "HKCR", "/f", "FirefoxHTML", "/k", "/c"}); for (String line: out.split("\n")) { if (line.startsWith("HKEY_CLASSES_ROOT")) { String keyPath = line.replace("HKEY_CLASSES_ROOT\\", "").trim(); try { String firefoxPath = Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, keyPath + "\\shell\\open\\command", ""); firefoxPath = firefoxPath.split(".exe\"")[0].replace("\"", "") + ".exe"; firefoxInstallations.add(firefoxPath); } catch (Win32Exception e) {} } } return firefoxInstallations; }
private List<String> searchFirefoxVersions() { List<String> firefoxInstallations = new ArrayList<>(); String out = OSCommand.executeCommandAndWait(new String[] {"REG", "QUERY", "HKCR", "/f", "FirefoxHTML", "/k", "/c"}); for (String line: out.split("\n")) { if (line.startsWith("HKEY_CLASSES_ROOT")) { String keyPath = line.replace("HKEY_CLASSES_ROOT\\", "").trim(); try { String firefoxPath = Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, keyPath + "\\shell\\open\\command", ""); firefoxPath = firefoxPath.split(".exe\"")[0].replace("\"", "") + ".exe"; firefoxInstallations.add(firefoxPath); } catch (Win32Exception e) {} } } return firefoxInstallations; }
/** * Set the file location only if it does not exist or has changed. * * @param eventMessageFile The message file location in the file system * @param categoryMessageFile The message file location in the file system * @param eventSourceKeyPath The registry path */ private void setVariableKeys(String eventMessageFile, String categoryMessageFile, String eventSourceKeyPath) { if (!Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE) || !Advapi32Util .registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE) .equalsIgnoreCase(eventMessageFile)) { Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, EVENT_MESSAGE_FILE, eventMessageFile); } if (!Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE) || !Advapi32Util .registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE) .equalsIgnoreCase(categoryMessageFile)) { Advapi32Util.registrySetStringValue(WinReg.HKEY_LOCAL_MACHINE, eventSourceKeyPath, CATEGORY_MESSAGE_FILE, categoryMessageFile); } }
browserList.put(BrowserType.CHROME, new ArrayList<>()); String chromePath = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Classes\\ChromeHTML\\shell\\open\\command", ""); chromePath = chromePath.split(".exe\"")[0].replace("\"", "") + ".exe"; String version; Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\IEXPLORE.EXE", ""); String version = getIeVersionFromRegistry(); String version = Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\MicrosoftEdge\\Main", "EdgeSwitchingOSBuildNumber"); browserList.put(BrowserType.EDGE, Arrays.asList(new BrowserInfo(BrowserType.EDGE, extractEdgeVersion(version), null))); } catch (Win32Exception | ConfigurationException e) {}
@Test public void testDoesKeyNeedUpdated_ExistingCmdNewer() throws Exception { TEST_EXE = File.createTempFile(TEST_CMD_NAME, ".cmd"); Mockito.when(FileUtils.contentEquals(TEST_EXE, mockCmdFile)).thenReturn(false); Mockito.when(mockCmdFile.getPath()).thenReturn(TEST_CMD_NAME); Mockito.when(mockCmdFile.lastModified()).thenReturn(TEST_EXE.lastModified() - 1000); Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenReturn(TEST_EXE.getPath() + " \"%1\""); Assert.assertFalse(WindowsStartup.doesKeyNeedUpdated(mockCmdFile)); }
@Test public void testDoesKeyNeedUpdated_ExistingCmdOlder() throws Exception { TEST_EXE = File.createTempFile(TEST_CMD_NAME, ".cmd"); Mockito.when(FileUtils.contentEquals(TEST_EXE, mockCmdFile)).thenReturn(false); Mockito.when(mockCmdFile.getPath()).thenReturn(TEST_CMD_NAME); Mockito.when(mockCmdFile.lastModified()).thenReturn(TEST_EXE.lastModified() + 1000); Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenReturn(TEST_EXE.getPath() + " \"%1\""); Assert.assertTrue(WindowsStartup.doesKeyNeedUpdated(mockCmdFile)); }
private static Set<String> findJavaHomesOnWindows(final String keyJavaHome, final String[] keys) { final Set<String> javaHomes = new HashSet<>(keys.length); for (final String key : keys) { if (Advapi32Util.registryKeyExists(HKEY_LOCAL_MACHINE, keyJavaHome + "\\" + key)) { final String javaHome = Advapi32Util.registryGetStringValue(HKEY_LOCAL_MACHINE, keyJavaHome + "\\" + key, "JavaHome"); if (StringUtils.isNoneBlank(javaHome)) { if (new File(javaHome).exists()) { javaHomes.add(javaHome); } } } } return javaHomes; }
@Test public void testDoesKeyNeedUpdated_DiffFilesSameContents() throws Exception { TEST_EXE = File.createTempFile(TEST_CMD_NAME, ".cmd"); Mockito.when(FileUtils.contentEquals(TEST_EXE, mockCmdFile)).thenReturn(true); Mockito.when(mockCmdFile.getPath()).thenReturn(TEST_CMD_NAME); Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenReturn(TEST_EXE.getPath() + " \"%1\""); Assert.assertFalse(WindowsStartup.doesKeyNeedUpdated(mockCmdFile)); }
@Test public void testDoesKeyNeedUpdated_SameFile() throws Exception { TEST_EXE = File.createTempFile(TEST_CMD_NAME, ".cmd"); Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenReturn(TEST_EXE.getPath() + " \"%1\""); Assert.assertFalse(WindowsStartup.doesKeyNeedUpdated(TEST_EXE)); }
@Test public void testDoesKeyNeedUpdated_ExistingCmdGone() throws Exception { Mockito.when(mockCmdFile.getPath()).thenReturn(TEST_CMD_NAME); Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenReturn(TEST_CMD_NAME + " \"%1\""); Assert.assertTrue(WindowsStartup.doesKeyNeedUpdated(mockCmdFile)); }
@Test public void testDoesKeyNeedUpdated_NoKeyExists() throws Exception { Mockito.when(Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER, WindowsStartup.VSOI_KEY, "")).thenThrow(Win32Exception.class); Assert.assertTrue(WindowsStartup.doesKeyNeedUpdated(mockCmdFile)); }
/** * Get String registry value(REG_SZ) * * @param keyPath * @param keyName * @return */ @Override public String getStringValue( String rootKey, String keyPath, String keyName ) { checkKeyExists(rootKey, keyPath, keyName); try { return Advapi32Util.registryGetStringValue(getHKey(rootKey), keyPath, keyName); } catch (RuntimeException re) { throw new RegistryOperationsException("Registry key is not of type String. " + getDescription(rootKey, keyPath, keyName), re); } }
/** * Get a registry REG_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ private static String registryGet32StringValue(com.sun.jna.platform.win32.WinReg.HKEY root, String key, String value) throws com.sun.jna.platform.win32.Win32Exception { com.sun.jna.platform.win32.WinReg.HKEYByReference phkKey = new com.sun.jna.platform.win32.WinReg.HKEYByReference(); int rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, com.sun.jna.platform.win32.WinNT.KEY_READ | com.sun.jna.platform.win32.WinNT.KEY_WOW64_32KEY, phkKey); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.Win32Exception(rc); } try { return com.sun.jna.platform.win32.Advapi32Util.registryGetStringValue(phkKey.getValue(), value); } finally { rc = com.sun.jna.platform.win32.Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != com.sun.jna.platform.win32.W32Errors.ERROR_SUCCESS) { throw new com.sun.jna.platform.win32.Win32Exception(rc); } } }
return registryGetStringValue(phkKey.getValue(), value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());