home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / WDESAMPL.BIN / jre_md.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-24  |  7.9 KB  |  288 lines

  1. /*
  2.  * @(#)jre_md.c    1.6 97/05/15 David Connelly
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  7.  * modify and redistribute this software in source and binary code form,
  8.  * provided that i) this copyright notice and license appear on all copies of
  9.  * the software; and ii) Licensee does not utilize the software in a manner
  10.  * which is disparaging to Sun.
  11.  *
  12.  * This software is provided "AS IS," without a warranty of any kind. ALL
  13.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  14.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  15.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  16.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  17.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  18.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  19.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  20.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  21.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  22.  * POSSIBILITY OF SUCH DAMAGES.
  23.  *
  24.  * This software is not designed or intended for use in on-line control of
  25.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  26.  * the design, construction, operation or maintenance of any nuclear
  27.  * facility. Licensee represents and warrants that it will not use or
  28.  * redistribute the Software for such purposes.
  29.  */
  30.  
  31. /*
  32.  * Win32 specific JRE support functions
  33.  */
  34.  
  35. #include <windows.h>
  36. #include <stdlib.h>
  37. #include <jni.h>
  38. #include "jre.h"
  39.  
  40. #define JRE_KEY        "Software\\JavaSoft\\Java Runtime Environment"
  41. #define JDK_KEY        "Software\\JavaSoft\\Java Development Kit"
  42.  
  43. #define RUNTIME_LIB "javai.dll"
  44.  
  45. /* From jre_main.c */
  46. extern jboolean debug;
  47.  
  48. /* Forward Declarations */
  49. jint LoadSettings(JRESettings *set, HKEY key);
  50. jint GetSettings(JRESettings *set, const char *version, const char *keyname);
  51. char *GetStringValue(HKEY key, const char *name);
  52.  
  53. /*
  54.  * Retrieve settings from registry for current runtime version. Returns
  55.  * 0 if successful otherwise returns -1 if no installed runtime was found
  56.  * or the registry data was invalid.
  57.  */
  58. jint
  59. JRE_GetCurrentSettings(JRESettings *set)
  60. {
  61.     jint r = -1;
  62.     HKEY key;
  63.  
  64.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, JRE_KEY, 0, KEY_READ, &key) == 0) {
  65.     char *ver = GetStringValue(key, "CurrentVersion");
  66.     if (ver != 0) {
  67.         r = JRE_GetSettings(set, ver);
  68.     }
  69.     free(ver);
  70.     RegCloseKey(key);
  71.     }
  72.     return r;
  73. }
  74.  
  75. /*
  76.  * Retrieves settings from registry for specified runtime version.
  77.  * Searches for either installed JRE and JDK runtimes. Returns 0 if
  78.  * successful otherwise returns -1 if requested version of runtime
  79.  * could not be found.
  80.  */
  81. jint
  82. JRE_GetSettings(JRESettings *set, const char *version)
  83. {
  84.     if (GetSettings(set, version, JRE_KEY) != 0) {
  85.     return GetSettings(set, version, JDK_KEY);
  86.     }
  87.     return 0;
  88. }
  89.  
  90. jint
  91. GetSettings(JRESettings *set, const char *version, const char *keyname)
  92. {
  93.     HKEY key;
  94.     int r = -1;
  95.  
  96.     if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyname, 0, KEY_READ, &key) == 0) {
  97.     char *major, *minor, *micro = 0;
  98.     if (JRE_ParseVersion(version, &major, &minor, µ) == 0) {
  99.         HKEY subkey;
  100.         char *ver = JRE_MakeVersion(major, minor, 0);
  101.         set->majorVersion = major;
  102.         set->minorVersion = minor;
  103.         if (RegOpenKeyEx(key, ver, 0, KEY_READ, &subkey) == 0) {
  104.         if ((r = LoadSettings(set, subkey)) == 0) {
  105.             if (micro != 0) {
  106.             if (set->microVersion == 0 ||
  107.                 strcmp(micro, set->microVersion) != 0) {
  108.                 r = -1;
  109.             }
  110.             }
  111.         }
  112.         RegCloseKey(subkey);
  113.         }
  114.         free(ver);
  115.     }
  116.     RegCloseKey(key);
  117.     }
  118.     return r;
  119. }
  120.  
  121. /*
  122.  * Load runtime settings from specified registry key. Returns 0 if
  123.  * successful otherwise -1 if the registry data was invalid.
  124.  */
  125. static jint
  126. LoadSettings(JRESettings *set, HKEY key)
  127. {
  128.     /* Full path name of JRE home directory (required) */
  129.     set->javaHome = GetStringValue(key, "JavaHome");
  130.     if (set->javaHome == 0) {
  131.     return -1;
  132.     }
  133.     /* Full path name of JRE runtime DLL */
  134.     set->runtimeLib = GetStringValue(key, "RuntimeLib");
  135.     if (set->runtimeLib == 0) {
  136.     set->runtimeLib = JRE_GetDefaultRuntimeLib(set->javaHome);
  137.     }
  138.     /* Class path setting to override default */
  139.     set->classPath = GetStringValue(key, "ClassPath");
  140.     if (set->classPath == 0) {
  141.     set->classPath = JRE_GetDefaultClassPath(set->javaHome);
  142.     }
  143.     /* Optional JIT compiler library name */
  144.     set->compiler = GetStringValue(key, "Compiler");
  145.     /* Release micro-version */
  146.     set->microVersion = GetStringValue(key, "MicroVersion");
  147.     return 0;
  148. }
  149.  
  150. /*
  151.  * Returns string data for the specified registry value name, or
  152.  * NULL if not found.
  153.  */
  154. static char *
  155. GetStringValue(HKEY key, const char *name)
  156. {
  157.     DWORD type, size;
  158.     char *value = 0;
  159.  
  160.     if (RegQueryValueEx(key, name, 0, &type, 0, &size) == 0 &&
  161.     type == REG_SZ ) {
  162.     value = JRE_Malloc(size);
  163.     if (RegQueryValueEx(key, name, 0, 0, value, &size) != 0) {
  164.         free(value);
  165.         value = 0;
  166.     }
  167.     }
  168.     return value;
  169. }
  170.  
  171. /*
  172.  * Returns default runtime settings based on location of this program.
  173.  * Makes best attempt at determining location of runtime. Returns 0
  174.  * if successful or -1 if a runtime could not be found.
  175.  */
  176. jint
  177. JRE_GetDefaultSettings(JRESettings *set)
  178. {
  179.     char buf[MAX_PATH], *bp;
  180.     int n;
  181.  
  182.     // Try to obtain default value for Java home directory based on
  183.     // location of this executable.
  184.  
  185.     if ((n = GetModuleFileName(0, buf, MAX_PATH)) == 0) {
  186.     return -1;
  187.     }
  188.     bp = buf + n;
  189.     while (*--bp != '\\') ;
  190.     bp -= 4;
  191.     if (bp < buf || strnicmp(bp, "\\bin", 4) != 0) {
  192.     return -1;
  193.     }
  194.     *bp = '\0';
  195.     set->javaHome = strdup(buf);
  196.  
  197.     // Get default runtime library
  198.     set->runtimeLib = JRE_GetDefaultRuntimeLib(set->javaHome);
  199.  
  200.     // Get default class path
  201.     set->classPath = JRE_GetDefaultClassPath(set->javaHome);
  202.  
  203.     // Reset other fields since these are unknown
  204.     set->compiler = 0;
  205.     set->majorVersion = 0;
  206.     set->minorVersion = 0;
  207.     set->microVersion = 0;
  208.  
  209.     return 0;
  210. }
  211.  
  212. /*
  213.  * Return default runtime library for specified Java home directory.
  214.  */
  215. char *
  216. JRE_GetDefaultRuntimeLib(const char *dir)
  217. {
  218.     char *cp = JRE_Malloc(strlen(dir) + sizeof(RUNTIME_LIB) + 8);
  219.     sprintf(cp, "%s\\bin\\" RUNTIME_LIB, dir);
  220.     return cp;
  221. }
  222.  
  223. /*
  224.  * Return default class path for specified Java home directory.
  225.  */
  226. char *
  227. JRE_GetDefaultClassPath(const char *dir)
  228. {
  229.     char *cp = JRE_Malloc(strlen(dir) * 4 + 64);
  230.     sprintf(cp, "%s\\lib\\rt.jar;%s\\lib\\i18n.jar;%s\\lib\\classes.zip;"
  231.         "%s\\classes", dir, dir, dir, dir);
  232.     return cp;
  233. }
  234.  
  235. /*
  236.  * Loads the runtime library corresponding to 'libname' and returns
  237.  * an opaque handle to the library.
  238.  */
  239. void *
  240. JRE_LoadLibrary(const char *path)
  241. {
  242.     return (void *)LoadLibrary(path);
  243. }
  244.  
  245. /*
  246.  * Unloads the runtime library associated with handle.
  247.  */
  248. void
  249. JRE_UnloadLibrary(void *handle)
  250. {
  251.     FreeLibrary(handle);
  252. }
  253.  
  254. /*
  255.  * Loads default VM args for the specified runtime library handle.
  256.  */
  257. jint
  258. JRE_GetDefaultJavaVMInitArgs(void *handle, void *vmargs)
  259. {
  260.     FARPROC proc = GetProcAddress(handle, "JNI_GetDefaultJavaVMInitArgs");
  261.     return proc != 0 ? ((*proc)(vmargs), 0) : -1;
  262. }
  263.  
  264. /*
  265.  * Creates a Java VM for the specified runtime library handle.
  266.  */
  267. jint
  268. JRE_CreateJavaVM(void *handle, JavaVM **vmp, JNIEnv **envp, void *vmargs)
  269. {
  270.     FARPROC proc = GetProcAddress(handle, "JNI_CreateJavaVM");
  271.     return proc != 0 ? (*proc)(vmp, envp, vmargs) : -1;
  272. }
  273.  
  274. /*
  275.  * Entry point for JREW (Windows-only) version of the runtime loader.
  276.  * This entry point is called when the '-subsystem:windows' linker
  277.  * option is used, and will cause the resulting executable to run
  278.  * detached from the console.
  279.  */
  280. int WINAPI
  281. WinMain(HINSTANCE inst, HINSTANCE prevInst, LPSTR cmdLine, int cmdShow)
  282. {
  283.     __declspec(dllimport) char **__initenv;
  284.  
  285.     __initenv = _environ;
  286.     exit(main(__argc, __argv));
  287. }
  288.