Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

win32vmdstart.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *cr
00003  *cr            (C) Copyright 1995-2008 The Board of Trustees of the
00004  *cr                        University of Illinois
00005  *cr                         All Rights Reserved
00006  *cr
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  * RCS INFORMATION:
00011  *
00012  *      $RCSfile: win32vmdstart.c,v $
00013  *      $Author: johns $        $Locker:  $             $State: Exp $
00014  *      $Revision: 1.37 $      $Date: 2008/03/27 19:36:52 $
00015  *
00016  ***************************************************************************
00017  * DESCRIPTION:
00018  *   Windows startup code
00019  ***************************************************************************/
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <windows.h>
00024 
00025 #define VMD_FILENAME_MAX 1024
00026 
00027 static char * vmd_get_vmddir(void) {
00028   LONG res;
00029   HKEY k;
00030   BYTE * vmddir;
00031   DWORD bufsz;
00032   DWORD buftype;
00033 
00034   res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
00035                      "Software\\University of Illinois\\VMD\\1.8.7",
00036                      0,
00037                      KEY_READ,
00038                      &k);
00039 
00040 /* XXX WIN64 registry hacks */
00041 #if defined(_WIN64)
00042   if (res != ERROR_SUCCESS) {
00043     printf("Failed to find registry key in 64-bit registry.\n");
00044     printf("Falling back in desparation for 32-bit registry key...\n");
00045     res = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
00046                        "Software\\WOW6432node\\University of Illinois\\VMD\\1.8.7",
00047                        0,
00048                        KEY_READ,
00049                        &k);
00050   }
00051 
00052   if (res != ERROR_SUCCESS)  {
00053     printf("Complete and total failure finding any usable registry key.\n");
00054     printf("Guessing VMD location from default 32-bit install directory\n");
00055     vmddir = (BYTE *) malloc(2048);
00056     strcpy((char *) vmddir, "c:\\Program Files (x86)\\University of Illinois\\VMD"); 
00057   }     
00058 #endif
00059 
00060   if (res != ERROR_SUCCESS)  {
00061     printf("Failed to find registry key.\n");
00062     return NULL;
00063   }
00064 
00065   bufsz = 2048;
00066   vmddir = (BYTE *) malloc(2048);
00067 
00068   res = RegQueryValueEx(k, "VMDDIR", NULL, &buftype, vmddir, &bufsz);
00069 
00070   if (res != ERROR_SUCCESS) {
00071     printf("Failed to get VMDDIR registry key value.\n");
00072     free(vmddir);
00073     vmddir = NULL;
00074   }
00075 
00076   if (buftype != REG_SZ) {
00077     printf("Bad data type in VMDDIR registry key value\n");
00078     free(vmddir);
00079     vmddir = NULL;
00080   }
00081 
00082   return (char *) vmddir;
00083 }
00084 
00085 #if defined(VMDSEPARATESTARTUP)
00086 // used when seperate startup (for shell) is needed
00087 int main(int argc, char **argv) {
00088 #else
00089 // called by VMD main program
00090 int win32vmdstart(void) {
00091 #endif
00092 
00093   int i, j, len;
00094   char *str, *str2, *vmddir;
00095   char tmp[MAX_PATH + 8192];
00096   char buffer[MAX_PATH +1];
00097   int usedregistry;
00098 #if defined(VMDSEPARATESTARTUP)
00099   int rc;
00100 #endif
00101 
00102 /* XXX debugging hacks to be sure the compiler mode is set correctly */
00103 #ifdef _WIN64
00104   printf("TEST: Windows x64 VMD test build.\n");
00105   printf("TEST: sizeof(int): %d  sizeof(long): %d  sizeof(void *): %d\n", 
00106     sizeof(int), sizeof(long), sizeof(void *));
00107 #endif 
00108 
00109   vmddir = NULL;             // initialize to NULL for safety
00110   usedregistry = 1;          // change this to 0 if we fail
00111   vmddir = vmd_get_vmddir(); // search Windows registry first 
00112 
00113   if (vmddir == NULL) {
00114 #if 1
00115     // get full pathname to VMD executable
00116     GetModuleFileName(NULL, buffer, sizeof(buffer));
00117     usedregistry = 0;  // registry failed, we had to guess, using exe path
00118     vmddir = buffer;   // get from the Win32 API vmd.exe path
00119 #elif defined(VMDSEPARATESTARTUP)
00120     // desparate attempt to get VMDDIR from argv[0], which only works if 
00121     // we were started from the explorer shell
00122     usedregistry = 0;  // registry failed, we had to guess, using exe path
00123     vmddir = argv[0];  // get from the explorer shell vmd.exe path
00124 #else
00125     return -1; // fail and exit
00126 #endif
00127   }
00128 
00129   len = strlen(vmddir);
00130   str = (char *) malloc(len);
00131   str2 = (char *) malloc(len);
00132   strcpy(str,  vmddir);
00133   strcpy(str2, vmddir);
00134 
00135   j=len;
00136   for (i=0; i<len; i++) {
00137     if (str[i] == '\\') {
00138       str2[i] = '/';
00139       j=i;
00140     }
00141   }
00142 
00143   if (usedregistry) {
00144     free(vmddir);  // free memory allocated by the vmd_get_vmddir() function
00145     vmddir = NULL; // make sure we don't accidentally use it again
00146   } else {  
00147     // If we didn't use the registry, we need to strip off everything after
00148     // and including the last "\" character. 
00149     // If we did use the registry, no string truncation is required.
00150     str[j] = '\0';
00151     str2[j] = '\0';
00152   }
00153 
00154   strcpy(tmp, "VMDDIR=");
00155   strcat(tmp, str2);
00156   putenv(tmp);
00157 
00158   strcpy(tmp, "TCL_LIBRARY=");
00159   strcat(tmp, str2);
00160   strcat(tmp, "/scripts/tcl");
00161   putenv(tmp);
00162 
00163   strcpy(tmp, "TK_LIBRARY=");
00164   strcat(tmp, str2);
00165   strcat(tmp, "/scripts/tk");
00166   putenv(tmp);
00167 
00168   if (!getenv("PYTHONPATH")) {
00169     strcpy(tmp, "PYTHONPATH=");
00170     strcat(tmp, str2);
00171     strcat(tmp, "/scripts/python");
00172     putenv(tmp);
00173   } else {
00174     strcpy(tmp, getenv("PYTHONPATH"));
00175     strcat(tmp, ":");
00176     strcat(tmp, str2);
00177     strcat(tmp, "/scripts/python");
00178     putenv(tmp);
00179   }
00180 
00181   strcpy(tmp, "VMDBABELBIN=");
00182   strcat(tmp, str);
00183   strcat(tmp, "\\babel\\babel.exe");
00184   putenv(tmp);
00185 
00186   strcpy(tmp, "BABEL_DIR=");
00187   strcat(tmp, str);
00188   strcat(tmp, "\\babel");
00189   putenv(tmp);
00190 
00191   strcpy(tmp, "STRIDE_BIN=\"");
00192   strcat(tmp, str);
00193   strcat(tmp, "\\stride_WIN32.exe\"");
00194   putenv(tmp);
00195 
00196   strcpy(tmp, "SURF_BIN=");
00197   strcat(tmp, str);
00198   strcat(tmp, "\\surf_WIN32.exe");
00199   putenv(tmp);
00200 
00201   strcpy(tmp, "TACHYON_BIN=");
00202   strcat(tmp, str);
00203   strcat(tmp, "\\tachyon_WIN32.exe");
00204   putenv(tmp);
00205 
00206 #if defined(VMDSEPARATESTARTUP)
00207   strcpy(tmp, str);
00208   strcat(tmp, "\\winvmd.exe");
00209 
00210   // Add all of the command line options we may have had to the 
00211   // invocation of the real program.
00212   strcat(tmp, " ");
00213   for(i=1; i<argc; i++) {
00214     strcat(tmp, argv[i]);
00215     strcat(tmp, " ");
00216   }
00217 
00218   // Run the program!
00219   rc = WinExec(tmp, SW_SHOW);
00220   if (!rc) {
00221     printf("failed to start program %s\n", tmp);
00222     Sleep(10000);  
00223   }
00224 #endif
00225 
00226   return 0;
00227 }
00228 
00229 
00230 
00231 

Generated on Sun Sep 7 01:26:21 2008 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002