Code for accessing windows registry throwing error

0

I have a chunk of code which is called at my application startup to find the ProgramData folder of the users PC and to create the application.log file for my app.

Problem is that in one of the users PC having windows 7, its throwing nullpoint error in the Java tracing pop up. I am not sure how to recreate or debug this issue, as this is the step to create the log file and put in ProgramData folder.

Any help is really appreciated!

    final int HKEY_LOCAL_MACHINE = 0x80000002;
    final int KEY_QUERY_VALUE = 1;
    final Preferences systemRoot = Preferences.systemRoot();
    final Preferences userRoot = Preferences.userRoot();
    final Class clz = userRoot.getClass();
    String appData = null;
    try {
        Method openKey = clz.getDeclaredMethod("WindowsRegOpenKey",
                new Class[] { int.class, byte[].class, int.class });
        openKey.setAccessible(true);
        Method closeKey = clz.getDeclaredMethod("WindowsRegCloseKey",
                new Class[] { int.class });
        closeKey.setAccessible(true);
        Method queryKey = clz.getDeclaredMethod("WindowsRegQueryValueEx",
                new Class[] { int.class, byte[].class });
        queryKey.setAccessible(true); 
        int[] result = (int[]) openKey
                .invoke(
                        systemRoot,
                        new Object[] {
                                new Integer(HKEY_LOCAL_MACHINE),
                                toCstr("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders"),
                                new Integer(KEY_QUERY_VALUE) });
    int handle = result[0];
    if(handle!=0){
        byte[] bytes =(byte[]) queryKey.invoke(
                systemRoot,
                new Object[] {
                        handle,
                        toCstr("Common AppData")});
        appData = new String(bytes,"US-ASCII");
        closeKey.invoke(systemRoot,new Object[]{handle});
    }
    } catch (Exception e1) {
        //log.error(e1.getStackTrace());
    }
java
asked on Stack Overflow Mar 5, 2018 by Rajesh Singh • edited Mar 5, 2018 by timiTao

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0