@@ -117,9 +117,15 @@ internal static int Send(string command)
117
117
throw new Exception ( "Can't open this file" ) ;
118
118
}
119
119
}
120
+ catch ( TargetInvocationException tie )
121
+ {
122
+ Console . WriteLine ( tie . InnerException ) ;
123
+ }
120
124
catch ( Exception e )
121
125
{
122
126
Console . WriteLine ( "Execution failed: " + e . Message ) ;
127
+ if ( e . InnerException != null )
128
+ Console . WriteLine ( e . InnerException . Message ) ;
123
129
}
124
130
125
131
return 1 ;
@@ -128,23 +134,44 @@ internal static int Send(string command)
128
134
private static int ExecuteAssembly ( Assembly assembly , string [ ] args )
129
135
{
130
136
var mains = GetMainsWithHelpAttribute ( assembly ) ;
137
+ var mos = assembly . GetLoadedModules ( ) ;
138
+
139
+ // No main
131
140
if ( mains . Count ( ) < 1 )
132
141
throw new Exception ( "We can't execute this" ) ;
142
+
143
+ // Multi mains
133
144
if ( mains . Count ( ) > 1 )
134
145
Console . WriteLine ( "Warning: more than one entry point exists" ) ;
135
- var main = mains . FirstOrDefault ( ) ;
146
+
147
+ // Get the main
148
+ var main = mains . First ( ) ;
136
149
object programReturn ;
137
150
151
+ // Instanciate a class if needed
152
+ object instance = null ;
153
+
154
+ if ( ! main . IsStatic )
155
+ instance = Activator . CreateInstance ( main . DeclaringType ) ;
156
+
157
+ // Start the app
138
158
var parameters = main . GetParameters ( ) ;
139
- if ( parameters . Length == 1 && parameters [ 0 ] . ParameterType == typeof ( string [ ] ) )
140
- {
141
- programReturn = main . Invoke ( null , new object [ ] { args } ) ;
142
- }
143
- else if ( parameters . Length == 0 )
144
- programReturn = main . Invoke ( null , null ) ;
145
- else
159
+
160
+ if ( parameters . Length == 4 && // main(string[] arguments, Guid instance, Guid previousInstance, int windowState)
161
+ parameters [ 0 ] . ParameterType == typeof ( string [ ] ) &&
162
+ parameters [ 1 ] . ParameterType == typeof ( Guid ) &&
163
+ parameters [ 2 ] . ParameterType == typeof ( Guid ) &&
164
+ parameters [ 3 ] . ParameterType == typeof ( int ) )
165
+ programReturn = main . Invoke ( instance , new object [ ] { args , Guid . NewGuid ( ) , Guid . Empty , 0 } ) ; // Invoke the main with the given arguments and window info
166
+ else if ( parameters . Length == 1 && // main(string[] arguments)
167
+ parameters [ 0 ] . ParameterType == typeof ( string [ ] ) )
168
+ programReturn = main . Invoke ( instance , new object [ ] { args } ) ; // Invoke the main with the given arguments
169
+ else if ( parameters . Length == 0 ) // main()
170
+ programReturn = main . Invoke ( instance , null ) ; // Invoke the main without arguments
171
+ else // Bad main, ex: main(int index, object value)
146
172
throw new Exception ( "Bad Entry Point" ) ;
147
173
174
+ // Get exit code
148
175
if ( programReturn is int exitCode )
149
176
return exitCode ;
150
177
else if ( programReturn is Task < int > exitCodeAsync )
@@ -198,18 +225,28 @@ private static IEnumerable<MethodInfo> GetMainsWithHelpAttribute(Assembly assemb
198
225
// https://docs.microsoft.com/dotnet/csharp/fundamentals/program-structure/main-command-line#overview
199
226
private static bool CheckValidMainMethode ( MethodInfo method )
200
227
{
201
- // Must be static
228
+ /*/ / Must be static
202
229
if (!method.IsStatic)
203
- return false ;
230
+ return false;*/
204
231
205
232
// Must have the name Main
206
233
if ( method . Name . ToLower ( ) != "main" )
207
234
return false ;
208
235
209
236
// Must have 1 string parameter or not
210
237
var parameters = method . GetParameters ( ) ;
211
- if ( parameters . Length == 1 && parameters [ 0 ] . ParameterType != typeof ( string [ ] ) ||
212
- parameters . Length > 1 )
238
+
239
+ if ( // main(string[] arguments, Guid instance, Guid previousInstance, int windowState)
240
+ parameters . Length == 4 &&
241
+ parameters [ 0 ] . ParameterType != typeof ( string [ ] ) &&
242
+ parameters [ 1 ] . ParameterType != typeof ( Guid ) &&
243
+ parameters [ 2 ] . ParameterType != typeof ( Guid ) &&
244
+ parameters [ 3 ] . ParameterType != typeof ( int ) ||
245
+ // main(string[] arguments)
246
+ parameters . Length == 1 &&
247
+ parameters [ 0 ] . ParameterType != typeof ( string [ ] ) ||
248
+ // main()
249
+ parameters . Length > 1 )
213
250
return false ;
214
251
215
252
// Must return a integer or not
@@ -220,6 +257,34 @@ private static bool CheckValidMainMethode(MethodInfo method)
220
257
return true ;
221
258
}
222
259
260
+ /*private static bool CheckValidApiMethode(MethodInfo method)
261
+ {
262
+ // Must be static
263
+ if (!method.IsStatic)
264
+ return false;
265
+
266
+ // Must have the name vOS_Init
267
+ if (method.Name != "vOS_Main")
268
+ return false;
269
+
270
+ // Must have 1 string parameter or not
271
+ var parameters = method.GetParameters();
272
+
273
+ // vOS_Main(Guid instance, Guid previousInstance, string commandLineArguments, int windowState)
274
+ if (parameters.Length != 4 ||
275
+ parameters[0].ParameterType != typeof(Guid) ||
276
+ parameters[1].ParameterType != typeof(Guid) ||
277
+ parameters[2].ParameterType != typeof(string) ||
278
+ parameters[3].ParameterType != typeof(int))
279
+ return false;
280
+
281
+ // Must return a integer
282
+ if (method.ReturnType != typeof(int))
283
+ return false;
284
+
285
+ return true;
286
+ }*/
287
+
223
288
// http://www.blackbeltcoder.com/Articles/strings/a-c-command-line-parser
224
289
// https://stackoverflow.com/questions/24047674/how-to-reset-position-in-stringreader-to-begining-of-string
225
290
private static string [ ] CommandLineToArgs ( string commandLine )
0 commit comments