zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Launcher.c
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <windows.h>
21 
22 // See http://msdn.microsoft.com/en-us/library/ms682425.aspx
23 #define BUFFER_SIZE 32768
24 int launch(const char local_exe[], const char arguments[]);
25 
26 int main(int argc, char **argv) {
27  const char executable_name[] = EXECUTABLE_NAME;
28  char lpCmdLine[BUFFER_SIZE] = "";
29  int i;
30  char *d, *dend, *s;
31 
32  d = lpCmdLine;
33  dend = lpCmdLine + BUFFER_SIZE;
34  for(i = 1; i < argc; ++i) {
35  for(s = argv[i]; *s; ++s) {
36  if(d == dend)
37  return -1;
38 
39  *(d++) = *s;
40  }
41 
42  if(d == dend)
43  return -1;
44 
45  *(d++) = i + 1 == argc ? '\0' : ' ';
46  }
47 
48  return launch(executable_name, lpCmdLine);
49 }
50 
51 int launch(const char local_exe[], const char arguments[]) {
52  char dir[BUFFER_SIZE];
53  const DWORD nSize = GetModuleFileNameA(NULL, dir, BUFFER_SIZE - 1);
54  int i;
55  char full_exe[BUFFER_SIZE];
56  char full_exe_with_args[BUFFER_SIZE];
57  STARTUPINFOA siStartupInfo;
58  PROCESS_INFORMATION piProcessInfo;
59 
60  if(!nSize || GetLastError() == ERROR_INSUFFICIENT_BUFFER)
61  return -2;
62  else
63  dir[nSize] = '\0';
64 
65  for(i = (int)nSize - 1; i != -1; --i)
66  if(dir[i] == '\\') {
67  if(i + 9 > BUFFER_SIZE)
68  return -3;
69 #ifdef X64
70 #ifdef NDEBUG
71  memcpy(dir + i, "\\bin\\x64", 9);
72 #else
73  memcpy(dir + i, "\\bin\\d64", 9);
74 #endif
75 #else
76 #ifdef NDEBUG
77  memcpy(dir + i, "\\bin\\x32", 9);
78 #else
79  memcpy(dir + i, "\\bin\\d32", 9);
80 #endif
81 #endif
82  break;
83  }
84  else
85  dir[i] = '\0';
86 
87  if(strlen(dir) + strlen(local_exe) + 1 > BUFFER_SIZE)
88  return -4;
89  sprintf_s(full_exe, BUFFER_SIZE, "%s\\%s", dir, local_exe);
90 
91  if(strlen(full_exe) + strlen(arguments) + 1 > BUFFER_SIZE)
92  return -5;
93  sprintf_s(full_exe_with_args, BUFFER_SIZE, "%s %s", full_exe, strlen(arguments));
94 
95  memset(&siStartupInfo, 0, sizeof(siStartupInfo));
96  memset(&piProcessInfo, 0, sizeof(piProcessInfo));
97  siStartupInfo.cb = sizeof(siStartupInfo);
98 
99  if(!SetCurrentDirectoryA(dir)) {
100 #ifdef X64
101 #ifdef NDEBUG
102  MessageBoxA(0, "Failed to set the current working directory to bin\\x64\\", 0, MB_OK);
103 #else
104  MessageBoxA(0, "Failed to set the current working directory to bin\\d64\\", 0, MB_OK);
105 #endif
106 #else
107 #ifdef NDEBUG
108  MessageBoxA(0, "Failed to set the current working directory to bin\\x32\\", 0, MB_OK);
109 #else
110  MessageBoxA(0, "Failed to set the current working directory to bin\\d32\\", 0, MB_OK);
111 #endif
112 #endif
113 
114  return 1;
115  }
116 
117  if(!CreateProcessA(full_exe,
118  full_exe_with_args,
119  NULL,
120  NULL,
121  FALSE,
122  0,
123  NULL,
124  NULL,
125  &siStartupInfo,
126  &piProcessInfo))
127  {
128  MessageBoxA(0, "Failed to launch \"" EXECUTABLE_NAME "\"", 0, MB_OK);
129 
130  return 2;
131  }
132 
133  if(WaitForSingleObject(piProcessInfo.hProcess, INFINITE) == WAIT_FAILED)
134  return (int)GetLastError();
135 
136  CloseHandle(piProcessInfo.hThread);
137  CloseHandle(piProcessInfo.hProcess);
138 
139  return 0;
140 }
GLdouble s
Definition: glew.h:1376
int main(int argc, char **argv)
Definition: bootstrap.cpp:102
#define NULL
Definition: ftobjs.h:61
#define memset
Definition: SDL_malloc.c:633
return Display return Display Bool Bool int d
Definition: SDL_x11sym.h:30
int launch(const char local_exe[], const char arguments[])
Definition: Launcher.c:51
#define BUFFER_SIZE
Definition: Launcher.c:23
#define FALSE
Definition: ftobjs.h:57
#define memcpy
Definition: SDL_malloc.c:634
typedef DWORD(WINAPI *XInputGetState_t)(DWORD dwUserIndex
int i
Definition: pngrutil.c:1377