Beispiel #1
0
int main() {
	/*
		TODO: using abs paths just to test this shit.
	*/

	// Set path where managed assemblies live.
	mono_set_assemblies_path("C:\\Users\\Niko\\Source\\Repos\\SaNi\\vs2013\\sanimono\\SaNi\\bin\\Debug");
	// Set Mono library and configuration paths.
	mono_set_dirs("C:\\Program Files (x86)\\Mono\\lib", "C:\\Program Files (x86)\\Mono\\etc");

	// Our application assembly that contains 
	// the code we are going to run.
	domain = mono_jit_init_version("SaNi", "v4.0.30319");
	
	// Our domain in which the code will be 
	// executed.
	MonoAssembly* assembly = mono_domain_assembly_open(domain, "C:\\Users\\Niko\\Source\\Repos\\SaNi\\vs2013\\sanimono\\SaNi\\bin\\Debug\\SaNi.exe");

	if (!assembly) throw std::runtime_error("could not load the given Mono assembly");

	mono_add_internal_call("SaNi.Utils::Print(string)", (void*)print);
	mono_add_internal_call("SaNi.Utils::GetString()", (void*)getString);

	const auto argc = 6;
	char* argv[argc] = { "HELLO", "WORLD", "FROM", "THE", "F*****G", "MONO" };

	mono_jit_exec(domain, assembly, argc, argv);

	// Clean mono.
	mono_jit_cleanup(domain);

	return 0;
}
Beispiel #2
0
void main(int argc, char* argv[])
{
	mono_set_dirs("lib", "");
	mono_config_parse(NULL);

	MonoDomain* domain = mono_jit_init("managed.exe");
	if(!domain)
	{
		printf("Unable to initialize mono jit.\n");
		return;
	}
	MonoAssembly* assembly = mono_domain_assembly_open(domain, "managed.exe");
	if (!assembly)
	{
		printf("Unable to open assembly.\n");
		return;
	}
	
	char* argvv[1] = { "managed" };
	mono_jit_exec(domain, assembly, 1, argvv);
	
	mono_jit_cleanup(domain);

	return;
}
Beispiel #3
0
int app_mono_run(struct sip_msg *msg, char *arg)
{
	int ret;
	char *argv[2];
	int argc;
	sr_mono_load_t *mi;

	if(_sr_mono_load_list == NULL)
		return -1;
	mi = _sr_mono_load_list;

	LM_DBG("running Mono assembly: [[%s]]\n", mi->script);
	_sr_M_env.msg = msg;

	_sr_M_env.domain = mi->domain;
	_sr_M_env.assembly = mi->assembly;
	if (_sr_M_env.assembly==NULL) {
		LM_DBG("empty assembly\n");
		memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
		return -1;
	}
	mono_domain_set(_sr_M_env.domain, 0);
	argc = 1;
	argv[0] = mi->script;
	if(arg!=NULL) {
		argc++;
		argv[1] = arg;
	}
	mono_jit_exec(_sr_M_env.domain, _sr_M_env.assembly, argc, argv);
	ret = mono_environment_exitcode_get();
	LM_DBG("returned code from mono environment: %d\n", ret);

	memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
	return (ret==0)?1:-1;
}
Beispiel #4
0
Error GDMonoAssembly::load(MonoDomain *p_domain) {

	ERR_FAIL_COND_V(loaded, ERR_FILE_ALREADY_IN_USE);

	uint64_t last_modified_time = FileAccess::get_modified_time(path);

	Vector<uint8_t> data = FileAccess::get_file_as_array(path);
	ERR_FAIL_COND_V(data.empty(), ERR_FILE_CANT_READ);

	String image_filename(path);

	MonoImageOpenStatus status;

	image = mono_image_open_from_data_with_name(
			(char *)&data[0], data.size(),
			true, &status, false,
			image_filename.utf8().get_data());

	ERR_FAIL_COND_V(status != MONO_IMAGE_OK || image == NULL, ERR_FILE_CANT_OPEN);

#ifdef DEBUG_ENABLED
	String pdb_path(path + ".pdb");

	if (!FileAccess::exists(pdb_path)) {
		pdb_path = path.get_basename() + ".pdb"; // without .dll

		if (!FileAccess::exists(pdb_path))
			goto no_pdb;
	}

	pdb_data.clear();
	pdb_data = FileAccess::get_file_as_array(pdb_path);
	mono_debug_open_image_from_memory(image, &pdb_data[0], pdb_data.size());

no_pdb:

#endif

	assembly = mono_assembly_load_from_full(image, image_filename.utf8().get_data(), &status, false);

	ERR_FAIL_COND_V(status != MONO_IMAGE_OK || assembly == NULL, ERR_FILE_CANT_OPEN);

	if (p_domain && mono_image_get_entry_point(image)) {
		// TODO should this be removed? do we want to call main? what other effects does this have?
		mono_jit_exec(p_domain, assembly, 0, NULL);
	}

	loaded = true;
	modified_time = last_modified_time;

	return OK;
}
Beispiel #5
0
static void main_function (MonoDomain *domain, const char *file, int argc, char** argv)
{
	MonoAssembly *assembly;

	assembly = mono_domain_assembly_open (domain, file);
	if (!assembly)
		exit (2);
	/*
	 * mono_jit_exec() will run the Main() method in the assembly.
	 * The return value needs to be looked up from
	 * System.Environment.ExitCode.
	 */
	mono_jit_exec (domain, assembly, argc, argv);
}
Beispiel #6
0
int app_mono_exec(struct sip_msg *msg, char *script, char *param)
{
	int ret;
	char *argv[2];
	int argc;

	argc = 1;
	argv[0] = script;
	if(param!=NULL) {
		argc++;
		argv[1] = param;
	}
	LM_DBG("executing Mono assembly: [[%s]]\n", argv[0]);
	_sr_M_env.msg = msg;

	mono_config_parse (NULL);
	/*
	 * mono_jit_init() creates a domain: each assembly is
	 * loaded and run in a MonoDomain.
	 */
	_sr_M_env.domain = mono_jit_init (argv[0]);
	/*
	 * We add our special internal functions, so that C# code
	 * can call us back.
	 */
	sr_mono_load_class_core();
	sr_mono_load_class_pv();
	sr_mono_load_class_hdr();

	_sr_M_env.assembly = mono_domain_assembly_open(_sr_M_env.domain, argv[0]);
	if (_sr_M_env.assembly==NULL) {
		ret = -1;
		goto done;
	}
	/*
	 * mono_jit_exec() will run the Main() method in the assembly.
	 * The return value needs to be looked up from
	 * System.Environment.ExitCode.
	 */
	mono_jit_exec(_sr_M_env.domain, _sr_M_env.assembly, argc, argv);
	ret = mono_environment_exitcode_get();
	LM_DBG("returned code from mono environment: %d\n", ret);

done:
	mono_jit_cleanup(_sr_M_env.domain);

	memset(&_sr_M_env, 0, sizeof(sr_mono_env_t));
	return (ret==0)?1:-1;
}
Beispiel #7
0
int main(int argc, char* argv[])
{
    MonoDomain *domain;
    MonoAssembly *assembly;
    const char *file;
    int retval;
    
    if (argc < 2)
    {
        fprintf(stderr, "Please provide name of mono assembly to load.\n");
        return 1;
    }
    
    file = argv[1];

    // Load the default Mono configuration file, this is needed
    // if you are planning on using the dllmaps defined on the
    // system configuration
    mono_config_parse(NULL);
    
    // Create mono AppDomain
    domain = mono_jit_init(file);

    // Open entry assembly in new AppDomain
    assembly = mono_domain_assembly_open(domain, file);

    if (assembly != NULL)
    {
        // mono_jit_exec runs the Main() method in the assembly
        mono_jit_exec(domain, assembly, argc - 1, argv + 1);

        // Get exit code from entry assembly
        retval = mono_environment_exitcode_get();
    }
    else
    {
        fprintf(stderr, "Failed to load mono assembly: %s\n", file);
        retval = 2;
    }
    
    // This clean up call tends to crash - since we are exiting anyway, we skip it...
    //mono_jit_cleanup(domain);

    return retval;
}
Beispiel #8
0
void mono_convert_symbols(const char * path) {
    std::string converter_path_string = PathUtil::GetLibDirectory()
        .append("mono/4.5/pdb2mdb.exe");

    char *converter_path = new char[converter_path_string.size() + 1];

    converter_path[converter_path_string.size()] = 0;
    memcpy(converter_path, converter_path_string.c_str(),
        converter_path_string.size());

    MonoAssembly *converter_assembly = mono_domain_assembly_open(
        mono_domain_get(), converter_path);

    assert(converter_assembly);

    char * argv[2];
    argv[0] = (char *)converter_path;
    argv[1] = (char *)path;
    mono_jit_exec(mono_domain_get(), converter_assembly, 2, argv);
}
Beispiel #9
0
/**
 * This program must be run like this: ./test MonoEmbedded.exe
 * (it requires one argument, even when it is not going to be used because I do not know what
 *  parameters to use in mono_jit_exec method, it crashes with NULL value :/)
 */
int main (int argc, char *argv[])
{
    /*
     * Load the default Mono configuration file, this is needed
     * if you are planning on using the dllmaps defined on the
     * system configuration
     */
    mono_config_parse (NULL);

    /*
     * mono_jit_init() creates a domain: each assembly is
     * loaded and run in a MonoDomain.
     */
    MonoDomain *domain = mono_jit_init ("../MonoEmbedded/bin/Debug/MonoEmbedded.exe");

    /*
     * Optionally, add an internal call that your startup.exe
     * code can call, this will bridge startup.exe to Mono
     */
    mono_add_internal_call ("MonoEmbedded.MainClass::Print", (void *)p_Print);

    /*
     * Open the executable, and run the Main method declared
     * in the executable
     */
    MonoAssembly *assembly = mono_domain_assembly_open (domain, "../MonoEmbedded/bin/Debug/MonoEmbedded.exe");
    if (!assembly) {
        exit (2);
    }

    /*
     * mono_jit_exec() will run the Main() method in the assembly.
     * The return value needs to be looked up from
     * System.Environment.ExitCode.
     */
    mono_jit_exec (domain, assembly, argc - 1, argv + 1);

    return 0;
}
Beispiel #10
0
__int32 WINAPI _CorExeMain(void)
{
    int exit_code;
    int argc;
    char **argv;
    MonoDomain *domain=NULL;
    MonoImage *image;
    MonoImageOpenStatus status;
    MonoAssembly *assembly=NULL;
    WCHAR filename[MAX_PATH];
    char *filenameA;
    ICLRRuntimeInfo *info;
    RuntimeHost *host;
    HRESULT hr;
    int i;

    get_utf8_args(&argc, &argv);

    GetModuleFileNameW(NULL, filename, MAX_PATH);

    TRACE("%s", debugstr_w(filename));
    for (i=0; i<argc; i++)
        TRACE(" %s", debugstr_a(argv[i]));
    TRACE("\n");

    filenameA = WtoA(filename);
    if (!filenameA)
        return -1;

    FixupVTable(GetModuleHandleW(NULL));

    hr = get_runtime_info(filename, NULL, NULL, 0, 0, FALSE, &info);

    if (SUCCEEDED(hr))
    {
        hr = ICLRRuntimeInfo_GetRuntimeHost(info, &host);

        if (SUCCEEDED(hr))
            hr = RuntimeHost_GetDefaultDomain(host, &domain);

        if (SUCCEEDED(hr))
        {
            image = mono_image_open_from_module_handle(GetModuleHandleW(NULL),
                filenameA, 1, &status);

            if (image)
                assembly = mono_assembly_load_from(image, filenameA, &status);

            if (assembly)
            {
                mono_trace_set_assembly(assembly);

                exit_code = mono_jit_exec(domain, assembly, argc, argv);
            }
            else
            {
                ERR("couldn't load %s, status=%d\n", debugstr_w(filename), status);
                exit_code = -1;
            }

            RuntimeHost_DeleteDomain(host, domain);
        }
        else
            exit_code = -1;

        ICLRRuntimeInfo_Release(info);
    }
    else
        exit_code = -1;

    HeapFree(GetProcessHeap(), 0, argv);

    if (domain)
    {
        mono_thread_manage();
        mono_jit_cleanup(domain);
    }

    return exit_code;
}