[this gets TL; DR ... sorry ...]
I am working on a huge (mostly) C ++ / MFC application with hundreds of DLLs; it supports the dynamically loaded "add" mechanism through COM, so add-ons can be developed in .NET using COM interoperability. Some limited new features are developed in .NET without using this add-in mechanism (although they are still dynamically loaded); however, the end user may decide not to use this feature. Therefore, .NET cannot be loaded at startup.
But when does .NET load , I need to do some specific .NET initialization (in particular, set CurrentUICulture to match my own / unmanaged user interface).
One solution is to simply punt and do this .NET initialization when the code starts loading either the new .NET function modules or COM. Given the nature of this application, this is probably a 95% solution (most users will use the new functionality).
But this is not perfect. Someone can โeasilyโ add new .NET functionality at any time by creating a module with the / clr flag (remember that this is a huge application).
Another reliable (and obvious) solution simply causes .NET to load when launched through C ++ / CLI. But some of the smart C ++ developers for whom every byte and clock cycle do not want to do this; somewhat understandable since there is no need to set CurrentUICulture if / before loading .NET.
Another possibility that I was thinking about is to hook the LoadLibrary and look for mscorlib . Now I know that .NET for some reason loads, loads it in normal mode and initializes before other code does something. But the LoadLibrary binding (or something else, for that matter) is really not what I want to do.
So, is there a simple (ier) / best way to tell if .NET should load?
Edit: The LockClrVersion read response is pretty close. The only hiccup is that it will not work if you connect to a DLL / mixed-mode assembly.
// ClrAboutToLoad.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <MSCorEE.h> // http://community.bartdesmet.net/blogs/bart/archive/2005/07/22/2882.aspx FLockClrVersionCallback begin_init, end_init; STDAPI hostCallback() { printf("hostCallback()\n"); // we're in control; notify the shim to grant us the exclusive initialization right begin_init(); ICLRRuntimeHost *pHost = NULL; HRESULT hr = CorBindToRuntimeEx(NULL, L"wks", STARTUP_SERVER_GC, CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (PVOID*) &pHost); hr = pHost->Start(); // mission completed; tell the shim we're ready end_init(); return S_OK; } int _tmain(int argc, _TCHAR* argv[]) { LockClrVersion(&hostCallback, &begin_init, &end_init); //fnTheDLL(); HMODULE hModule = LoadLibrary(L"TheDLL"); FARPROC fp = GetProcAddress(hModule, "fnTheDLL"); typedef void (*fnTheDLL_t)(); fnTheDLL_t fnTheDLL = reinterpret_cast<fnTheDLL_t>(fp); fnTheDLL(); FreeLibrary(hModule); return 0; }