How do you delay load a managed DLL from within an unmanaged app?

How do you delay load a managed DLL from within an unmanaged app?

Ask Question

10 years, 6 months ago

10 years, 6 months ago

1k times



I have a native c++ app, and I want to use some managed types that are in a separate managed dll. I believe that one way to do this and still keep the c++ app totally native is to use COM interop with .NET. However my problem is that my app has to initially run on machines that don’t have the CLR installed, so I don’t want the CLR to be loaded unless I actually make use of the codepath that calls into the managed DLL.

How can I delay load the managed DLL?

c# .net visual-studio visual-studio-2008 com

share|improve this question

asked Sep 15 ’09 at 14:25

Leeks and LeaksLeeks and Leaks

1,18011 gold badge1414 silver badges2424 bronze badges

add a comment

2 Answers





I think the best way to achieve this is via COM interop, native to managed in this case. If you’re app is native it wont’t load the CLR by default. You can “delay load” the CLR by creating a COM object which is defined in the managed assembly only when it’s actually required. The COM layer of the CLR will take care of loading the CLR at that point and returning a CCW to the managed object.

share|improve this answer

answered Sep 15 ’09 at 14:27


627k130130 gold badges11371137 silver badges13811381 bronze badges

I tried this, but in order to use COM interop, you need to compile with /clr:oldSyntax, and just doing that alone causes the CLR to be loaded, without even actually calling any COM interop > managed DLL code. Does this sound like the correct behavior?

– Leeks and Leaks
Sep 15 ’09 at 14:45

@Leaks, it shouldn’t. If you export the TLB from your managed assembly and register it you should be able to create the object just like any other COM object. It should be indistinguishable in native code from any other COM object.

– JaredPar
Sep 15 ’09 at 14:49

You’re right, however the TechNet article I read on how to import the assembly TLB said I had to have the /clr:oldSyntax flag set in the compiler. This turned out to be completely false.

– Leeks and Leaks
Sep 22 ’09 at 15:26

add a comment


I would suggest making a COM callable wrapper for the managed component.

You can have your own .NET class with an interface that can be referenced by unmanaged code (in my case I had Classic ASP instantiating an object that was built with .NET).

share|improve this answer

edited Sep 15 ’09 at 14:35

answered Sep 15 ’09 at 14:27


9,19866 gold badges3535 silver badges8787 bronze badges

add a comment

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password


Post as a guest


Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged c# .net visual-studio visual-studio-2008 com or ask your own question.

The Overflow Blog

Tracking down performance pitfalls in Vue.js

Ways to Help the Fight Against COVID-19 from Home

Featured on Meta

Planned maintenance scheduled for Saturday, March 28, 2020 at 13:00 UTC (9AM…

An Update On Creative Commons Licensing

Community and Moderator guidelines for escalating issues via new response…

How does the Triage queue work?

Triage needs to be fixed urgently, and users need to be notified upon…


674How can I rename a project folder from within Visual Studio?17calling managed code from unmanaged code848How would you count occurrences of a string (actually a char) within a string?0Is it possible for an unmanaged C++ app to only load the CLR when it needs managed types?7Calling a native callback from managed .NET code (when loading the managed code using COM)2Mixed Managed/Unmanaged Assembly Load Order38How to diagnose COM-callable wrapper object creation failure?0Loading a .NET DLL from Resources inside a Win32/clr DLL1Using native registration free C++ COM server from managed app

Hot Network Questions

Are force vectors members of a vector space?

What is the correct way to convert 2 bytes to a signed 16-bit integer?

Re-scale and move vector layer manually in QGIS

Why don’t iron meteorites on Mars rust or oxidize? Why are they shiny?

How do “tip jet” helicopters cancel the torque effect of the main rotor?

What is a BFF Word™?

what enables cloudflare to disable direct ip address access?

Align list ignoring negative sign of the first number in items

What’s the proper english name for this mechanical tool to remove bolts of the door?

Why does camouflage make some planes fly slower?

Abelian category equivalent to a non-abelian category

A simple encryption program using Python

Will you have to prove US citizenship to receive the $1200 COVID-19 bailout money?

Birds on a wire (again) – how is it that birds feel no current ? They are just making a paralell circuit, no?

Draw an ASCII oak tree (or a woman with curly hair?)

Cat Keeps Drinking Unattended Drinks

Does there exist an aggregation of videos on optimization?

What are those large poles for that are always in front of US stand-alone store buildings?

Is Safari’s “Close Tabs to the Right” a new feature released in macOS 10.15.4?

Does an instrument like this exist?

Should I capitalize Brothers?

Why would a ‘first world’ alien opt to have a brood of dumb children rather than one smart one?

VertexContract and contraction of vertices of degree 2

Is it true that Netscape Navigator eventually became Mozilla Firefox?

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


Leave a Reply

Your email address will not be published. Required fields are marked *