Java wrapper for Windows registry API RegQueryValueEx() - Java Native OS

Java examples for Native OS:Windows

Description

Java wrapper for Windows registry API RegQueryValueEx()

Demo Code


//package com.java2s;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.prefs.Preferences;

public class Main {
    private static final Preferences systemRoot = Preferences.systemRoot();
    private static Method windowsRegQueryValueEx = null;

    /**/*from w w w .j  a  v  a 2s.  c om*/
     * Java wrapper for Windows registry API RegQueryValueEx()
     * 
     * @param hKey
     *            The Native Handle
     * @param valueName
     *            The Name of value to be queried as a byte array
     * @return The value queried
     */
    public static byte[] RegQueryValueEx(int hKey, byte[] valueName) {
        try {
            return (byte[]) windowsRegQueryValueEx.invoke(systemRoot,
                    new Object[] { new Integer(hKey), valueName });
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * Java wrapper for Windows registry API RegQueryValueEx()
     * 
     * @param hKey
     *            The Native Handle
     * @param valueName
     *            The Name of value to be queried
     * @return The value queried
     */
    public static byte[] RegQueryValueEx(int hKey, String valueName) {
        return RegQueryValueEx(hKey, stringToByteArray(valueName));
    }

    /**
     * Returns this java string as a null-terminated byte array
     */
    public static byte[] stringToByteArray(String str) {
        byte[] result = new byte[str.length() + 1];
        for (int i = 0; i < str.length(); i++) {
            result[i] = (byte) str.charAt(i);
        }
        result[str.length()] = 0;
        return result;
    }
}

Related Tutorials