< prev index next >

src/jdk.incubator.jpackage/windows/native/libjpackage/WindowsRegistry.cpp

Print this page




  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 <Windows.h>
  27 #include <strsafe.h>
  28 #include <tchar.h>
  29 #include <jni.h>
  30 
  31 #include "Utils.h"
  32 
  33 // Max value name size per MSDN plus NULL
  34 #define VALUE_NAME_SIZE 16384
  35 
  36 #ifdef __cplusplus
  37 extern "C" {
  38 #endif
  39 #undef jdk_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE
  40 #define jdk_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE 1L
  41 
  42     /*
  43      * Class:     jdk_jpackage_internal_WindowsRegistry
  44      * Method:    readDwordValue
  45      * Signature: (ILjava/lang/String;Ljava/lang/String;I)I
  46      */
  47     JNIEXPORT jint JNICALL Java_jdk_jpackage_internal_WindowsRegistry_readDwordValue(
  48             JNIEnv *pEnv, jclass c, jint key, jstring jSubKey, jstring jValue, jint defaultValue) {


  49         jint jResult = defaultValue;
  50 
  51         if (key != jdk_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE) {
  52             return jResult;
  53         }
  54 
  55         wstring subKey = GetStringFromJString(pEnv, jSubKey);
  56         wstring value = GetStringFromJString(pEnv, jValue);
  57 
  58         HKEY hSubKey = NULL;
  59         LSTATUS status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey.c_str(), 0,
  60                 KEY_QUERY_VALUE, &hSubKey);
  61         if (status == ERROR_SUCCESS) {
  62             DWORD dwValue = 0;
  63             DWORD cbData = sizeof (DWORD);
  64             status = RegQueryValueEx(hSubKey, value.c_str(), NULL, NULL,
  65                     (LPBYTE) & dwValue, &cbData);
  66             if (status == ERROR_SUCCESS) {
  67                 jResult = (jint) dwValue;
  68             }
  69 
  70             RegCloseKey(hSubKey);
  71         }
  72 
  73         return jResult;
  74     }
  75 
  76     /*
  77      * Class:     jdk_jpackage_internal_WindowsRegistry
  78      * Method:    openRegistryKey
  79      * Signature: (ILjava/lang/String;)J
  80      */
  81     JNIEXPORT jlong JNICALL Java_jdk_jpackage_internal_WindowsRegistry_openRegistryKey(

  82             JNIEnv *pEnv, jclass c, jint key, jstring jSubKey) {
  83         if (key != jdk_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE) {
  84             return 0;
  85         }
  86 
  87         wstring subKey = GetStringFromJString(pEnv, jSubKey);
  88         HKEY hSubKey = NULL;
  89         LSTATUS status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey.c_str(), 0,
  90                 KEY_QUERY_VALUE, &hSubKey);
  91         if (status == ERROR_SUCCESS) {
  92             return (jlong)hSubKey;
  93         }
  94 
  95         return 0;
  96     }
  97 
  98     /*
  99      * Class:     jdk_jpackage_internal_WindowsRegistry
 100      * Method:    enumRegistryValue
 101      * Signature: (JI)Ljava/lang/String;
 102      */
 103     JNIEXPORT jstring JNICALL Java_jdk_jpackage_internal_WindowsRegistry_enumRegistryValue(

 104             JNIEnv *pEnv, jclass c, jlong lKey, jint jIndex) {
 105         HKEY hKey = (HKEY)lKey;
 106         TCHAR valueName[VALUE_NAME_SIZE] = {0}; // Max value name size per MSDN plus NULL
 107         DWORD cchValueName = VALUE_NAME_SIZE;
 108         LSTATUS status = RegEnumValue(hKey, (DWORD)jIndex, valueName, &cchValueName,
 109                                       NULL, NULL, NULL, NULL);
 110         if (status == ERROR_SUCCESS) {
 111             size_t chLength = 0;
 112             if (StringCchLength(valueName, VALUE_NAME_SIZE, &chLength) == S_OK) {

 113                 return GetJStringFromString(pEnv, valueName, (jsize)chLength);
 114             }
 115         }
 116 
 117         return NULL;
 118     }
 119 
 120     /*
 121      * Class:     jdk_jpackage_internal_WindowsRegistry
 122      * Method:    closeRegistryKey
 123      * Signature: (J)V
 124      */
 125     JNIEXPORT void JNICALL Java_jdk_jpackage_internal_WindowsRegistry_closeRegistryKey(

 126             JNIEnv *pEnc, jclass c, jlong lKey) {
 127         HKEY hKey = (HKEY)lKey;
 128         RegCloseKey(hKey);
 129     }
 130 
 131     /*
 132      * Class:     jdk_jpackage_internal_WindowsRegistry
 133      * Method:    comparePaths
 134      * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
 135      */
 136      JNIEXPORT jboolean JNICALL Java_jdk_jpackage_internal_WindowsRegistry_comparePaths(

 137              JNIEnv *pEnv, jclass c, jstring jPath1, jstring jPath2) {
 138          wstring path1 = GetStringFromJString(pEnv, jPath1);
 139          wstring path2 = GetStringFromJString(pEnv, jPath2);
 140 
 141          path1 = GetLongPath(path1);
 142          path2 = GetLongPath(path2);
 143 
 144          if (path1.length() == 0 || path2.length() == 0) {
 145              return JNI_FALSE;
 146          }
 147 
 148          if (path1.length() != path2.length()) {
 149              return JNI_FALSE;
 150          }
 151 
 152          if (_tcsnicmp(path1.c_str(), path2.c_str(), path1.length()) == 0) {
 153              return JNI_TRUE;
 154          }
 155 
 156          return JNI_FALSE;


  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 <Windows.h>
  27 #include <strsafe.h>
  28 #include <tchar.h>
  29 #include <jni.h>
  30 
  31 #include "Utils.h"
  32 
  33 // Max value name size per MSDN plus NULL
  34 #define VALUE_NAME_SIZE 16384
  35 
  36 #ifdef __cplusplus
  37 extern "C" {
  38 #endif
  39 #undef jdk_incubator_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE
  40 #define jdk_incubator_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE 1L
  41 
  42     /*
  43      * Class:     jdk_incubator_jpackage_internal_WindowsRegistry
  44      * Method:    readDwordValue
  45      * Signature: (ILjava/lang/String;Ljava/lang/String;I)I
  46      */
  47     JNIEXPORT jint JNICALL
  48             Java_jdk_incubator_jpackage_internal_WindowsRegistry_readDwordValue(
  49             JNIEnv *pEnv, jclass c, jint key, jstring jSubKey,
  50             jstring jValue, jint defaultValue) {
  51         jint jResult = defaultValue;
  52 
  53         if (key != jdk_incubator_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE) {
  54             return jResult;
  55         }
  56 
  57         wstring subKey = GetStringFromJString(pEnv, jSubKey);
  58         wstring value = GetStringFromJString(pEnv, jValue);
  59 
  60         HKEY hSubKey = NULL;
  61         LSTATUS status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey.c_str(), 0,
  62                 KEY_QUERY_VALUE, &hSubKey);
  63         if (status == ERROR_SUCCESS) {
  64             DWORD dwValue = 0;
  65             DWORD cbData = sizeof (DWORD);
  66             status = RegQueryValueEx(hSubKey, value.c_str(), NULL, NULL,
  67                     (LPBYTE) & dwValue, &cbData);
  68             if (status == ERROR_SUCCESS) {
  69                 jResult = (jint) dwValue;
  70             }
  71 
  72             RegCloseKey(hSubKey);
  73         }
  74 
  75         return jResult;
  76     }
  77 
  78     /*
  79      * Class:     jdk_incubator_jpackage_internal_WindowsRegistry
  80      * Method:    openRegistryKey
  81      * Signature: (ILjava/lang/String;)J
  82      */
  83     JNIEXPORT jlong JNICALL
  84             Java_jdk_incubator_jpackage_internal_WindowsRegistry_openRegistryKey(
  85             JNIEnv *pEnv, jclass c, jint key, jstring jSubKey) {
  86         if (key != jdk_incubator_jpackage_internal_WindowsRegistry_HKEY_LOCAL_MACHINE) {
  87             return 0;
  88         }
  89 
  90         wstring subKey = GetStringFromJString(pEnv, jSubKey);
  91         HKEY hSubKey = NULL;
  92         LSTATUS status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey.c_str(), 0,
  93                 KEY_QUERY_VALUE, &hSubKey);
  94         if (status == ERROR_SUCCESS) {
  95             return (jlong)hSubKey;
  96         }
  97 
  98         return 0;
  99     }
 100 
 101     /*
 102      * Class:     jdk_incubator_jpackage_internal_WindowsRegistry
 103      * Method:    enumRegistryValue
 104      * Signature: (JI)Ljava/lang/String;
 105      */
 106     JNIEXPORT jstring JNICALL
 107             Java_jdk_incubator_jpackage_internal_WindowsRegistry_enumRegistryValue(
 108             JNIEnv *pEnv, jclass c, jlong lKey, jint jIndex) {
 109         HKEY hKey = (HKEY)lKey;
 110         TCHAR valueName[VALUE_NAME_SIZE] = {0}; // Max size per MSDN plus NULL
 111         DWORD cchValueName = VALUE_NAME_SIZE;
 112         LSTATUS status = RegEnumValue(hKey, (DWORD)jIndex, valueName,
 113                 &cchValueName, NULL, NULL, NULL, NULL);
 114         if (status == ERROR_SUCCESS) {
 115             size_t chLength = 0;
 116             if (StringCchLength(valueName, VALUE_NAME_SIZE, &chLength)
 117                     == S_OK) {
 118                 return GetJStringFromString(pEnv, valueName, (jsize)chLength);
 119             }
 120         }
 121 
 122         return NULL;
 123     }
 124 
 125     /*
 126      * Class:     jdk_incubator_jpackage_internal_WindowsRegistry
 127      * Method:    closeRegistryKey
 128      * Signature: (J)V
 129      */
 130     JNIEXPORT void JNICALL
 131             Java_jdk_incubator_jpackage_internal_WindowsRegistry_closeRegistryKey(
 132             JNIEnv *pEnc, jclass c, jlong lKey) {
 133         HKEY hKey = (HKEY)lKey;
 134         RegCloseKey(hKey);
 135     }
 136 
 137     /*
 138      * Class:     jdk_incubator_jpackage_internal_WindowsRegistry
 139      * Method:    comparePaths
 140      * Signature: (Ljava/lang/String;Ljava/lang/String;)Z
 141      */
 142      JNIEXPORT jboolean JNICALL
 143             Java_jdk_incubator_jpackage_internal_WindowsRegistry_comparePaths(
 144             JNIEnv *pEnv, jclass c, jstring jPath1, jstring jPath2) {
 145          wstring path1 = GetStringFromJString(pEnv, jPath1);
 146          wstring path2 = GetStringFromJString(pEnv, jPath2);
 147 
 148          path1 = GetLongPath(path1);
 149          path2 = GetLongPath(path2);
 150 
 151          if (path1.length() == 0 || path2.length() == 0) {
 152              return JNI_FALSE;
 153          }
 154 
 155          if (path1.length() != path2.length()) {
 156              return JNI_FALSE;
 157          }
 158 
 159          if (_tcsnicmp(path1.c_str(), path2.c_str(), path1.length()) == 0) {
 160              return JNI_TRUE;
 161          }
 162 
 163          return JNI_FALSE;
< prev index next >