none
Forcing managed dll to load at startup RRS feed

  • Question

  • Hi all

    I've got a mixed managed C/C++ executable which uses a managed C# dll. At the moment I've just added a reference to the dll in the executable's project properties and the dll loads as if by magic when I call it's static method. :) :) :)

    Problem is there is a noticeable pause, and I want to be able to load at startup instead. How can I do this? LoadLibrary() doesn't seem to do anything. I could perhaps put a managed block in the startup code which calls an empty method in the dll, but thats dodgy....

    lanm
    Monday, June 2, 2008 12:40 AM

Answers

  • Hi Lanm,

    You can use Assembly.Load method in order to dynamically load your managed assembly ahead of time.
    Vitaliy Liptchinsky
    • Marked as answer by lanm Wednesday, June 4, 2008 2:30 AM
    Monday, June 2, 2008 10:30 AM
  • You are relying on a thunk the compiler generates that automatically loads mscoree.dll and the CLR when you call the native export.  Short from loading the CLR yourself with CorBindToRuntimeEx(), after which you can load assemblies, I'd just export a do-nothing function that you call in your .exe startup code.
    Hans Passant.
    • Marked as answer by lanm Wednesday, June 4, 2008 2:30 AM
    Monday, June 2, 2008 12:24 PM
    Moderator

All replies

  • I'm not entirely sure I understand what you mean, but do you mean that when your program starts, you would like some code in that managed assembly to execute before the rest of your program executes?

    If so, then I would guess the right way to do that would be to force its execution. In C# I would put the code into the static Main method, before the code that sets up Application and loads the main form.

    I'm fairly sure I don't understand your problem, so please correct me.

    http://presentationmode.blogspot.com/
    Monday, June 2, 2008 7:50 AM
  • Hi Lanm,

    You can use Assembly.Load method in order to dynamically load your managed assembly ahead of time.
    Vitaliy Liptchinsky
    • Marked as answer by lanm Wednesday, June 4, 2008 2:30 AM
    Monday, June 2, 2008 10:30 AM
  • You are relying on a thunk the compiler generates that automatically loads mscoree.dll and the CLR when you call the native export.  Short from loading the CLR yourself with CorBindToRuntimeEx(), after which you can load assemblies, I'd just export a do-nothing function that you call in your .exe startup code.
    Hans Passant.
    • Marked as answer by lanm Wednesday, June 4, 2008 2:30 AM
    Monday, June 2, 2008 12:24 PM
    Moderator
  • Thanks all; I'll use System::Reflection::Assembly::Load() or just use an empty method to force the dll to load.

    And to Lasse - I was talking about how to control when the dll was loaded because I didn't have any code which did this explicitly; it was all automatic and magic. :) Unfortunately the main method is in C and hence can't be compiled with /clr and be managed code, but that is a whole other kettle of fish. I'll just have to have some managed C++ code the C can call to load the C#. :S
    Wednesday, June 4, 2008 2:30 AM