Java源码示例:com.sun.jna.platform.win32.Advapi32Util
示例1
/**
* Get Integer registry value(REG_DWORD)
*
* @param keyPath
* @param keyName
* @return
*/
@Override
public int getIntValue(
String rootKey,
String keyPath,
String keyName ) {
checkKeyExists(rootKey, keyPath, keyName);
try {
return Advapi32Util.registryGetIntValue(getHKey(rootKey), keyPath, keyName);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Registry key is not of type Integer. "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例2
/**
* Get Long registry value(REG_QWORD)
*
* @param keyPath
* @param keyName
* @return
*/
@Override
public long getLongValue(
String rootKey,
String keyPath,
String keyName ) {
checkKeyExists(rootKey, keyPath, keyName);
try {
return Advapi32Util.registryGetLongValue(getHKey(rootKey), keyPath, keyName);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Registry key is not of type Long. "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例3
/**
* Get Binary registry value(REG_BINARY)
*
* @param keyPath
* @param keyName
* @return
*/
@Override
public byte[] getBinaryValue(
String rootKey,
String keyPath,
String keyName ) {
checkKeyExists(rootKey, keyPath, keyName);
try {
return Advapi32Util.registryGetBinaryValue(getHKey(rootKey), keyPath, keyName);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Registry key is not of type Binary. "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例4
/**
* Creates path in the registry.
*
* Will fail if the path exists already.
*
* If want to create a nested path(for example "Software\\MyCompany\\MyApplication\\Configuration"),
* it is needed to call this method for each path token(first for "Software\\MyCompany", then for "Software\\MyCompany\\MyApplication" etc)
*
* @param keyPath
*/
@Override
public void createPath(
String rootKey,
String keyPath ) {
log.info("Create regestry key path: " + getDescription(rootKey, keyPath, null));
int index = keyPath.lastIndexOf("\\");
if (index < 1) {
throw new RegistryOperationsException("Invalid path '" + keyPath + "'");
}
String keyParentPath = keyPath.substring(0, index);
String keyName = keyPath.substring(index + 1);
checkPathDoesNotExist(rootKey, keyPath);
try {
Advapi32Util.registryCreateKey(getHKey(rootKey), keyParentPath, keyName);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't create registry key. "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例5
/**
* Applies a String(REG_SZ) value on the specified key.
*
* The key will be created if does not exist.
* The key type will be changed if needed.
*
* @param keyPath
* @param keyName
* @param keyValue
*/
@Override
public void setStringValue(
String rootKey,
String keyPath,
String keyName,
String keyValue ) {
log.info("Set String value '" + keyValue + "' on: " + getDescription(rootKey, keyPath, keyName));
try {
Advapi32Util.registrySetStringValue(getHKey(rootKey), keyPath, keyName, keyValue);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't set registry String value '" + keyValue
+ "' to: "
+ getDescription(rootKey, keyPath, keyName),
re);
}
}
示例6
/**
* Applies a Integer(REG_DWORD) value on the specified key.
*
* The key will be created if does not exist.
* The key type will be changed if needed.
*
* @param keyPath
* @param keyName
* @param keyValue
*/
@Override
public void setIntValue(
String rootKey,
String keyPath,
String keyName,
int keyValue ) {
log.info("Set Intger value '" + keyValue + "' on: " + getDescription(rootKey, keyPath, keyName));
try {
Advapi32Util.registrySetIntValue(getHKey(rootKey), keyPath, keyName, keyValue);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't set registry Integer value '" + keyValue
+ "' to: "
+ getDescription(rootKey, keyPath, keyName),
re);
}
}
示例7
/**
* Applies a Long(REG_QWORD) value on the specified key.
*
* The key will be created if does not exist.
* The key type will be changed if needed.
*
* @param keyPath
* @param keyName
* @param keyValue
*/
@Override
public void setLongValue(
String rootKey,
String keyPath,
String keyName,
long keyValue ) {
log.info("Set Long value '" + keyValue + "' on: " + getDescription(rootKey, keyPath, keyName));
try {
Advapi32Util.registrySetLongValue(getHKey(rootKey), keyPath, keyName, keyValue);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't set registry Long value '" + keyValue + "' to: "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例8
/**
* Applies a Binary(REG_BINARY) value on the specified key.
*
* The key will be created if does not exist.
* The key type will be changed if needed.
*
* @param keyPath
* @param keyName
* @param keyValue
*/
@Override
public void setBinaryValue(
String rootKey,
String keyPath,
String keyName,
byte[] keyValue ) {
log.info("Set Binary value '" + Arrays.toString(keyValue) + "' on: "
+ getDescription(rootKey, keyPath, keyName));
try {
Advapi32Util.registrySetBinaryValue(getHKey(rootKey), keyPath, keyName, keyValue);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't set registry binary value to: "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例9
/**
* Delete a registry key
*
* @param keyPath
* @param keyName
*/
@Override
public void deleteKey(
String rootKey,
String keyPath,
String keyName ) {
log.info("Delete key: " + getDescription(rootKey, keyPath, keyName));
try {
Advapi32Util.registryDeleteValue(getHKey(rootKey), keyPath, keyName);
} catch (RuntimeException re) {
throw new RegistryOperationsException("Couldn't delete registry key. "
+ getDescription(rootKey, keyPath, keyName), re);
}
}
示例10
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);
}
}
示例11
@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;
}
}
示例12
/**
* 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;
}
}
示例13
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 "";
}
示例14
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 "";
}
示例15
/**
* 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;
}
示例16
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;
}
}
示例17
/**
* 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);
}
}
示例18
private void checkPathDoesNotExist(
String rootKey,
String keyPath ) {
if (Advapi32Util.registryKeyExists(getHKey(rootKey), keyPath)) {
throw new RegistryOperationsException("Registry path already exists. "
+ getDescription(rootKey, keyPath, null));
}
}
示例19
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;
}
示例20
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;
}
示例21
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;
}
示例22
/**
* 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;
}
}
示例23
@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;
}
}
示例24
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;
}
示例25
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;
}
示例26
private static void registryDeleteKey(WinReg.HKEY hKey, String keyName) {
boolean exists = Advapi32Util.registryKeyExists(hKey, keyName);
if (exists) {
Advapi32Util.registryDeleteKey(hKey, keyName);
}
}
示例27
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);
}
}
示例28
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");
}
示例29
private static void addAllJavaHomesOnWindows(final String keyJre, final Set<String> javaHomes) {
if (Advapi32Util.registryKeyExists(HKEY_LOCAL_MACHINE, keyJre)) {
javaHomes.addAll(findJavaHomesOnWindows(keyJre, Advapi32Util.registryGetKeys(HKEY_LOCAL_MACHINE, keyJre)));
}
}