/** * Gets one of the root keys. * * @param key * key type * @return root key */ private static HKEY getRegistryRootKey(REGISTRY_ROOT_KEY key) { Advapi32 advapi32; HKEYByReference pHandle; HKEY handle = null; advapi32 = Advapi32.INSTANCE; pHandle = new WinReg.HKEYByReference(); if (advapi32.RegOpenKeyEx(rootKeyMap.get(key), null, 0, 0, pHandle) == WINERROR.ERROR_SUCCESS) { handle = pHandle.getValue(); } return (handle); }
@Override protected void doApplySize(final Size size) throws BotConfigurationException { final int width = size.x(); final int height = size.y(); final HKEYByReference key = Advapi32Util.registryGetKey(WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\BlueStacks\\Guests\\Android\\FrameBuffer\\0", WinNT.KEY_READ | WinNT.KEY_WRITE); final int w1 = Advapi32Util.registryGetIntValue(key.getValue(), "WindowWidth"); final int h1 = Advapi32Util.registryGetIntValue(key.getValue(), "WindowHeight"); final int w2 = Advapi32Util.registryGetIntValue(key.getValue(), "GuestWidth"); final int h2 = Advapi32Util.registryGetIntValue(key.getValue(), "GuestHeight"); if (w1 != width || h1 != height || w2 != width || h2 != height) { Advapi32Util.registrySetIntValue(key.getValue(), "WindowWidth", width); Advapi32Util.registrySetIntValue(key.getValue(), "WindowHeight", height); Advapi32Util.registrySetIntValue(key.getValue(), "GuestWidth", width); Advapi32Util.registrySetIntValue(key.getValue(), "GuestHeight", height); Advapi32Util.registrySetIntValue(key.getValue(), "FullScreen", 0); } throw new BotConfigurationException(String.format("Please restart %s to fix resolution", BS_WINDOW_NAME)); }
/** * Create a registry key. * * @param root Root key. * @param parentPath Path to an existing registry key. * @param keyName Key name. * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(HKEY root, String parentPath, String keyName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, parentPath, 0, WinNT.KEY_CREATE_SUB_KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryCreateKey(phkKey.getValue(), keyName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set an integer value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetIntValue(HKEY root, String keyPath, String name, int value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetIntValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetStringValue(HKEY root, String keyPath, String name, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetStringValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param value Value to write to registry. */ public static void registrySetExpandableStringValue(HKEY root, String keyPath, String name, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetExpandableStringValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a string array value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param arr Array of strings to write to registry. */ public static void registrySetStringArray(HKEY root, String keyPath, String name, String[] arr) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetStringArray(phkKey.getValue(), name, arr); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Set a binary value in registry. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param name Value name. * @param data Data to write to registry. */ public static void registrySetBinaryValue(HKEY root, String keyPath, String name, byte[] data) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registrySetBinaryValue(phkKey.getValue(), name, data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Delete a registry key. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param keyName Name of the key to delete. */ public static void registryDeleteKey(HKEY root, String keyPath, String keyName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registryDeleteKey(phkKey.getValue(), keyName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Delete a registry value. * * @param root Root key. * @param keyPath Path to an existing registry key. * @param valueName Name of the value to delete. */ public static void registryDeleteValue(HKEY root, String keyPath, String valueName) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WRITE | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { registryDeleteValue(phkKey.getValue(), valueName); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get names of the registry key's sub-keys. * * @param root Root key. * @param keyPath Path to a registry key. * @return Array of registry key names. */ public static String[] registryGetKeys(HKEY root, String keyPath) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryGetKeys(phkKey.getValue()); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a table of registry values. * * @param root Registry root. * @param keyPath Regitry key path. * @return Table of values. */ public static TreeMap<String, Object> registryGetValues(HKEY root, String keyPath) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { return registryGetValues(phkKey.getValue()); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Checks whether a registry key exists. * * @param root HKEY_LOCAL_MACHINE, etc. * @param key Path to the registry key. * @return True if the key exists. */ public static boolean registryKeyExists(HKEY root, String key) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); switch (rc) { case W32Errors.ERROR_SUCCESS: Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } }
/** * Checks whether a registry value exists. * * @param root HKEY_LOCAL_MACHINE, etc. * @param key Registry key path. * @param value Value name. * @return True if the value exists. */ public static boolean registryValueExists(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); try { switch (rc) { case W32Errors.ERROR_SUCCESS: break; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); switch (rc) { case W32Errors.ERROR_SUCCESS: case W32Errors.ERROR_INSUFFICIENT_BUFFER: return true; case W32Errors.ERROR_FILE_NOT_FOUND: return false; default: throw new Win32Exception(rc); } } finally { if (phkKey.getValue() != null && phkKey.getValue() != WinBase.INVALID_HANDLE_VALUE) { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } } }
/** * 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) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } char[] data = new char[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return Native.toString(data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_EXPAND_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static String registryGetExpandableStringValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_EXPAND_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } char[] data = new char[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return Native.toString(data); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_MULTI_SZ value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static String[] registryGetStringArray(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_MULTI_SZ) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } Memory data = new Memory(lpcbData.getValue()); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } ArrayList<String> result = new ArrayList<>(); int offset = 0; while (offset < data.size()) { String s = data.getString(offset, true); offset += s.length() * Native.WCHAR_SIZE; offset += Native.WCHAR_SIZE; result.add(s); } return result.toArray(new String[result.size()]); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry REG_BINARY value. * * @param root Root key. * @param key Registry path. * @param value Name of the value to retrieve. * @return String value. */ public static byte[] registryGetBinaryValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_BINARY) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_BINARY"); } byte[] data = new byte[lpcbData.getValue()]; rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return data; } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Get a registry DWORD value. * * @param root Root key. * @param key Registry key path. * @param value Name of the value to retrieve. * @return Integer value. */ public static int registryGetIntValue(HKEY root, String key, String value) { HKEYByReference phkKey = new HKEYByReference(); int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, key, 0, WinNT.KEY_READ | WinNT.KEY_WOW64_32KEY, phkKey); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } try { IntByReference lpcbData = new IntByReference(); IntByReference lpType = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, (char[]) null, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } if (lpType.getValue() != WinNT.REG_DWORD) { throw new RuntimeException("Unexpected registry type " + lpType.getValue() + ", expected REG_SZ"); } IntByReference data = new IntByReference(); rc = Advapi32.INSTANCE.RegQueryValueEx( phkKey.getValue(), value, 0, lpType, data, lpcbData); if (rc != W32Errors.ERROR_SUCCESS && rc != W32Errors.ERROR_INSUFFICIENT_BUFFER) { throw new Win32Exception(rc); } return data.getValue(); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } } }
/** * Create a registry key. * * @param hKey Parent key. * @param keyName Key name. * @return True if the key was created, false otherwise. */ public static boolean registryCreateKey(HKEY hKey, String keyName) { HKEYByReference phkResult = new HKEYByReference(); IntByReference lpdwDisposition = new IntByReference(); int rc = Advapi32.INSTANCE.RegCreateKeyEx(hKey, keyName, 0, null, WinNT.REG_OPTION_NON_VOLATILE, WinNT.KEY_READ, null, phkResult, lpdwDisposition); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } rc = Advapi32.INSTANCE.RegCloseKey(phkResult.getValue()); if (rc != W32Errors.ERROR_SUCCESS) { throw new Win32Exception(rc); } return WinNT.REG_CREATED_NEW_KEY == lpdwDisposition.getValue(); }
/** * The RegOpenKeyEx function opens the specified registry key. Note that key * names are not case sensitive. * * @param hKey * Handle to an open key. * @param lpSubKey * Pointer to a null-terminated string containing the name of the * subkey to open. * @param ulOptions * Reserved; must be zero. * @param samDesired * Access mask that specifies the desired access rights to the * key. The function fails if the security descriptor of the key * does not permit the requested access for the calling process. * @param phkResult * Pointer to a variable that receives a handle to the opened * key. If the key is not one of the predefined registry keys, * call the RegCloseKey function after you have finished using * the handle. * @return If the function succeeds, the return value is ERROR_SUCCESS. If * the function fails, the return value is a nonzero error code * defined in Winerror.h. */ public int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions, int samDesired, HKEYByReference phkResult);
/** * * @param hKey * @param lpSubKey * @param Reserved * @param lpClass * @param dwOptions * @param samDesired * @param lpSecurityAttributes * @param phkResult * @param lpdwDisposition * @return If the function succeeds, the return value is ERROR_SUCCESS. If * the function fails, the return value is a nonzero error code * defined in Winerror.h. */ public int RegCreateKeyEx(HKEY hKey, String lpSubKey, int Reserved, String lpClass, int dwOptions, int samDesired, SECURITY_ATTRIBUTES lpSecurityAttributes, HKEYByReference phkResult, IntByReference lpdwDisposition);
/** * The RegOpenKeyEx function opens the specified registry key. Note that key * names are not case sensitive. * * @param hKey Handle to an open key. * @param lpSubKey Pointer to a null-terminated string containing the name * of the subkey to open. * @param ulOptions Reserved; must be zero. * @param samDesired Access mask that specifies the desired access rights to * the key. The function fails if the security descriptor of the key does * not permit the requested access for the calling process. * @param phkResult Pointer to a variable that receives a handle to the * opened key. If the key is not one of the predefined registry keys, call * the RegCloseKey function after you have finished using the handle. * @return If the function succeeds, the return value is ERROR_SUCCESS. If * the function fails, the return value is a nonzero error code defined in * Winerror.h. */ public int RegOpenKeyEx(HKEY hKey, String lpSubKey, int ulOptions, int samDesired, HKEYByReference phkResult);
/** * * @param hKey hKey * @param lpSubKey lpSubKey * @param Reserved Reserved * @param lpClass lpClass * @param dwOptions dwOptions * @param samDesired samDesired * @param lpSecurityAttributes lpSecurityAttributes * @param phkResult phkResult * @param lpdwDisposition lpdwDisposition * @return If the function succeeds, the return value is ERROR_SUCCESS. If * the function fails, the return value is a nonzero error code defined in * Winerror.h. */ public int RegCreateKeyEx(HKEY hKey, String lpSubKey, int Reserved, String lpClass, int dwOptions, int samDesired, SECURITY_ATTRIBUTES lpSecurityAttributes, HKEYByReference phkResult, IntByReference lpdwDisposition);