|
Java Concepts
|
|
Written by Vimal
|
|
Saturday, 14 June 2008 11:42 |
|
Java Registry Wrapper is a free pure java library for performing common windows registry functions. It provides the following functionalities.
- Opening Registry Keys
- Create Registry Keys
- Delete Registry Keys
- Enumerate Sub Keys
- Enumerate Registry Values
- Write/Update Registry Values.
Java Registry Wrapper was inspired by link posted on DZONE(http://lenkite.blogspot.com/2008/05/access-windows-registry-using-java.html) about accessing windows registry from pure java code. The trick was to use reflection to access private methods defined in WindowsPreference class. The trick was very good but adding reflection code everywhere in your applications where you needed to access registry was not practical. So I decided to create a Opensource Project for a Java Registry Wrapper so that accessing and manipulating windows registry values could be simple method calls. This wrapper class just uses reflection to delegate all calls to WindowsPreference class and also provides methods that directly accept string keys and values. You can download Java Registry Wrapper from Google Code. View the source code for Java Registry Wrapper here.
Below is some sample code that I wrote for demonstrating Java Registry Wrapper.
package org.snipecode.reg.test;
import org.snipecode.reg.RegUtil;
import junit.framework.TestCase;
public class RegUtilTestCase extends TestCase
{
private static void testWriteRead()
{
// Create a key
int handle = RegUtil.RegCreateKeyEx(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Java\\regutil")[RegUtil.NATIVE_HANDLE];
//close the handle
RegUtil.RegCloseKey(handle);
//open a new handle with all access
handle = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Java\\regutil",RegUtil.KEY_ALL_ACCESS)[RegUtil.NATIVE_HANDLE];
//Write a value
RegUtil.RegSetValueEx(handle, "TestName", "TestValue");
//Read the value
byte[] val = RegUtil.RegQueryValueEx(handle, "TestName");
//Check the value
System.out.println(new String(val).toString().trim());
// Close the handle
//delete the value
RegUtil.RegDeleteKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\SnipCode\\regutil");
RegUtil.RegDeleteKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\SnipCode");
}
private static void testReadEnum()
{
// Open a Handle
int[] ret = RegUtil.RegOpenKey(RegUtil.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", RegUtil.KEY_QUERY_VALUE);
int handle = ret[RegUtil.NATIVE_HANDLE];
// get the Number of Values in the key
int[] info = RegUtil.RegQueryInfoKey(handle);
int count = info[RegUtil.VALUES_NUMBER];
int maxlen = info[RegUtil.MAX_VALUE_NAME_LENGTH];
for(int index =0 ;index< count;index++)
{
// get the Name of a key
// Note to use 1 greater than the length returned by query
byte[] name = RegUtil.RegEnumValue(handle,index, maxlen+1);
System.out.print(new String(name).trim() +" = ");
// Get its Value
byte[] values = RegUtil.RegQueryValueEx(handle, name);
if(null!=values)
System.out.print(new String(values).trim());
System.out.println();
}
// Finally Close the handle
RegUtil.RegCloseKey(handle);
}
public static void main(String[] args)
{
testWriteRead();
testReadEnum();
}
}
|
|
Last Updated ( Saturday, 14 June 2008 13:37 )
|