Panda3D

p3dWrapper.c

00001 /* Filename: p3dWrapper.c
00002  * Created by:  rdb (16Jan10)
00003  *
00004  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
00005  *
00006  * PANDA 3D SOFTWARE
00007  * Copyright (c) Carnegie Mellon University.  All rights reserved.
00008  *
00009  * All use of this software is subject to the terms of the revised BSD
00010  * license.  You should have received a copy of this license along
00011  * with this source code in a file named "LICENSE."
00012  *
00013  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00014 
00015 /* p3dWrapper is a small wrapper executable that locates a .p3d file
00016    in the same directory as this executable file, with the same name
00017    (except .p3d instead of .exe of course). It is only meant to be
00018    used on Windows system, it is not needed on Unix-like systems. */
00019 
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <stdio.h>
00023 #include <windows.h>
00024 #include <process.h>
00025 #include <assert.h>
00026 
00027 #define BUFFER_SIZE 1024
00028 
00029 int main (int argc, char* argv[]) {
00030   int i;
00031   char buffer [BUFFER_SIZE];
00032   char* p3dfile;
00033   char* runtime = NULL;
00034   DWORD size;
00035   STARTUPINFO si;
00036   PROCESS_INFORMATION pi;
00037   char *cmd;
00038   char *newcmd;
00039   HKEY hKey = 0;
00040   char buf [1024] = {0};
00041   DWORD dwType = 0;
00042   DWORD dwBufSize = sizeof(buf);
00043   size = GetModuleFileName (NULL, buffer, BUFFER_SIZE);
00044   assert (size > 0);
00045 
00046   /* Chop off the .exe and replace it by .p3d. */
00047   p3dfile = (char*) malloc (size + 1);
00048   memcpy (p3dfile, buffer, size);
00049   p3dfile [size] = 0;
00050   memcpy (p3dfile + size - 3, "p3d", 3);
00051 
00052   /* Find the Panda3D applet\DefaultIcon key and extract the path to the runtime from there. */
00053   if (RegOpenKey (HKEY_CLASSES_ROOT, "Panda3D applet\\DefaultIcon", &hKey) == ERROR_SUCCESS) {
00054     dwType = REG_SZ;
00055     if (RegQueryValueEx(hKey, 0, 0, &dwType, (BYTE*) buf, &dwBufSize) == ERROR_SUCCESS) {
00056       for (i = dwBufSize - 1; i >= 0; --i) {
00057         if (buf [i] == '/' || buf [i] == '\\') {
00058           runtime = (char*) malloc (i + 13);
00059           memcpy (runtime, buf, i);
00060           runtime [i] = 0;         
00061           strcat (runtime, "\\panda3d.exe");      
00062           break;
00063         }
00064       }
00065     } else {
00066       fprintf (stderr, "Failed to read registry key. Try reinstalling the Panda3D Runtime.\n");
00067       return 1;
00068     }
00069     RegCloseKey(hKey);
00070   } else {
00071     fprintf (stderr, "The Panda3D Runtime does not appear to be installed!\n");
00072     return 1;
00073   }
00074 
00075   if (runtime == NULL) {
00076     fprintf (stderr, "Failed to find panda3d.exe in registry. Try reinstalling the Panda3D Runtime.\n");
00077     return 1;
00078   }
00079 
00080   /* Build the command-line and run panda3d.exe. */
00081   cmd = GetCommandLine();
00082   newcmd = (char*) malloc (strlen(runtime) + strlen(p3dfile) + strlen (cmd) - strlen (argv[0]) + 7);
00083   sprintf (newcmd, "\"%s\" \"%s\" %s", runtime, p3dfile, cmd + strlen (argv[0]));
00084   memset(&si, 0, sizeof(si));
00085   si.cb = sizeof(STARTUPINFO);
00086   if (CreateProcess(runtime, newcmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
00087     WaitForSingleObject(pi.hProcess, INFINITE);
00088   }
00089   free (newcmd);
00090   return 0;
00091 }
00092 
 All Classes Functions Variables Enumerations