0N/A/*
2362N/A * Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <stdlib.h>
0N/A#include <jni.h>
0N/A#include <windows.h>
0N/A#ifdef __cplusplus
0N/Aextern "C" {
0N/A#endif
0N/A JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegOpenKey
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey, jint securityMask) {
0N/A HKEY handle;
0N/A char* str;
0N/A int tmp[2];
0N/A int errorCode=-1;
0N/A jintArray result;
0N/A str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
0N/A errorCode = RegOpenKeyEx((HKEY)hKey, str, 0, securityMask, &handle);
0N/A (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);
0N/A tmp[0]= (int) handle;
0N/A tmp[1]= errorCode;
0N/A result = (*env)->NewIntArray(env,2);
0N/A (*env)->SetIntArrayRegion(env, result, 0, 2, tmp);
0N/A return result;
0N/A }
0N/A
0N/A JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCloseKey
0N/A (JNIEnv* env, jclass this_class, jint hKey) {
0N/A return (jint) RegCloseKey((HKEY) hKey);
0N/A };
0N/A
0N/A JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegCreateKeyEx
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) {
0N/A HKEY handle;
0N/A char* str;
0N/A int tmp[3];
0N/A DWORD lpdwDisposition;
0N/A int errorCode;
0N/A jintArray result;
0N/A str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
0N/A errorCode = RegCreateKeyEx((HKEY)hKey, str, 0, NULL,
0N/A REG_OPTION_NON_VOLATILE, KEY_READ,
0N/A NULL, &handle, &lpdwDisposition);
0N/A (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);
0N/A tmp[0]= (int) handle;
0N/A tmp[1]= errorCode;
0N/A tmp[2]= lpdwDisposition;
0N/A result = (*env)->NewIntArray(env,3);
0N/A (*env)->SetIntArrayRegion(env, result, 0, 3, tmp);
0N/A return result;
0N/A }
0N/A
0N/A JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteKey
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray lpSubKey) {
0N/A char* str;
0N/A int result;
0N/A str = (*env)->GetByteArrayElements(env, lpSubKey, NULL);
0N/A result = RegDeleteKey((HKEY)hKey, str);
0N/A (*env)->ReleaseByteArrayElements(env, lpSubKey, str, 0);
0N/A return result;
0N/A
0N/A };
0N/A
0N/A JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegFlushKey
0N/A (JNIEnv* env, jclass this_class, jint hKey) {
0N/A return RegFlushKey ((HKEY)hKey);
0N/A }
0N/A
0N/A JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryValueEx
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) {
0N/A char* valueNameStr;
0N/A char* buffer;
0N/A jbyteArray result;
0N/A DWORD valueType;
0N/A DWORD valueSize;
0N/A valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
0N/A if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, NULL,
0N/A &valueSize) != ERROR_SUCCESS) {
0N/A (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
0N/A return NULL;
0N/A }
0N/A
0N/A buffer = (char*)malloc(valueSize);
0N/A
0N/A if (RegQueryValueEx((HKEY)hKey, valueNameStr, NULL, &valueType, buffer,
0N/A &valueSize) != ERROR_SUCCESS) {
0N/A free(buffer);
0N/A (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
0N/A return NULL;
0N/A }
0N/A
0N/A if (valueType == REG_SZ) {
0N/A result = (*env)->NewByteArray(env, valueSize);
0N/A (*env)->SetByteArrayRegion(env, result, 0, valueSize, buffer);
0N/A } else {
0N/A result = NULL;
0N/A }
0N/A free(buffer);
0N/A (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
0N/A return result;
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegSetValueEx
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName, jbyteArray data) {
0N/A char* valueNameStr;
0N/A char* dataStr;
0N/A int size = -1;
0N/A int nameSize = -1;
0N/A int error_code = -1;
0N/A if ((valueName == NULL)||(data == NULL)) {return -1;}
0N/A size = (*env)->GetArrayLength(env, data);
0N/A dataStr = (*env)->GetByteArrayElements(env, data, NULL);
0N/A valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
0N/A error_code = RegSetValueEx((HKEY)hKey, valueNameStr, 0,
0N/A REG_SZ, dataStr, size);
0N/A (*env)->ReleaseByteArrayElements(env, data, dataStr, 0);
0N/A (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
0N/A return error_code;
0N/A }
0N/A
0N/A JNIEXPORT jint JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegDeleteValue
0N/A (JNIEnv* env, jclass this_class, jint hKey, jbyteArray valueName) {
0N/A char* valueNameStr;
0N/A int error_code = -1;
0N/A if (valueName == NULL) {return -1;}
0N/A valueNameStr = (*env)->GetByteArrayElements(env, valueName, NULL);
0N/A error_code = RegDeleteValue((HKEY)hKey, valueNameStr);
0N/A (*env)->ReleaseByteArrayElements(env, valueName, valueNameStr, 0);
0N/A return error_code;
0N/A }
0N/A
0N/A JNIEXPORT jintArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegQueryInfoKey
0N/A (JNIEnv* env, jclass this_class, jint hKey) {
0N/A jintArray result;
0N/A int tmp[5];
0N/A int valuesNumber = -1;
0N/A int maxValueNameLength = -1;
0N/A int maxSubKeyLength = -1;
0N/A int subKeysNumber = -1;
0N/A int errorCode = -1;
0N/A errorCode = RegQueryInfoKey((HKEY)hKey, NULL, NULL, NULL,
0N/A &subKeysNumber, &maxSubKeyLength, NULL,
0N/A &valuesNumber, &maxValueNameLength,
0N/A NULL, NULL, NULL);
0N/A tmp[0]= subKeysNumber;
0N/A tmp[1]= (int)errorCode;
0N/A tmp[2]= valuesNumber;
0N/A tmp[3]= maxSubKeyLength;
0N/A tmp[4]= maxValueNameLength;
0N/A result = (*env)->NewIntArray(env,5);
0N/A (*env)->SetIntArrayRegion(env, result, 0, 5, tmp);
0N/A return result;
0N/A }
0N/A
0N/A JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumKeyEx
0N/A (JNIEnv* env, jclass this_class, jint hKey , jint subKeyIndex, jint maxKeyLength) {
0N/A int size = maxKeyLength;
0N/A jbyteArray result;
0N/A char* buffer = NULL;
0N/A buffer = (char*)malloc(maxKeyLength);
0N/A if (RegEnumKeyEx((HKEY) hKey, subKeyIndex, buffer, &size, NULL, NULL,
0N/A NULL, NULL) != ERROR_SUCCESS){
0N/A free(buffer);
0N/A return NULL;
0N/A }
0N/A result = (*env)->NewByteArray(env, size + 1);
0N/A (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
0N/A free(buffer);
0N/A return result;
0N/A }
0N/A
0N/A JNIEXPORT jbyteArray JNICALL Java_java_util_prefs_WindowsPreferences_WindowsRegEnumValue
0N/A (JNIEnv* env, jclass this_class, jint hKey , jint valueIndex, jint maxValueNameLength){
0N/A int size = maxValueNameLength;
0N/A jbyteArray result;
0N/A char* buffer = NULL;
0N/A int error_code;
0N/A buffer = (char*)malloc(maxValueNameLength);
0N/A error_code = RegEnumValue((HKEY) hKey, valueIndex, buffer,
0N/A &size, NULL, NULL, NULL, NULL);
0N/A if (error_code!= ERROR_SUCCESS){
0N/A free(buffer);
0N/A return NULL;
0N/A }
0N/A result = (*env)->NewByteArray(env, size + 1);
0N/A (*env)->SetByteArrayRegion(env, result, 0, size + 1, buffer);
0N/A free(buffer);
0N/A return result;
0N/A }
0N/A
0N/A
0N/A#ifdef __cplusplus
0N/A}
0N/A#endif