1 /* 2 * Copyright (c) 2000, 2002, 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 <jni.h> 28 #include <windows.h> 29 #ifdef __cplusplus 30 extern "C" { 31 #endif 32 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegOpenKey 33 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey, jint securityMask) { 34 HKEY handle; 35 char* str; 36 int tmp[2]; 37 int errorCode=-1; 38 jintArray result; 39 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 40 errorCode = RegOpenKeyEx((HKEY)hKey, str, 0, securityMask, &handle); 41 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 42 tmp[0]= (int) handle; 43 tmp[1]= errorCode; 44 result = (*env)->NewIntArray(env,2); 45 (*env)->SetIntArrayRegion(env, result, 0, 2, tmp); 46 return result; 47 } 48 49 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCloseKey 50 (JNIEnv* env, jclass this_class, jint hKey) { 51 return (jint) RegCloseKey((HKEY) hKey); 52 }; 53 54 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCreateKeyEx 55 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) { 56 HKEY handle; 57 char* str; 58 int tmp[3]; 59 DWORD lpdwDisposition; 60 int errorCode; 61 jintArray result; 62 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 63 errorCode = RegCreateKeyEx((HKEY)hKey, str, 0, NULL, 64 REG_OPTION_NON_VOLATILE, KEY_READ, 65 NULL, &handle, &lpdwDisposition); 66 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 67 tmp[0]= (int) handle; 68 tmp[1]= errorCode; 69 tmp[2]= lpdwDisposition; 70 result = (*env)->NewIntArray(env,3); 71 (*env)->SetIntArrayRegion(env, result, 0, 3, tmp); 72 return result; 73 } 74 75 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteKey 76 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) { 77 char* str; 78 int result; 79 str = (*env)->GetByteArrayElements(env, lpSubKey, NULL); 80 result = RegDeleteKey((HKEY)hKey, str); 81 (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0); 82 return result; 83 84 }; 85 86 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegFlushKey 87 (JNIEnv* env, jclass this_class, jint hKey) { 88 return RegFlushKey ((HKEY)hKey); 89 } 90 91 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryValueEx 92 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) { 93 char* valueNameStr; 94 char* buffer; 95 jbyteArray result; 96 DWORD valueType; 97 DWORD valueSize; 98 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 99 if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, NULL, 100 &valueSize) != ERROR_SUCCESS) { 101 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 102 return NULL; 103 } 104 105 buffer = (char*)malloc(valueSize); 106 107 if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer, 108 &valueSize) != ERROR_SUCCESS) { 109 free(buffer); 110 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 111 return NULL; 112 } 113 114 if (valueType == REG_SZ) { 115 result = (*env)->NewByteArray(env, valueSize); 116 (*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer); 117 } else { 118 result = NULL; 119 } 120 free(buffer); 121 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 122 return result; 123 } 124 125 126 127 128 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegSetValueEx 129 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName, jbyteArray data) { 130 char* valueNameStr; 131 char* dataStr; 132 int size = -1; 133 int nameSize = -1; 134 int error_code = -1; 135 if ((valueName == NULL)||(data == NULL)) {return -1;} 136 size = (*env)->GetArrayLength(env, data); 137 dataStr = (*env)->GetByteArrayElements(env, data, NULL); 138 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 139 error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0, 140 REG_SZ, dataStr, size); 141 (*env)->ReleaseByteArrayElements(env, data, dataStr, 0); 142 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 143 return error_code; 144 } 145 146 JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteValue 147 (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) { 148 char* valueNameStr; 149 int error_code = -1; 150 if (valueName == NULL) {return -1;} 151 valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL); 152 error_code = RegDeleteValue((HKEY)hKey, valueNameStr); 153 (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0); 154 return error_code; 155 } 156 157 JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey 158 (JNIEnv* env, jclass this_class, jint hKey) { 159 jintArray result; 160 int tmp[5]; 161 int valuesNumber = -1; 162 int maxValueNameLength = -1; 163 int maxSubKeyLength = -1; 164 int subKeysNumber = -1; 165 int errorCode = -1; 166 errorCode = RegQueryInfoKey((HKEY)hKey, NULL, NULL, NULL, 167 &subKeysNumber, &maxSubKeyLength, NULL, 168 &valuesNumber, &maxValueNameLength, 169 NULL, NULL, NULL); 170 tmp[0]= subKeysNumber; 171 tmp[1]= (int)errorCode; 172 tmp[2]= valuesNumber; 173 tmp[3]= maxSubKeyLength; 174 tmp[4]= maxValueNameLength; 175 result = (*env)->NewIntArray(env,5); 176 (*env)->SetIntArrayRegion(env, result, 0, 5, tmp); 177 return result; 178 } 179 180 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumKeyEx 181 (JNIEnv* env, jclass this_class, jint hKey , jint subKeyIndex, jint maxKeyLength) { 182 int size = maxKeyLength; 183 jbyteArray result; 184 char* buffer = NULL; 185 buffer = (char*)malloc(maxKeyLength); 186 if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL, 187 NULL, NULL) != ERROR_SUCCESS){ 188 free(buffer); 189 return NULL; 190 } 191 result = (*env)->NewByteArray(env, size + 1); 192 (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer); 193 free(buffer); 194 return result; 195 } 196 197 JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumValue 198 (JNIEnv* env, jclass this_class, jint hKey , jint valueIndex, jint maxValueNameLength){ 199 int size = maxValueNameLength; 200 jbyteArray result; 201 char* buffer = NULL; 202 int error_code; 203 buffer = (char*)malloc(maxValueNameLength); 204 error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer, 205 &size, NULL, NULL, NULL, NULL); 206 if (error_code!= ERROR_SUCCESS){ 207 free(buffer); 208 return NULL; 209 } 210 result = (*env)->NewByteArray(env, size + 1); 211 (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer); 212 free(buffer); 213 return result; 214 } 215 216 217 #ifdef __cplusplus 218 } 219 #endif