Hi, dear community!
I have strange problem with loading assembly from file and unfortunately I can't reproduce it with simple easy-to-share code. I describe what I'm doing right below.
I have 4 applications:
"Complex Invoker" - the foreign(not mine) open source application, which is the computer game actually, for which I'm writing a plugin. This application could invoke several function from dll(see Proxy definition below). To be simple it calls 3 functions init(); release(); DoSomething(); actual names are a little bit different but it doesn't matter. Complex invoker is written in pure unmanaged c\c++ and compiled with MSVC.
"Simple Invoker" - the application I wrote from scratch to test if the Problem (see below) occurs in any way. It does the same - calls 3 functions as in Complex Invoker. Simple invoker is written in pure unmanaged c\c++ and compiled with MSVC.
"Proxy" - the dll which is being called by both of Invokers. It exports the functions init(); release(); DoSomething(); for calling them by Invokers. From the other part here is a managed (CLR) part which is called by init() function. The actual managed class is used to call Assembly.Load(Byte[],Byte[]); This function call loads an Assembly from file (see below) to instantiate class from that assembly. The class from assembly implements the interface "SomeInterface" which is also defined in "Proxy" ("Assembly" has a reference to "Proxy"). Proxy is written in mixed mode(managed+unmanaged) C++ in MSVC with /clr flag.
"Assembly" is dll(managed assembly) with single class which implements "SomeInterface" from Proxy. It is very simple and written with c#.
Now here are my goals. I want the Invoker (complex one specifically) to call proxy which in turn loads Assembly and invoke functions within the class (in Assembly) instance. The key requirement is to be able to "reload" Assembly on demand without reexecuting Invoker. The Invoker has the mechanism to signal the need to reload to Proxy, which in turn do Assembly.Load(Byte[],Byte[]) for Assembly.dll.
So now is the Problem. It works very well with "Simple Invoker" and fails to work with "Complex Invoker"! With Simple Invoker I was able to "reload" (actually load number of Assemblies) the Assembly more then 50 times and with "Complex Invoker" the Assembly.Load() method fails on the first attempt to reload an assembly, i.e. the Proxy loads the Assembly at first time and fails to reload it on demand. It is interesting that Simple and Complex loader do EXACTLY the same function call flow, i.e. LoadLibraryA("Pathto\Proxy.dll"); GetProcAddress for init, release, handleEvent; calling these functions; and after that FreeLibrary(). And I see the problem with inter-operation(dunno what kind) between exactly the Complex Invoker and .NET libs. Complex Invoker has something in it's code what breaks MS .NET correctness. I'm 99% sure it's not my fault as a coder.
Just before I go into deeper details about Exceptions I got and approaches I took to try to overcome such problems (like using AppDomains), I want to clarify If someone on this forum has the ability, time and will to go deep into the System.Load() internals (i.e. this should be the one with System and System.Reflection sources). This would require the installation of "Complex Invoker" and both Proxy and Assembly (don't think it is too tough task).
Thanks in advance!
VBR, Ivan