/** * 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) { registrySetStringValue(root, keyPath, name, value, 0); }
/** * 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); } }
/** * 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); } }
/** * 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, 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); } } }
registrySetStringValue(phkKey.getValue(), name, value); } finally { rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());