I just saw this go by on an internal DL and it seemed worth documenting.
What is the correct way determine what framework version is installed on a computer to choose the correct CLR hosting approach?
I would like to use CLR 4.0 runtime if CLR 4.0 is installed (and use CLRCreateInstance -> ICLRMetaHost->GetRuntime), but if it is not installed, I’d use CorBindToRuntimeEx.
According to MDSN, GetCORVersion is deprecated. What should I use instead? ICLRRuntimeInfo::EnumerateInstalledRuntimes would not work in an environment where CLR 4.0 is not installed, correct?
I would like my code to work in CLR 2.0+ environment.
And the answer:
- LoadLibrary mscoree
- GetProcAddress for CLRCreateInstance. If you get NULL, fall back to legacy path (CorBindToRuntimeEx)
- Call CLRCreateInstance to get ICLRMetaHost. If you get E_NOTIMPL, fall back to legacy path (same as above)
- Otherwise, party on the ICLRMetaHost you just got
My kingdom for sample code...
Rick Strahl has some samples here:
http://www.west-wind.com/wconnect/weblog/ShowEntry.blog?id=631
But it looks like he's using the deprecated APIs for .NET 2.0. I'm on a platform where I know at least .NET 3.5 will be installed. Or should be. If not, I fail gracefully.
Posted by: Louis | October 06, 2010 at 12:09
OK. I found some very good sample code for loading the Runtime host and calling methods on a .NET DLL from an unmanaged app. There are two samples in the All-in-One Code Framework. One that targets CLR 1.0/2.0 and another that shows 2.0/4.0.
Posted by: Louis | October 07, 2010 at 10:11