When exactly a C# assembly gets loaded by managed C++

C

Chen Zhuo

Hi all experts,


We are having a problem with the exact time when a C# dll gets loaded in
managed C++. The scenario is like:


In managed.cpp:
#using MyCSharp.dll

class Test
{
static void func()
{
if (false)
{
call into MyCSharp.dll;
...
}
}
}


The situation is: when the outside caller calls Test::func(), the
MyCSharp.dll is NOT there (due to some installer constraint). By right,
since the if condition is always a false, we expect no problem for
running Test::func() because we don't actually use anything in
MyCSharpt.dll, however the OS still tries to load MyCSharp.dll at
runtime, and throws exception since the dll cannot be found.

So our questions are:
1. In this case, when is the exact time that the dll gets loaded? Are
there any official document for this assembly loading behavior?

2. Is there any way to make it work at runtime without removing the call
into MyCSharp.dll (we want to keep the code there but it will never get
executed during runtime)?

Your help is greatly appreciated.


Best regards,
Chen Zhuo
 
G

Guest

I think it's at the method level (I'm sure someone can give a much better
explanation), put your call into another assembly in another method. I
remember seeing code like this in a CLR Inside-Out article about performance,
the scenario they used was for calling code in an exception handler that
hardly ever runs (hence, don't want to load the dll most of the time).

class Test
{
public static void MyFunc()
{
if ( false )
RunCodeInOtherAssembly();
}

private void RunCodeInOtherAssemly()
{
...call into MyCSharp.dll;
}
}

Cheers
Kieren
 
C

Chen Zhuo

Hi KierenH,


Thanks for your reply, but here it still tries to load the assembly even
when I factor the code so that the calling into the assembly is in
another method that never gets executed.

The assembly loading behavior we are seeing here is that the assembly
loading is at source code file level. Whenever calling an API in the
managed cpp file that has "#using MyCSharp.dll", it tries to load
MyCSharp.dll immediately, regardless of whether the invoked API really
needs MyCSharp.dll.

Could you find any official documentation that clearly states the .NET
assembly loading behavior from managed C++? We searched through the web
but didn't find much useful info.

Thanks a lot!


Best regards,
Chen Zhuo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top