com.sun.jna.platform.win32.WinReg Java Examples

The following examples show how to use com.sun.jna.platform.win32.WinReg. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalRegistryOperations.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
private WinReg.HKEY getHKey(
                             String key ) {

    if (key.equalsIgnoreCase(HKEY_CLASSES_ROOT)) {
        return WinReg.HKEY_CLASSES_ROOT;
    } else if (key.equalsIgnoreCase(HKEY_CURRENT_USER)) {
        return WinReg.HKEY_CURRENT_USER;
    } else if (key.equalsIgnoreCase(HKEY_LOCAL_MACHINE)) {
        return WinReg.HKEY_LOCAL_MACHINE;
    } else if (key.equalsIgnoreCase(HKEY_USERS)) {
        return WinReg.HKEY_USERS;
    } else if (key.equalsIgnoreCase(HKEY_CURRENT_CONFIG)) {
        return WinReg.HKEY_CURRENT_CONFIG;
    } else {
        throw new RegistryOperationsException("Unsupported root key '" + key + "'");
    }
}
 
Example #2
Source File: ContextMenuTools.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isAddedToContextMenu() {
    if (!Platform.isWindows()) {
        return false;
    }
    final WinReg.HKEY REG_CLASSES_HKEY = WinReg.HKEY_LOCAL_MACHINE;
    final String REG_CLASSES_PATH = "Software\\Classes\\";
    try {
        if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY, REG_CLASSES_PATH + ".swf")) {
            return false;
        }
        String clsName = Advapi32Util.registryGetStringValue(REG_CLASSES_HKEY, REG_CLASSES_PATH + ".swf", "");
        if (clsName == null) {
            return false;
        }
        return Advapi32Util.registryKeyExists(REG_CLASSES_HKEY, REG_CLASSES_PATH + clsName + "\\shell\\ffdec");
    } catch (Win32Exception ex) {
        return false;
    }
}
 
Example #3
Source File: Options.java    From opsu with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the osu! installation directory.
 * @return the directory, or null if not found
 */
private static File getOsuInstallationDirectory() {
	if (!System.getProperty("os.name").startsWith("Win"))
		return null;  // only works on Windows

	// registry location
	final WinReg.HKEY rootKey = WinReg.HKEY_CLASSES_ROOT;
	final String regKey = "osu\\DefaultIcon";
	final String regValue = null; // default value
	final String regPathPattern = "\"(.+)\\\\[^\\/]+\\.exe\"";

	String value;
	try {
		value = Advapi32Util.registryGetStringValue(rootKey, regKey, regValue);
	} catch (Win32Exception e) {
		return null;  // key/value not found
	}
	Pattern pattern = Pattern.compile(regPathPattern);
	Matcher m = pattern.matcher(value);
	if (!m.find())
		return null;
	File dir = new File(m.group(1));
	return (dir.isDirectory()) ? dir : null;
}
 
Example #4
Source File: LibraryFinder.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
private static String findCoreLibrary() {
    String path="";
    if (Platform.isWindows()) {
    	try {
	    	path = getCPath(true);
	       	path = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "InstallDir");
    	} catch (Exception ignored) {
    		try {
		    	path = getCPath(false);
		       	path = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "InstallDir");
    		} catch (Exception ignored1) {
    			return "";
    		}
    	}
       	path = path + "CoreFoundation.dll";
       	File f = new File(path);
       	if (f.exists())
       		return path;
    } else if (Platform.isMac()) {
         return "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
    }
    return "";
}
 
Example #5
Source File: WindowsRegistryParameterProvider.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Map<String, String> readProperties() {
	if (!IS_WINDOWS) {
		return null;
	}
	try {
		// Check if key exists
		if (!Advapi32Util.registryKeyExists(WinReg.HKEY_CURRENT_USER, CONFIG_KEY)) {
			return null;
		}
		// Read values from registry
		TreeMap<String, Object> map = Advapi32Util.registryGetValues(WinReg.HKEY_CURRENT_USER, CONFIG_KEY);
		Map<String, String> values = new HashMap<>();
		map.forEach((String k, Object oV) -> {
					String v = Objects.toString(oV, null);
					values.put(k.trim(), v != null ? v.trim() : null);
				}
		);
		LogService.getRoot().fine(() -> String.format("Successfully enforced %d settings from the Windows registry.", values.size()));
		return values;
	} catch (Throwable e) {
		LogService.getRoot().log(Level.WARNING, "Failed to access the Windows registry.", e);
		return null;
	}
}
 
Example #6
Source File: LibraryFinder.java    From APICloud-Studio with GNU General Public License v3.0 6 votes vote down vote up
private static String findMobileLibrary() {
	if (Platform.isWindows()) {
		String path;
	    try {
	    	path = getMDPath(true);
	    	path = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "iTunesMobileDeviceDLL");
	    } catch (Exception ignored) {
	    	try {
		    	path = getMDPath(false);
		    	path = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, path, "iTunesMobileDeviceDLL");
	    	} catch (Exception ignored1) {
	    		log.info(ignored.getMessage());
	    		return "";
	    	}
		}
    	File f = new File(path);
    	if (f.exists())
    		return path;
	} else {
		if (Platform.isMac()) {
			return "/System/Library/PrivateFrameworks/MobileDevice.framework/MobileDevice";
		}
	}
	return "";
}
 
Example #7
Source File: Sc2RegMonitor.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the current APM originating from the Windows Registry.
 * 
 * Note: since 2.0 SC2 outputs real-time APM instead of game-time APM,
 * so no conversion is performed to convert it anymore.
 * 
 * <p><b>How to interpret the values in the registry?</b><br>
 * The digits of the result: 5 has to be subtracted from the first digit (in decimal representation), 4 has to be subtracted from the second digit,
 * 3 from the third etc.
 * If the result of a subtraction is negative, 10 has to be added.
 * Examples: 64 => 10 APM; 40 => 96 APM; 8768 => 3336 APM; 38 => 84 APM</p>
 * 
 * @return the current APM or <code>null</code> if some error occurs 
 */
public static Integer getApm() {
	try {
		final String apmString = Advapi32Util.registryGetStringValue( WinReg.HKEY_CURRENT_USER, "Software\\Razer\\Starcraft2", "APMValue" );
		int apm = 0;
		
		for ( int idx = 0, delta = 5, digit; idx < apmString.length(); idx++, delta-- ) {
			digit = apmString.charAt( idx ) - '0' - delta;
			if ( digit < 0 )
				digit += 10;
			apm = apm * 10 + digit;
		}
		
		return apm;
	} catch ( final Exception e ) {
		// Silently ignore, do not print stack trace
		return null;
	}
}
 
Example #8
Source File: LocalRegistryOperations.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
private void checkKeyExists(
                             String rootKey,
                             String keyPath,
                             String keyName ) {

    try {
        WinReg.HKEY rootHKey = getHKey(rootKey);
        if (!Advapi32Util.registryValueExists(rootHKey, keyPath, keyName)) {
            throw new RegistryOperationsException("Registry key does not exist. "
                                                  + getDescription(rootKey, keyPath, keyName));
        }
    } catch (Win32Exception e) {
        throw new RegistryOperationsException("Registry key path does not exist. "
                                              + getDescription(rootKey, keyPath, keyName), e);
    }
}
 
Example #9
Source File: JnaWinRegistry.java    From p4ic4idea with Apache License 2.0 5 votes vote down vote up
@Nullable
public static String readString(WinReg.HKEY hkey, String key, String valueName) {
    if (! isAvailable()) {
        return null;
    }
    try {
        return Advapi32Util.registryGetStringValue(hkey, key, valueName);
    } catch (Win32Exception e) {
        return null;
    }
}
 
Example #10
Source File: Sc2RegMonitor.java    From sc2gears with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the current game status from the Windows Registry.
 * @return the current game status or <code>null</code> if some error occurs 
 */
public static Integer getGameStatus() {
	try {
		return Advapi32Util.registryGetIntValue( WinReg.HKEY_CURRENT_USER, "Software\\Razer\\Starcraft2", "StartModule" );
	} catch ( final Exception e ) {
		// Silently ignore, do not print stack trace
		return null;
	}
}
 
Example #11
Source File: Configuration.java    From opsu-dance with GNU General Public License v3.0 5 votes vote down vote up
private File loadOsuInstallationDirectory() {
	if (!System.getProperty("os.name").startsWith("Win")) {
		return null;
	}

	final WinReg.HKEY rootKey = WinReg.HKEY_CLASSES_ROOT;
	final String regKey = "osu\\DefaultIcon";
	final String regValue = null; // default value
	final String regPathPattern = "\"(.+)\\\\[^\\/]+\\.exe\"";

	String value;
	try {
		value = Advapi32Util.registryGetStringValue(rootKey, regKey, regValue);
	} catch (Win32Exception ignored) {
		return null;
	}
	Pattern pattern = Pattern.compile(regPathPattern);
	Matcher m = pattern.matcher(value);
	if (!m.find()) {
		return null;
	}
	File dir = new File(m.group(1));
	if (dir.isDirectory()) {
		return dir;
	}
	return null;
}
 
Example #12
Source File: OSUtils.java    From SWET with MIT License 5 votes vote down vote up
public static int getAdvancedOptionsZoom() {
	int value = -1;
	try {
		value = Advapi32Util.registryGetIntValue(WinReg.HKEY_LOCAL_MACHINE,
				"SOFTWARE\\Microsoft\\Internet Explorer\\AdvancedOptions\\ACCESSIBILITY\\ZOOMLEVEL",
				"CheckedValue");
	} catch (Exception e) {
		e.printStackTrace();
	}
	return value;
}
 
Example #13
Source File: OSUtils.java    From SWET with MIT License 5 votes vote down vote up
private static List<String> findBrowsersInRegistry() {
	// String regPath = "SOFTWARE\\Clients\\StartMenuInternet\\";
	String regPath = is64bit
			? "SOFTWARE\\Wow6432Node\\Clients\\StartMenuInternet\\"
			: "SOFTWARE\\Clients\\StartMenuInternet\\";

	List<String> browsers = new ArrayList<>();
	String path = null;
	try {
		for (String browserName : Advapi32Util
				.registryGetKeys(WinReg.HKEY_LOCAL_MACHINE, regPath)) {
			path = Advapi32Util
					.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE,
							regPath + "\\" + browserName + "\\shell\\open\\command", "")
					.replace("\"", "");
			if (path != null && new File(path).exists()) {
				String browser = path.replaceAll("\\\\", "/")
						.replaceAll("^(?:.+)/([^/]+)(.exe)$", "$1$2");
				if (debug)
					err.println("Found browser: " + browser);
				browsers.add(path.toString());
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return browsers;
}
 
Example #14
Source File: LibraryFinder.java    From APICloud-Studio with GNU General Public License v3.0 4 votes vote down vote up
private static boolean loadPath() {
	String mdPath;
	String cfpath;
	if (Platform.isWindows()) {
		try {

			mdPath = getMDPath(true);
			mdPath = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, mdPath, "iTunesMobileDeviceDLL");
			
			cfpath = getCPath(true);
			cfpath = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, cfpath, "InstallDir");

			ANBActivator
			.getDefault()
			.getLog()
			.log(new Status(IStatus.ERROR, ANBActivator.PLUGIN_ID, 0, 
					"loadPath() mdpath = "+mdPath+ " cfpath = "+cfpath, null));

		} catch (Throwable ignored) {
			try {
				mdPath = getMDPath(false);
				mdPath = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, mdPath, "iTunesMobileDeviceDLL");
				
				cfpath = getCPath(false);
				cfpath = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, cfpath, "InstallDir");

			} catch (Throwable ignored1) {
				ignored.printStackTrace();
				ANBActivator
				.getDefault()
				.getLog()
				.log(new Status(IStatus.ERROR, ANBActivator.PLUGIN_ID, 0, 
						"windows registry path not exist", null));
				return false;
			}
		}
		File fMd = new File(mdPath);
		if (!(fMd.exists())) {
			ANBActivator
			.getDefault()
			.getLog()
			.log(new Status(IStatus.ERROR, ANBActivator.PLUGIN_ID, 0, 
					"File path not exist", null));
			return false;
		}			
		String pathEnvVar = System.getenv("Path");
		pathEnvVar = pathEnvVar +   cfpath  + ";" + fMd.getParent() ;
		
		Environment.setEnv("Path", pathEnvVar);
		ANBActivator
		.getDefault()
		.getLog()
		.log(new Status(IStatus.ERROR, ANBActivator.PLUGIN_ID, 0, 
				"Environment.setEnv pathEnvVar = " + pathEnvVar, null));
		
         String sqlite3 = cfpath + "/SQLite3.dll";
         File fsql = new File(sqlite3);
         if (fsql.exists())
           System.load(cfpath + "/SQLite3.dll");
         
         String cfnetwork = cfpath + "/CFNetwork.dll";
         File fcfn = new File(cfnetwork);
         if (fcfn.exists())
           System.load(cfpath + "/CFNetwork.dll");
    }
    return true;
}
 
Example #15
Source File: ContextMenuTools.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private static void registryDeleteKey(WinReg.HKEY hKey, String keyName) {
    boolean exists = Advapi32Util.registryKeyExists(hKey, keyName);
    if (exists) {
        Advapi32Util.registryDeleteKey(hKey, keyName);
    }
}
 
Example #16
Source File: ContextMenuTools.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
private static void registryDeleteValue(WinReg.HKEY root, String keyPath, String valueName) {
    boolean exists = Advapi32Util.registryValueExists(root, keyPath, valueName);
    if (exists) {
        Advapi32Util.registryDeleteValue(root, keyPath, valueName);
    }
}
 
Example #17
Source File: Main.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
public static void initLang() {
    if (!Configuration.locale.hasValue()) {
        if (Platform.isWindows()) {
            //Load from Installer
            String uninstKey = "{E618D276-6596-41F4-8A98-447D442A77DB}_is1";
            uninstKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + uninstKey;
            try {
                if (Advapi32Util.registryKeyExists(WinReg.HKEY_LOCAL_MACHINE, uninstKey)) {
                    if (Advapi32Util.registryValueExists(WinReg.HKEY_LOCAL_MACHINE, uninstKey, "NSIS: Language")) {
                        String installedLoc = Advapi32Util.registryGetStringValue(WinReg.HKEY_LOCAL_MACHINE, uninstKey, "NSIS: Language");
                        int lcid = Integer.parseInt(installedLoc);
                        char[] buf = new char[9];
                        int cnt = Kernel32.INSTANCE.GetLocaleInfo(lcid, Kernel32.LOCALE_SISO639LANGNAME, buf, 9);
                        String langCode = new String(buf, 0, cnt).trim().toLowerCase();

                        cnt = Kernel32.INSTANCE.GetLocaleInfo(lcid, Kernel32.LOCALE_SISO3166CTRYNAME, buf, 9);
                        String countryCode = new String(buf, 0, cnt).trim().toLowerCase();

                        List<String> langs = Arrays.asList(SelectLanguageDialog.getAvailableLanguages());
                        for (int i = 0; i < langs.size(); i++) {
                            langs.set(i, langs.get(i).toLowerCase());
                        }

                        String selectedLang = null;

                        if (langs.contains(langCode + "-" + countryCode)) {
                            selectedLang = SelectLanguageDialog.getAvailableLanguages()[langs.indexOf(langCode + "-" + countryCode)];
                        } else if (langs.contains(langCode)) {
                            selectedLang = SelectLanguageDialog.getAvailableLanguages()[langs.indexOf(langCode)];
                        }
                        if (selectedLang != null) {
                            Configuration.locale.set(selectedLang);
                        }
                    }
                }
            } catch (Exception ex) {
                //ignore
            }
        }
    }
    Locale.setDefault(Locale.forLanguageTag(Configuration.locale.get()));
    AppStrings.updateLanguage();

    Helper.decompilationErrorAdd = AppStrings.translate(Configuration.autoDeobfuscate.get() ? "deobfuscation.comment.failed" : "deobfuscation.comment.tryenable");
}