JNIEXPORT jobjectArray JNICALL Java_org_netbeans_installer_utils_system_windows_WindowsRegistry_getMultiStringValue0()

in src/main/cpp/jnilib/windows/src/jni_WindowsRegistry.c [441:523]


JNIEXPORT jobjectArray JNICALL Java_org_netbeans_installer_utils_system_windows_WindowsRegistry_getMultiStringValue0(JNIEnv *jEnv, jobject jObject, jint jMode, jint jSection, jstring jKey, jstring jName) {
    HKEY  hkey   = 0;
    unsigned short* key   = getWideChars(jEnv, jKey);
    unsigned short* value = getWideChars(jEnv, jName);
    
    int	i, start, count, cnt;
    LONG regErr = 0;
    unsigned short* data = 0;
    jstring string;
    jclass strClass;
    DWORD dwType;
    DWORD size = 0;
    
    jarray result = 0;
    if(RegOpenKeyExW(getHKEY(jSection), key, 0, KEY_QUERY_VALUE | getMode(jMode), &hkey) == ERROR_SUCCESS) {
        if (RegQueryValueExW(hkey, value, NULL, &dwType, NULL, &size) == ERROR_SUCCESS) {
            if (dwType == REG_MULTI_SZ) {
                data = (unsigned short*) LocalAlloc(LPTR, size + 8);
                
                if (data != NULL) {
                    if (RegQueryValueExW(hkey, value, NULL, &dwType, (byte*) data, &size) == ERROR_SUCCESS) {
                        for (count = 0, i = 0; i < (int) size; i++) {
                            if (data[i] == '\0') { // \0 - end of a single string
                                count++;
                                if (data[i + 1] == '\0') { // two \0 's in a row - end of all strings
                                    break;
                                }
                            }
                        }
                        
                        strClass = (*jEnv)->FindClass(jEnv, "java/lang/String");
                        if (strClass != NULL) {
                            result = (*jEnv)->NewObjectArray(jEnv, (jsize) count, strClass, NULL);
                            if (result != NULL) {
                                for (cnt = 0, start = 0, i = 0; (i < (int) size) && (cnt < count); i++) {
                                    if (data[i] == '\0') {
                                        string = getStringWithLengthW(jEnv, &data[start], i - start);
                                        
                                        if (string != NULL) {
                                            (*jEnv)->SetObjectArrayElement(jEnv, (jobjectArray) result, (jsize) cnt++, string);
                                            
                                            start = i + 1;
                                            if (data[start] == '\0') {
                                                break;
                                            }
                                        } else {
                                            throwException(jEnv, "Cannot create an array element");
                                            break;
                                        }
                                    }
                                }
                            } else {
                                throwException(jEnv, "Cannot create resulting array");
                            }
                        } else {
                            throwException(jEnv, "Cannot find java.lang.String");
                        }
                    } else {
                        throwException(jEnv, "Cannot read value data");
                    }
                    FREE(data);
                } else {
                    throwException(jEnv, "Cannot allocate memory for value");
                }
            } else {
                throwException(jEnv, "Value is of wrong type");
            }
        } else {
            throwException(jEnv, "Cannot read key data");
        }
    } else {
        throwException(jEnv, "Cannot open key");
    }
    
    if (hkey != 0) {
        RegCloseKey(hkey);
    }
    
    FREE(key);
    FREE(value);
    
    return (jobjectArray) result;
}