I want to load an assembly from a memory buffer. The requirement for me is that all assemblies including mscorlib.dll should be loaded from memory buffers, without using FS IO.
I was able to load an assembly and invoke a static method with this code:
MonoAssembly *assembly;
MonoImageOpenStatus status;
MonoImage * image0 = mono_image_open_from_data((char*)rawData, 3072, 0, &status);
const char * name = mono_image_get_name(image0);
mono_image_init(image0);
assembly = mono_assembly_load_from(image0, "main.dll", &status);
MonoImage * image1 = mono_assembly_get_image(assembly);
char* cbuf = (char*) malloc(200);
sprintf(cbuf, "Image name: %s", name);
MessageBoxA(NULL, cbuf, "INFO", MB_OK);
MonoClass* class1;
class1 = mono_class_from_name(image1, "", "App");
MonoMethod * method;
method = mono_class_get_method_from_name(class1, "Init", 0);
MonoObject * obj = mono_runtime_invoke(method, NULL, NULL, NULL);
MonoString * str = mono_object_to_string(obj, NULL);
wchar_t * strw = mono_string_to_utf16(str);
wprintf(strw);
But it requires me to set MONO_PATH. This works when the only dependency of the assembly is mscorlib.dll. When I try to use any other DLL (System.Json, for example) Mono fails to load my assembly.
My target goal is to remove all file dependencies and load everything from memory buffers (including mscorlib.dll, System.Json.dll, etc.). I am using custom PAK files that are going to store everything including Mono config and assemblies (our own as well as System.*.dll). I need to find a way to make Mono load all of my assemblies properly without any direct filesystem access (everything must be loaded from in-memory buffers).
Are there any ways I can reach my target goal:
MONO_PATH or a directory from mono_set_dirs)Thank you in advance!
PS: How can I manually load an assembly (System.Json) and make it visible to the assemblies that are loaded after System.Json?
Check out mono_register_bundled_assemblies () in assembly.h.
Thank you very much for your quick response!
Check out mono_register_bundled_assemblies () in assembly.h.
So I just create MonoBundledAssembly and register it before or after mono_jit_init?
Also, how do I load initial mscorlib.dll from a buffer? If I register it with mono_register_bundled_assemblies is it going to be available when Mono JIT engine starts up (mono_jit_init does not work without mscorlib.dll)?
It should be called before mono_jit_init (). The runtime will load assemblies from that list instead of from the file system.
Thank you very much for your short and clear answers!