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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


KierenH wrote:
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

"Chen Zhuo" wrote:

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

.



Relevant Pages

  • Re: Probelms when Pascal calls a C DLL
    ... The C DLL actually loads another C DLL ... Don't load DLLs while a DLL is already being loaded. ... >> decalred as Global variables) in the C-DLLis lets say 0x32d900, ... > The calling convention in Delphi might be misdeclared. ...
    (comp.lang.pascal.delphi.misc)
  • re: how to debug load-time dll exception?
    ... i did an experiment by putting ole32.dll into the appinit_dlls registry ... so this must have something more to do with ole32.dll than my dll. ... > calling imported functions other than those located in kernel32.dll may ... > some functions in their dlls call loadlibrary to load other system ...
    (microsoft.public.win32.programmer.kernel)
  • Re: running a service/driver/application upon startup
    ... debug version of the image is too large to load onto the device. ... I ran dumpbin/exports on the finger dll and saw the required function names: ... > theories as to why it's not getting loaded but just doing 'services load ...
    (microsoft.public.windowsce.embedded)
  • RE: binding failure on 3rd party assembly - works in Debug, not in
    ... and in Release build regasm also fails trying to load ... it works in Debug builds. ... I have 5 different x64 versions of this dll. ... Use this to trace the file system behaviors of your applicaiton. ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Unable to load custom driver on WinCE 6
    ... I'm trying to make and load a simple DLL on WinCE 6 by following the ... I've made a subproject at PB project to include the simple DLL ... At the target device, the mydriver.dll file is in the Windows ... I wrote a simple application to load the driver dynamically ...
    (microsoft.public.windowsce.embedded)