Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright appNativa Inc. All Rights Reserved.
 *
 * This file is part of the Real-time Application Rendering Engine (RARE).
 *
 * RARE 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
    public static String readRegistryValue(boolean user, String path, String key) throws Exception {
        final Class clz = Class.forName("java.util.prefs.WindowsPreferences");
        final Field f = clz.getDeclaredField(user ? "userRoot" : "systemRoot");

        f.setAccessible(true);

        final Object root = f.get(null);
        final Method openKey = clz.getDeclaredMethod("openKey", byte[].class, int.class, int.class);

        openKey.setAccessible(true);

        final Method closeKey = clz.getDeclaredMethod("closeKey", int.class);

        closeKey.setAccessible(true);

        final Method winRegQueryValue = clz.getDeclaredMethod("WindowsRegQueryValueEx", int.class, byte[].class);

        winRegQueryValue.setAccessible(true);

        byte[] valb = null;
        String vals = null;
        Integer handle = -1;

        handle = (Integer) openKey.invoke(root, toCSTR(path), 0x20019, 0x20019);
        valb = (byte[]) winRegQueryValue.invoke(root, handle, toCSTR(key));
        vals = ((valb != null) ? new String(valb).trim() : null);
        closeKey.invoke(root, handle);

        return vals;
    }

    private static byte[] toCSTR(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;
    }
}