⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winregistrywrapper.java

📁 JDesktop Integration Components (JDIC)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    lpValueName, lpValue);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            return setResult;
        }
    }

    /**
     * Removes a named value from the specified registry key.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param valueName given value name (not null)
     * @return ERROR_SUCCESS if succedd, or error code if fail
     */
    public static int WinRegDeleteValue(int hKey, String subKey, String valueName) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_WRITE);

        if (openResult == null) {
            return -1;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return openResult[ERROR_CODE];
        } else {
            byte[] lpValueName = stringToByteArray(valueName);
            int deleteResult = RegDeleteValue(openResult[OPENED_KEY_HANDLE],
                    lpValueName);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            return deleteResult;
        }
    }

    /**
     * Retrieves information about a specified registry key.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @return array contain query result
     */
    public static int[] WinRegQueryInfoKey(int hKey, String subKey) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return null;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return openResult;
        } else {
            int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            return queryResult;
        }
    }

    /**
     * Enumerates subkeys of the specified registry key. The function retrieves information
     * about one subkey each time it is called.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param subKeyIndex index of the sub key
     * @param maxKeyLength max length of sub keys
     * @return name of the sub key
     */
    public static String WinRegEnumKeyEx(int hKey, String subKey, int subKeyIndex, int maxKeyLength) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return null;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return null;
        } else {
            byte[] keyBytes = RegEnumKeyEx(openResult[OPENED_KEY_HANDLE],
                    subKeyIndex, maxKeyLength);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            if (keyBytes != null) {
                return byteArrayToString(keyBytes);
            } else {
                return null;
            }
        }
    }

    /**
     * Enumerates the values for the specified registry key. The function copies one indexed
     * value name and data block for the key each time it is called.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param valueIndex value index
     * @param maxValueNameLength max length of the value name
     * @return value name
     */
    public static String WinRegEnumValue(int hKey, String subKey, int valueIndex, int maxValueNameLength) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return null;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return null;
        } else {
            byte[] valueBytes = RegEnumValue(openResult[OPENED_KEY_HANDLE],
                    valueIndex, maxValueNameLength);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            if (valueBytes != null) {
                return byteArrayToString(valueBytes);
            } else {
                return null;
            }
        }
    }

    /**
     * Enumerates all the sub keys under the specified registry key.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param maxKeyLength max number of sub keys
     * @return a array containing name of the sub keys
     */
    public static String[] WinRegGetSubKeys(int hKey, String subKey, int maxKeyLength) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return null;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return null;
        } else {
            int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]);
            int subKeysNum = queryResult[SUBKEYS_NUMBER];

            if (subKeysNum == 0) {
                RegCloseKey(openResult[OPENED_KEY_HANDLE]);
                return null;
            } else {
                String[] keyStrings = new String[subKeysNum];
                byte[] keyBytes;

                for (int subKeyIndex = 0; subKeyIndex < subKeysNum; subKeyIndex++) {
                    keyBytes = RegEnumKeyEx(openResult[OPENED_KEY_HANDLE],
                            subKeyIndex, maxKeyLength);
                    keyStrings[subKeyIndex] = byteArrayToString(keyBytes);
                }
                RegCloseKey(openResult[OPENED_KEY_HANDLE]);

                return keyStrings;
            }
        }
    }

    /**
     * Enumerates all the values under the specified registry key.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param maxValueLength max number of values
     * @return a string array containing the name of each value
     */
    public static String[] WinRegGetValues(int hKey, String subKey, int maxValueLength) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return null;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return null;
        } else {
            int[] queryResult = RegQueryInfoKey(openResult[OPENED_KEY_HANDLE]);
            int valuesNum = queryResult[VALUES_NUMBER];

            if (valuesNum == 0) {
                RegCloseKey(openResult[OPENED_KEY_HANDLE]);
                return null;
            } else {
                String[] valueStrings = new String[valuesNum];
                byte[] valueBytes;

                for (int valueIndex = 0; valueIndex < valuesNum; valueIndex++) {
                    valueBytes = RegEnumValue(openResult[OPENED_KEY_HANDLE],
                            valueIndex, maxValueLength);
                    valueStrings[valueIndex] = byteArrayToString(valueBytes);
                }
                RegCloseKey(openResult[OPENED_KEY_HANDLE]);

                return valueStrings;
            }
        }
    }

    /**
     * Checks whether the specified registry key exsists in the registry.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @return ERROR_SUCCESS if succedd, or error code if fail
     */
    public static int WinRegSubKeyExist(int hKey, String subKey) {
        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return ERROR_ITEM_NOTEXIST;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return ERROR_ITEM_NOTEXIST;
        } else {
            RegCloseKey(openResult[OPENED_KEY_HANDLE]);
            return ERROR_ITEM_EXIST;
        }
    }

    /**
     * Checks whether the specified value exsists in the specified registry key.
     * @param hKey specified windows registry folder constant
     * @param subKey given sub key (not null)
     * @param valueName given value name (not null)
     * @return ERROR_ITEM_EXIST if the value exists under given sub key
     */
    public static int WinRegValueExist(int hKey, String subKey, String valueName) {
        if (subKey.trim().equals("")) {
            return ERROR_ITEM_NOTEXIST;
        }

        byte[] lpSubKey = stringToByteArray(subKey);
        int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ);

        if (openResult == null) {
            return ERROR_ITEM_NOTEXIST;
        }

        if (openResult[ERROR_CODE] != ERROR_SUCCESS) {
            return ERROR_ITEM_NOTEXIST;
        } else {
            byte[] lpValueName = stringToByteArray(valueName);
            byte[] valueBytes = RegQueryValueEx(openResult[OPENED_KEY_HANDLE],
                    lpValueName);

            RegCloseKey(openResult[OPENED_KEY_HANDLE]);

            if (valueBytes == null) {
                return ERROR_ITEM_NOTEXIST;
            } else {
                if ((valueBytes.length == 1) && (valueBytes[0] == 0) && (valueName.equals("")) ){
                    return ERROR_ITEM_NOTEXIST;
                } else {
                    return ERROR_ITEM_EXIST;
                }
            }
        }
    }

    /**
     * Determines the MIME type from the data provided.
     * Now the input data comes from the specified URL object.
     * @param url given url (not null)
     * @return correponding mime type information, or null if couldn't
     */
    public static String WinFindMimeFromData(URL url) {
        byte[] urlBytes;
        byte[] result;
        String urlString = url.toString();

        urlBytes = stringToByteArray(urlString);

        result = FindMimeFromData(urlBytes, null);
        if (result != null) {
            return byteArrayToString(result);
        } else {
            byte[] dataBytes = new byte[256];

            DataInputStream inStream = null;
            try {
                inStream = new DataInputStream(url.openStream());
                // Read a buffer size of 256 bytes of data to sniff the mime type.
                inStream.read(dataBytes, 0, 256);
                inStream.close();
            } catch (IOException e) {
                // Cannot open the connection to the URL, return.
                return null;
            } finally {
                // No matter what happens, always close streams already opened.
                if (inStream != null) {
                    try {
                        inStream.close();
                    } catch (IOException e) {
                    }
                }
            }

            result = FindMimeFromData(null, dataBytes);
            if (result != null) {
                return byteArrayToString(result);
            } else {
                return null;
            }
        }
    }

    /**
     * Expands environment-variable strings and replaces them with their defined values.
     * <P>
     * E.g: "%SystemRoot%\\system32\\NOTEPAD.EXE %1" -> "C:\\system32\\NOTEPAD.EXE %1"
     * </P>
     * @param envVariable given environment variable (not null)
     * @return expression after environment variable replacement
     */
    public static String WinExpandEnvironmentStrings(String envVariable) {
        byte[] envVariableBytes = stringToByteArray(envVariable);
        byte[] resultBytes = ExpandEnvironmentStrings(envVariableBytes);

        return (byteArrayToString(resultBytes));
    }
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -