1 /* 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdlib.h> 27 #include <windows.h> 28 #include "jni.h" 29 #include "jni_util.h" 30 #include "jvm.h" 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegOpenKey 35 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey, jint securityMask) { 36 HKEY handle; 37 char* str; 38 int tmp[2]; 39 int errorCode=-1; 40 jintArray result; 41 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 42 CHECK_NULL_RETURN(str, NULL); 43 errorCode = RegOpenKeyEx((HKEY)hKey, str, 0, securityMask, &handle); 44 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 45 tmp[0]= (int) handle; 46 tmp[1]= errorCode; 47 result = (*env)->NewIntArray(env,2); 48 if(result != NULL) { 49 (*env)->SetIntArrayRegion(env, result, 0, 2, tmp); 50 } 51 return result; 52 } 53 54 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCloseKey 55 (JNIEnv* env, jclass this_class, jint hKey) { 56 return (jint) RegCloseKey((HKEY) hKey); 57 }; 58 59 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCreateKeyEx 60 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) { 61 HKEY handle; 62 char* str; 63 int tmp[3]; 64 DWORD lpdwDisposition; 65 int errorCode; 66 jintArray result = NULL; 67 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 68 CHECK_NULL_RETURN(str, NULL); 69 errorCode = RegCreateKeyEx((HKEY)hKey, str, 0, NULL, 70 REG_OPTION_NON_VOLATILE, KEY_READ, 71 NULL, &handle, &lpdwDisposition); 72 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 73 tmp[0]= (int) handle; 74 tmp[1]= errorCode; 75 tmp[2]= lpdwDisposition; 76 result = (*env)->NewIntArray(env,3); 77 if (result != NULL) { 78 (*env)->SetIntArrayRegion(env, result, 0, 3, tmp); 79 } 80 return result; 81 } 82 83 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteKey 84 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) { 85 char* str; 86 int result; 87 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 88 CHECK_NULL_RETURN(str, -1); 89 result = RegDeleteKey((HKEY)hKey, str); 90 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 91 return result; 92 93 }; 94 95 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegFlushKey 96 (JNIEnv* env, jclass this_class, jint hKey) { 97 return RegFlushKey ((HKEY)hKey); 98 } 99 100 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryValueEx 101 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) { 102 char* valueNameStr; 103 char* buffer; 104 jbyteArray result; 105 DWORD valueType; 106 DWORD valueSize; 107 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 108 CHECK_NULL_RETURN(valueNameStr, NULL); 109 if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, NULL, 110 &valueSize) != ERROR_SUCCESS) { 111 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 112 return NULL; 113 } 114 115 buffer = (char*)malloc(valueSize); 116 117 if (buffer != NULL) { 118 if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer, 119 &valueSize) != ERROR_SUCCESS) { 120 free(buffer); 121 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 122 return NULL; 123 } 124 } else { 125 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); 126 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 127 return NULL; 128 } 129 130 if (valueType == REG_SZ) { 131 result = (*env)->NewByteArray(env, valueSize); 132 if (result != NULL) { 133 (*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer); 134 } 135 } else { 136 result = NULL; 137 } 138 free(buffer); 139 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 140 return result; 141 } 142 143 144 145 146 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegSetValueEx 147 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName, jbyteArray data) { 148 char* valueNameStr; 149 char* dataStr; 150 int size = -1; 151 int nameSize = -1; 152 int error_code = -1; 153 if ((valueName == NULL)||(data == NULL)) {return -1;} 154 size = (*env)->GetArrayLength(env, data); 155 dataStr = (*env)->GetByteArrayElements(env, data, NULL); 156 CHECK_NULL_RETURN(dataStr, -1); 157 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 158 CHECK_NULL_RETURN(valueNameStr, -1); 159 error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0, 160 REG_SZ, dataStr, size); 161 (*env)->ReleaseByteArrayElements(env, data, dataStr, 0); 162 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 163 return error_code; 164 } 165 166 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteValue 167 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) { 168 char* valueNameStr; 169 int error_code = -1; 170 if (valueName == NULL) {return -1;} 171 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 172 CHECK_NULL_RETURN(valueNameStr, -1); 173 error_code = RegDeleteValue((HKEY)hKey, valueNameStr); 174 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 175 return error_code; 176 } 177 178 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey 179 (JNIEnv* env, jclass this_class, jint hKey) { 180 jintArray result = NULL; 181 int tmp[5]; 182 int valuesNumber = -1; 183 int maxValueNameLength = -1; 184 int maxSubKeyLength = -1; 185 int subKeysNumber = -1; 186 int errorCode = -1; 187 errorCode = RegQueryInfoKey((HKEY)hKey, NULL, NULL, NULL, 188 &subKeysNumber, &maxSubKeyLength, NULL, 189 &valuesNumber, &maxValueNameLength, 190 NULL, NULL, NULL); 191 tmp[0]= subKeysNumber; 192 tmp[1]= (int)errorCode; 193 tmp[2]= valuesNumber; 194 tmp[3]= maxSubKeyLength; 195 tmp[4]= maxValueNameLength; 196 result = (*env)->NewIntArray(env,5); 197 if (result != NULL) { 198 (*env)->SetIntArrayRegion(env, result, 0, 5, tmp); 199 } 200 return result; 201 } 202 203 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumKeyEx 204 (JNIEnv* env, jclass this_class, jint hKey , jint subKeyIndex, jint maxKeyLength) { 205 int size = maxKeyLength; 206 jbyteArray result; 207 char* buffer = NULL; 208 buffer = (char*)malloc(maxKeyLength); 209 if (buffer == NULL) { 210 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); 211 return NULL; 212 } 213 if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL, 214 NULL, NULL) != ERROR_SUCCESS){ 215 free(buffer); 216 return NULL; 217 } 218 result = (*env)->NewByteArray(env, size + 1); 219 if (result != NULL) { 220 (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer); 221 } 222 free(buffer); 223 return result; 224 } 225 226 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumValue 227 (JNIEnv* env, jclass this_class, jint hKey , jint valueIndex, jint maxValueNameLength){ 228 int size = maxValueNameLength; 229 jbyteArray result; 230 char* buffer = NULL; 231 int error_code; 232 buffer = (char*)malloc(maxValueNameLength); 233 if (buffer == NULL) { 234 JNU_ThrowOutOfMemoryError(env, "native memory allocation failed"); 235 return NULL; 236 } 237 error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer, 238 &size, NULL, NULL, NULL, NULL); 239 if (error_code!= ERROR_SUCCESS){ 240 free(buffer); 241 return NULL; 242 } 243 result = (*env)->NewByteArray(env, size + 1); 244 if (result != NULL) { 245 (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer); 246 } 247 free(buffer); 248 return result; 249 } 250 251 252 #ifdef __cplusplus 253 } 254 #endif