regtest.java /* * Copyright (c) 2001-2005 Servertec. All Rights Reserved. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * THIS NOTICE MUST NOT BE ALTERED NOR REMOVED. * * CopyrightVersion 1.0 */ import stec.sfc.Win32.RegistryKey; import stec.sfc.Win32.RegistryValueType; public class regtest { private static byte[] getBytes(String s) { byte[] sbytes = s.getBytes(); byte[] bytes = new byte[sbytes.length + 1]; System.arraycopy(sbytes, 0, bytes, 0, sbytes.length); bytes[sbytes.length] = 0; return bytes; } public static void main(String[] args) throws Exception { System.out.println("create key: HKEY_LOCAL_MACHINE\\SOFTWARE\\sfc_test_key"); RegistryKey key = RegistryKey.LOCAL_MACHINE.createKey("SOFTWARE\\sfc_test_key"); System.out.println("create subkey: HKEY_LOCAL_MACHINE\\SOFTWARE\\sfc_test_key\\sfc_test_subkey"); RegistryKey subkey = key.createKey("sfc_test_subkey"); subkey.close(); System.out.println("add values: username=admin"); key.setString("username", "admin"); System.out.println("add values: password=admin"); key.setString("password", "admin"); dump(key); System.out.println("delete subkey: HKEY_LOCAL_MACHINE\\SOFTWARE\\sfc_test_key\\sfc_test_subkey"); key.deleteKey("sfc_test_subkey", true); System.out.println("delete value: username"); key.deleteValue("username"); dump(key); key.close(); try { System.out.println("delete key: HKEY_LOCAL_MACHINE\\SOFTWARE\\sfc_test_key"); RegistryKey.LOCAL_MACHINE.deleteKey("SOFTWARE\\sfc_test_key", true); } catch(Exception ex) { ex.printStackTrace(); } } private static void dump(RegistryKey key) throws Exception { int count = key.getSubKeyCount(); System.out.println("key count: " + count); for(int i = 0; i < count; i++) { System.out.println("[" + i + "] " + key.getSubKeyName(i)); } count = key.getValueCount(); System.out.println("value count: " + count); String name; int type; int length; byte[] buffer; for(int i = 0; i < count; i++) { name = key.getValueName(i); System.out.println("name[" + i + "] " + name); type = key.getValueDataType(i); if(type == RegistryValueType.STRING) { System.out.println("value[" + i + "] " + key.getString(name)); } else { System.out.println("type[" + i + "] " + type); length = key.getValueDataLength(i); System.out.println("length[" + i + "] " + length); buffer = new byte[length + 1]; length = key.getValueData(i, buffer); System.out.println("value[" + i + "] " + new String(buffer, 0, length)); } } } }