osutil-win32: get JavaHome path of current JRE
authorjinhyung.jo <jinhyung.jo@samsung.com>
Thu, 5 Nov 2015 06:08:19 +0000 (15:08 +0900)
committerJinhyung Jo <jinhyung.jo@samsung.com>
Tue, 10 Nov 2015 04:18:06 +0000 (13:18 +0900)
Currently, the emulator gets the java path by traverses the registry
subkeys of "\\SOFTWARE\\JavaSoft\\Java Runtime Environment".
This can cause side effects.
So modified to get the java path in the normal way.
It gets the JavaHome path of the CurrentVersion.

Change-Id: Ibfa41f3c939f01f3b0ffd5fe4cfa2bdb575be76a
Signed-off-by: Jinhyung Jo <jinhyung.jo@samsung.com>
tizen/src/util/osutil-win32.c

index c56bae9..7fe2661 100644 (file)
@@ -295,94 +295,96 @@ int is_wow64(void)
     fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
         GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
 
-    if (NULL != fnIsWow64Process) {
-        if (!fnIsWow64Process(GetCurrentProcess(), &result)) {
-            /* No need to handle error,
-               just check whether is this WoW64 process */
-        }
+    if (!fnIsWow64Process) {
+        WARN("GetProcAddress(IsWow64Process) failed, %s return false\n",
+             __func__);
+        return 0;
     }
+    /* No need to handle error,
+       just check whether is this WoW64 process */
+    fnIsWow64Process(GetCurrentProcess(), &result);
+
     return result;
 }
 
+/* Gets the JavaHome path from the windows registry.
+   Must call the RegOpenKeyEx by using the following flag.
+   For details, "http://stackoverflow.com/questions/10533421/
+   accessing-64-bit-registry-from-32-bit-application" */
 #define MY_KEY_WOW64_64KEY 0x0100
 static char wow64_java_path[PATH_MAX];
 bool get_java_path(char **java_path)
 {
-    int index;
     LONG res;
-    HKEY hKey, hSubKey;
-    char strChoosenName[PATH_MAX] = {0};
-    char strSubKeyName[PATH_MAX] = {0};
+    HKEY hKey;
+    char strKey[PATH_MAX] = {0};
+    char strVersion[PATH_MAX] = {0};
     char strJavaHome[PATH_MAX] = {0};
-    DWORD dwSubKeyNameMax = PATH_MAX;
     DWORD dwBufLen = PATH_MAX;
-    char strKeyList[4][64] = {
-                /* 64bit runtime */
-                "SOFTWARE\\JavaSoft\\Java Runtime Environment",
-                "SOFTWARE\\JavaSoft\\Java Development Kit",
-                /* 32bit runtime */
-                "SOFTWARE\\Wow6432Node\\JavaSoft\\Java Runtime Environment",
-                "SOFTWARE\\Wow6432Node\\JavaSoft\\Java Development Kit"
-            };
 
     if (wow64_java_path[0] != '\0') {
-        strcpy(*java_path, wow64_java_path);
+        g_strlcpy(*java_path, wow64_java_path, PATH_MAX);
         return true;
     }
 
-    for (index = 0; index < ARRAY_SIZE(strKeyList); index++) {
-        res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
-                           strKeyList[index],
-                           0,
-                           KEY_QUERY_VALUE |
-                           KEY_ENUMERATE_SUB_KEYS |
-                           MY_KEY_WOW64_64KEY,
-                           &hKey);
-        if (res == ERROR_SUCCESS) {
-            break;
-        }
+    g_strlcpy(strKey, "SOFTWARE\\JavaSoft\\Java Runtime Environment", PATH_MAX);
+
+    /* Opens above key to query the current version */
+    res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                       strKey,
+                       0,
+                       KEY_QUERY_VALUE |
+                       MY_KEY_WOW64_64KEY,
+                       &hKey);
+    if (res != ERROR_SUCCESS) {
+        WARN("Java Runtime Environment key not found\n");
+        goto javahome_not_found;
     }
 
-    if (res == ERROR_SUCCESS) {
-        index = 0;
-        do {
-            dwSubKeyNameMax = PATH_MAX;
-            res = RegEnumKeyEx(hKey,
-                               index++,
-                               (LPSTR)strSubKeyName,
-                               &dwSubKeyNameMax,
-                               NULL, NULL, NULL, NULL);
-            if (strcmp(strChoosenName, strSubKeyName) < 0) {
-                strcpy(strChoosenName, strSubKeyName);
-            }
-        } while (res == ERROR_SUCCESS);
+    /* Queries for the current version */
+    res = RegQueryValueEx(hKey,
+                          "CurrentVersion",
+                          NULL,
+                          NULL,
+                          (LPBYTE)strVersion,
+                          &dwBufLen);
+    RegCloseKey(hKey);
+    if (res != ERROR_SUCCESS) {
+        WARN("JRE CurrentVersion not found\n");
+        goto javahome_not_found;
+    }
 
-        RegOpenKeyEx(hKey, strChoosenName, 0,
-                     KEY_QUERY_VALUE | MY_KEY_WOW64_64KEY, &hSubKey);
-        RegQueryValueEx(hSubKey, "JavaHome", NULL,
+    /* Adds the current version to the key */
+    g_strlcat(strKey, "\\", PATH_MAX);
+    g_strlcat(strKey, strVersion, PATH_MAX);
+
+    /* Opens above key to query the JavaHome */
+    res = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                       strKey,
+                       0,
+                       KEY_QUERY_VALUE |
+                       MY_KEY_WOW64_64KEY,
+                       &hKey);
+    if (res == ERROR_SUCCESS) {
+        /* Queries for the JavaHome */
+        dwBufLen = PATH_MAX;
+        RegQueryValueEx(hKey, "JavaHome", NULL,
                         NULL, (LPBYTE)strJavaHome, &dwBufLen);
-        RegCloseKey(hSubKey);
-        RegCloseKey(hKey);
-    } else {
-        /* TODO:
-           get from %winDir%\System32
-           but, is this really needed?
-        */
-        DWORD dwRet = 0;
-        char strJavaHomeVar[PATH_MAX] = {0,};
-        dwRet = GetEnvironmentVariable("JAVA_HOME",
-                                       strJavaHomeVar,
-                                       PATH_MAX);
-        if (dwRet != 0 && dwRet < PATH_MAX) {
-            strcpy(strJavaHome, strJavaHomeVar);
-        }
     }
-    if (strJavaHome[0] != '\0') {
-        sprintf(*java_path, "\"%s\\bin\\javaw.exe\"", strJavaHome);
-        strcpy(wow64_java_path, *java_path);
-    } else {
-        return false;
+    RegCloseKey(hKey);
+
+javahome_not_found:
+    if (strJavaHome[0] == '\0') {
+        dwBufLen = GetEnvironmentVariable("JAVA_HOME",
+                                          strJavaHome,
+                                          PATH_MAX);
+        if (dwBufLen == 0) {
+            WARN("There is no JavaHome\n");
+            return false;
+        }
     }
-
+    g_sprintf(*java_path, "\"%s\\bin\\javaw.exe\"", strJavaHome);
+    g_strlcpy(wow64_java_path, *java_path, PATH_MAX);
+    INFO("JavaHome: %s\n", wow64_java_path);
     return true;
 }