-
Notifications
You must be signed in to change notification settings - Fork 0
/
Launcher.cpp
177 lines (150 loc) · 4.57 KB
/
Launcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
This is an application that can be used to launch Java
applications by just clicking on a exe file.
You can rename launcher.exe to anything you want.
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <shellapi.h>
void deletenewlines(char* buffer)
{
while(*buffer != '\0')
{
if (*buffer == '\n')
{
*buffer = '\0';
return;
}
++buffer;
}
}
void getExecutable(char *result, char* lpCmdLine)
{ //returns the name of the executable appended with .cfg
const char* constCommandLine = ::GetCommandLine();
char* commandLine = new char[strlen(constCommandLine)+1];
strcpy(commandLine,constCommandLine);
// Remove lpCmdLine from back
int lastpos = strlen(commandLine)-strlen(lpCmdLine);
commandLine[lastpos] = '\0';
lastpos--;
while(commandLine[lastpos]=='\"' || commandLine[lastpos]==' ')
{
commandLine[lastpos]='\0';
lastpos--;
}
int firstpos = lastpos;
while(commandLine[firstpos] > 0 && commandLine[firstpos]!='\\'
&& commandLine[firstpos]!=':' && commandLine[firstpos]!='/')
firstpos--;
strcpy(&result[0], &commandLine[firstpos+1]);
int resultlength = strlen(result);
bool containsdot = false;
for (int i=0; i<resultlength; i++)
if (result[i]=='.')
containsdot = true;
if (containsdot)
while(result[resultlength]!='.')
resultlength--;
strcpy(&result[resultlength],".cfg");
resultlength = strlen(result);
result[resultlength+1] = '\0';
}
void getExecutablePath(char *result, char* lpCmdLine)
{ // returns the path to the executable
const char* constCommandLine = ::GetCommandLine();
char* commandLine = new char[strlen(constCommandLine)+1];
strcpy(commandLine,constCommandLine);
// Remove lpCmdLine from back
int lastpos = strlen(commandLine)-strlen(lpCmdLine);
commandLine[lastpos] = '\0';
lastpos--;
while(commandLine[lastpos]!='\\')
{
commandLine[lastpos]='\0';
lastpos--;
}
int firstpos = 0;
while(commandLine[firstpos]=='\"' || commandLine[firstpos]==' ')
firstpos++;
strcpy(&result[0], &commandLine[firstpos]);
int resultlength = strlen(result);
resultlength = strlen(result);
result[resultlength+1] = '\0';
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
FILE* file = NULL;
// Look for config file with same name as executable but with cfg suffix.
char executable[1000];
getExecutable(executable,lpCmdLine);
char executablePath[1000];
getExecutablePath(executablePath,lpCmdLine);
char executablePathWithName[1000];
file = fopen(executable,"rt"); // Check for the executable.cfg -file in the current working directory
char cfgFileName[1000];
if (file)
strcpy(cfgFileName,executable);
if (!file)
{ // Check for the cfg executable.file in the directory of the application
sprintf(executablePathWithName,"%s%s",executablePath,executable);
file = fopen(executablePathWithName,"rt");
strcpy(cfgFileName, executablePath);
_chdir(executablePath);
}
// Open default config file
if (!file)
{
file = fopen("ImageJ.cfg","rt");
strcpy(cfgFileName, "ImageJ.cfg");
}
if (file==NULL)
{
char temp[1000];
sprintf(temp,"Could not find file %s or ImageJ.cfg. Please\n"
"reinstall the application.", executable);
MessageBox(NULL, temp,"Error loading configuration file", MB_OK);
return 1;
}
char buffer[10000];
// Read new directory from config file. We will change to this dir
// before starting execution.
fgets(buffer,10000,file);
deletenewlines(buffer);
int result = _chdir(buffer);
if (result!=0)
{
char temp[1000];
sprintf(temp, "Could not find the directory given in %s\n" \
"Please reinstall the application.", cfgFileName);
MessageBox(NULL, temp, "Error changing directory",MB_OK);
return 2;
}
// Read the name of the executable. Is usually java.exe or javaw.exe
char exe[1000];
fgets(exe,1000,file);
deletenewlines(exe);
// Read any extra parameters you want to give to the program.
// such as -jar parameters.
char parameter[1000];
fgets(parameter,1000,file);
deletenewlines(parameter);
// Add parameters given to the exe
strcat(parameter," ");
strcat(parameter, lpCmdLine);
char thecwd[1000];
_getcwd(thecwd,1000);
if (ShellExecute(NULL,NULL,exe,parameter,thecwd,SW_SHOWNORMAL)<=(HINSTANCE)32)
{
MessageBox(NULL,
"Could not start the application",
"Please reinstall the application",MB_OK);
return 2;
}
return 0;
}