dll loading problems with .net assemblies when patcher is loaded through mfx ...

Charly Beck's icon

- i load and instruct 2 dot-net assemblies in my max external.
- when the patcher is loaded as patcher file it works, if the patcher is loaded via mfx it doesnt work.
- i found the .net runtime loads the dll module although it is allready loaded. then i get type conflicts.
- it loads the first time when i load it using LoadLibrary function.
- it loads a second time when the code invoked via exported dll-functions creates a .net object.
- for some reason it loads a third instance of the dll module in case mfx is loaded.

- i wonder if - due to the way max loads the mfx - the loadlibrary function works differently.
for example LoadLibraryEx with DONT_RESOLVE_DLL_REFERENCES doesnt call dllmain,
this may be the reson why .net can not initialize the environment and then is forced to load the dll a third time.

- Actually the dll is loaded 3 times in the scenario it doesn't work.
- In the scenario where it works it is loaded 2 times.

It works like:

Max loads External
External loads DotNetDll1
External calls DotNetDll1
DotNetDll1 loads DotNetDll2
DotNetDll2 loads second instance of DotNetDll1

The inheritance is like:
Type A in DotNetDll1 inherits from Type B in DotNetDll2.

So Type B needs to be casted from DotNetDll2.ModuleInstance1 to DotNetDll2.ModuleInstance2.

And this cast does not work in the mfx case. I get an error:
System.InvalidCastException: Objekt of Type "A" can not be convertet to type "B".
(although B is the baseclass of A, no bad cast.)

And i found out it works if i load the patcher before i load the mfx.
Then the dll is loaded "correctly" and .net doesn't load a third instance of the module.

Charly Beck's icon

this is why i wonder if the api to run max embedded in a host process as a dll module is public. Then i could load my dlls before loading max and it should work... (But this is still no solution for ableton live scenario)