emulator: Modified code about java path in WOW64
authorjinhyung.jo <jinhyung.jo@samsung.com>
Wed, 29 Jul 2015 08:30:43 +0000 (17:30 +0900)
committerJinhyung Jo <jinhyung.jo@samsung.com>
Fri, 11 Sep 2015 07:42:36 +0000 (16:42 +0900)
Use more registry keys and the environment variable
in oder to find more precise 'Java Home'.
Removed redundant code.

Change-Id: I9ef2d3e87cd47cb31ee0c4389269cca2708fcc4d
Signed-off-by: Jinhyung Jo <jinhyung.jo@samsung.com>
(cherry picked from commit 6ba1131714016b47c49e79f361c66cd25d148bc0)

include/qemu-common.h
tizen/src/emulator_common.h
util/oslib-win32.c

index 8ddb0cb..a998e8d 100644 (file)
@@ -474,11 +474,4 @@ int parse_debug_env(const char *name, int max, int initial);
 
 const char *qemu_ether_ntoa(const MACAddr *mac);
 
-#if defined(CONFIG_MARU) && defined(CONFIG_WIN32)
-#include "../tizen/src/emulator_common.h"
-
-int is_wow64(void);
-bool get_java_path(char **java_path);
-#endif
-
 #endif
index 99821be..fb9d5c3 100644 (file)
 
 #ifdef CONFIG_WIN32
 #define MY_KEY_WOW64_64KEY 0x0100
+#ifdef __cplusplus
+extern "C" {
+#endif
+int is_wow64(void);
+bool get_java_path(char **java_path);
+#ifdef __cplusplus
+}
+#endif
 #else
 #define JAVA_EXEFILE_PATH "java"
 #endif
index fed8de3..b5beed5 100644 (file)
 #include <shlobj.h>
 
 #ifdef CONFIG_MARU
+#include "tizen/src/emulator_common.h"
+
 typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
-LPFN_ISWOW64PROCESS fnIsWow64Process;
 
 int is_wow64(void)
 {
     int result = 0;
+    LPFN_ISWOW64PROCESS fnIsWow64Process;
 
     /* IsWow64Process is not available on all supported versions of Windows.
        Use GetModuleHandle to get a handle to the DLL that contains the function
        and GetProcAddress to get a pointer to the function if available. */
 
-    fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
-        GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
+    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
+        GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
 
     if (NULL != fnIsWow64Process) {
-        if (!fnIsWow64Process(GetCurrentProcess(),&result)) {
-            // handle error
-            fprintf(stderr, "Cannot find 'IsWow64Process'\n");
+        if (!fnIsWow64Process(GetCurrentProcess(), &result)) {
+            /* No need to handle error,
+               just check whether is this WoW64 process */
         }
     }
     return result;
 }
 
-bool get_java_path(char** java_path)
+static char wow64_java_path[JAVA_MAX_COMMAND_LENGTH];
+bool get_java_path(char **java_path)
 {
-    HKEY hKeyNew;
-    HKEY hKey;
-    //char strJavaRuntimePath[JAVA_MAX_COMMAND_LENGTH] = {0};
+    int index;
+    LONG res;
+    HKEY hKey, hSubKey;
     char strChoosenName[JAVA_MAX_COMMAND_LENGTH] = {0};
     char strSubKeyName[JAVA_MAX_COMMAND_LENGTH] = {0};
     char strJavaHome[JAVA_MAX_COMMAND_LENGTH] = {0};
-    int index;
     DWORD dwSubKeyNameMax = JAVA_MAX_COMMAND_LENGTH;
     DWORD dwBufLen = JAVA_MAX_COMMAND_LENGTH;
-
-    RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+    char strKeyList[4][64] = {
+                /* 64bit runtime */
                 "SOFTWARE\\JavaSoft\\Java Runtime Environment",
-                0,
-                KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | MY_KEY_WOW64_64KEY,
-                &hKey);
-    RegEnumKeyEx(hKey, 0, (LPSTR)strSubKeyName, &dwSubKeyNameMax,
-                NULL, NULL, NULL, NULL);
-    strcpy(strChoosenName, strSubKeyName);
-
-    index = 1;
-    while (ERROR_SUCCESS ==
-            RegEnumKeyEx(hKey, index, (LPSTR)strSubKeyName, &dwSubKeyNameMax,
-            NULL, NULL, NULL, NULL)) {
-        if (strcmp(strChoosenName, strSubKeyName) < 0) {
-            strcpy(strChoosenName, strSubKeyName);
+                "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);
+        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;
         }
-        index++;
     }
 
-    RegOpenKeyEx(hKey, strChoosenName, 0,
-                KEY_QUERY_VALUE | MY_KEY_WOW64_64KEY, &hKeyNew);
-    RegQueryValueEx(hKeyNew, "JavaHome", NULL,
-                    NULL, (LPBYTE)strJavaHome, &dwBufLen);
-    RegCloseKey(hKey);
+    if (res == ERROR_SUCCESS) {
+        RegEnumKeyEx(hKey, 0, (LPSTR)strSubKeyName, &dwSubKeyNameMax,
+                     NULL, NULL, NULL, NULL);
+        strcpy(strChoosenName, strSubKeyName);
+
+        index = 1;
+        while (ERROR_SUCCESS ==
+                RegEnumKeyEx(hKey, index,
+                             (LPSTR)strSubKeyName, &dwSubKeyNameMax,
+                             NULL, NULL, NULL, NULL)) {
+            if (strcmp(strChoosenName, strSubKeyName) < 0) {
+                strcpy(strChoosenName, strSubKeyName);
+            }
+            index++;
+        }
+
+        RegOpenKeyEx(hKey, strChoosenName, 0,
+                     KEY_QUERY_VALUE | MY_KEY_WOW64_64KEY, &hSubKey);
+        RegQueryValueEx(hSubKey, "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[JAVA_MAX_COMMAND_LENGTH] = {0,};
+        dwRet = GetEnvironmentVariable("JAVA_HOME",
+                                       strJavaHomeVar,
+                                       JAVA_MAX_COMMAND_LENGTH);
+        if (dwRet != 0 && dwRet < JAVA_MAX_COMMAND_LENGTH) {
+            strcpy(strJavaHome, strJavaHomeVar);
+        }
+    }
     if (strJavaHome[0] != '\0') {
-        sprintf(*java_path, "\"%s\\bin\\java\"", strJavaHome);
-        //strcpy(*java_path, strJavaHome);
-        //strcat(*java_path, "\\bin\\java");
+        sprintf(*java_path, "\"%s\\bin\\javaw.exe\"", strJavaHome);
+        strcpy(wow64_java_path, *java_path);
     } else {
         return false;
     }