Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions mono/metadata/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -3454,41 +3454,29 @@ free_main_args (void)
}

/**
* mono_runtime_run_main:
* @method: the method to start the application with (usually Main)
* mono_set_commandline_arguments:
* @argc: number of arguments from the command line
* @argv: array of strings from the command line
* @exc: excetption results
* @basedir: optional base path of assembly with entrypoint
*
* Execute a standard Main() method (argc/argv contains the
* executable name). This method also sets the command line argument value
* needed by System.Environment.
* This method sets the command line argument value needed by System.Environment.
*
*
*/
int
mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
MonoObject **exc)

void
mono_set_commandline_arguments(int argc, char* argv[], const char* basedir)
{
int i;
MonoArray *args = NULL;
MonoDomain *domain = mono_domain_get ();
gchar *utf8_fullpath;
MonoMethodSignature *sig;

g_assert (method != NULL);

mono_thread_set_main (mono_thread_current ());

g_assert (main_args == NULL); //this function should only be called once.
main_args = g_new0 (char*, argc);
num_main_args = argc;

if (!g_path_is_absolute (argv [0])) {
if (!g_path_is_absolute (argv [0]) && basedir != NULL) {
gchar *basename = g_path_get_basename (argv [0]);
gchar *fullpath = g_build_filename (method->klass->image->assembly->basedir,
basename,
NULL);

gchar *fullpath = g_build_filename (basedir, basename, NULL);

utf8_fullpath = mono_utf8_from_external (fullpath);
if(utf8_fullpath == NULL) {
/* Printing the arg text will cause glib to
Expand All @@ -3500,7 +3488,7 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
exit (-1);
}

g_free (fullpath);
g_free (basename);
} else {
Expand All @@ -3511,31 +3499,62 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
exit (-1);
}
}

main_args [0] = utf8_fullpath;

for (i = 1; i < argc; ++i) {
gchar *utf8_arg;

utf8_arg=mono_utf8_from_external (argv[i]);
if(utf8_arg==NULL) {
/* Ditto the comment about Invalid UTF-8 here */
g_print ("\nCannot determine the text encoding for argument %d (%s).\n", i, argv[i]);
g_print ("Please add the correct encoding to MONO_EXTERNAL_ENCODINGS and try again.\n");
exit (-1);
}

main_args [i] = utf8_arg;
}
argc--;
argv++;
}

/**
* mono_runtime_run_main:
* @method: the method to start the application with (usually Main)
* @argc: number of arguments from the command line
* @argv: array of strings from the command line
* @exc: excetption results
*
* Execute a standard Main() method (argc/argv contains the
* executable name). This method also sets the command line argument value
* needed by System.Environment.
*
*
*/
int
mono_runtime_run_main (MonoMethod *method, int argc, char* argv[],
MonoObject **exc)
{
int i;
MonoArray *args = NULL;
MonoDomain *domain = mono_domain_get ();

MonoMethodSignature *sig;

g_assert (method != NULL);

mono_thread_set_main (mono_thread_current ());

sig = mono_method_signature (method);
if (!sig) {
g_print ("Unable to load Main method.\n");
exit (-1);
}

mono_set_commandline_arguments(argc, argv, method->klass->image->assembly->basedir);

argc--;
argv++;

if (sig->param_count) {
args = (MonoArray*)mono_array_new (domain, mono_defaults.string_class, argc);
for (i = 0; i < argc; ++i) {
Expand Down
3 changes: 3 additions & 0 deletions mono/metadata/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ mono_runtime_invoke_array (MonoMethod *method, void *obj, MonoArray *params,
void*
mono_method_get_unmanaged_thunk (MonoMethod *method);

void
mono_set_commandline_arguments (int argc, char* argv[], const char* basedir);

MonoArray*
mono_runtime_get_main_args (void);

Expand Down