src/windows/native/java/util/WindowsPreferences.c

Print this page


   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
   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