我阅读了有关通过检查注册表项来确定所安装的.NET Framework版本的各种文章。但是我在article中读到,不同的.NET Framework版本需要在其上运行的不同CLR版本。因此,从注册表中我知道在Dev服务器上安装了.NET Framework 4.8。而且我已经运行了clrver.exe,并确定我安装了CLR版本4.0.30319,它最多仅支持.NET Framework v4.5.2。这一切都在我的Dev服务器上。我想为我的测试和生产服务器了解相同的信息。我可以从注册表中获取.NET Framework版本,但是由于clrver.exe是Visual Studio工具,如何在未安装Visual Studio的计算机上确定CLR版本?
答案 0 :(得分:-1)
clrver.exe 调用CLRCreateInstance
所以您可以做同样的事情=>
我明白了:
版本:v2.0.50727
版本:v4.0.30319
object pCLRMetaHost;
HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, typeof(ICLRMetaHost).GUID, out pCLRMetaHost);
if (hr == HRESULT.S_OK)
{
ICLRMetaHost CLRMetaHost = (ICLRMetaHost)pCLRMetaHost;
IEnumUnknown pEnumUnknown;
hr = CLRMetaHost.EnumerateInstalledRuntimes(out pEnumUnknown);
if (hr == HRESULT.S_OK)
{
object[] pUnknown = new object[1];
uint uceltFetched;
while (HRESULT.S_OK == pEnumUnknown.Next(1, pUnknown, out uceltFetched) && (uceltFetched == 1))
{
int nBufferSize = 256;
StringBuilder sbBuffer = new StringBuilder(nBufferSize);
ICLRRuntimeInfo CLRRuntimeInfo = (ICLRRuntimeInfo)pUnknown[0];
CLRRuntimeInfo.GetVersionString(sbBuffer, ref nBufferSize);
Console.WriteLine("Version: {0}", sbBuffer);
}
}
Marshal.ReleaseComObject(pCLRMetaHost);
}
带有声明:
public enum HRESULT : int
{
S_OK = 0,
S_FALSE = 1,
E_NOINTERFACE = unchecked((int)0x80004002),
E_NOTIMPL = unchecked((int)0x80004001),
E_FAIL = unchecked((int)0x80004005),
}
[ComImport]
[Guid("D332DB9E-B9B3-4125-8207-A14884F53216")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICLRMetaHost
{
HRESULT GetRuntime(string pwzVersion, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppRuntime) ;
HRESULT GetVersionFromFile(string pwzFilePath, StringBuilder pwzBuffer, ref uint pcchBuffer);
HRESULT EnumerateInstalledRuntimes(out IEnumUnknown ppEnumerator);
HRESULT EnumerateLoadedRuntimes(IntPtr hndProcess, out IEnumUnknown ppEnumerator);
HRESULT RequestRuntimeLoadedNotification(IntPtr pCallbackFunction);
HRESULT QueryLegacyV2RuntimeBinding([In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppUnk);
HRESULT ExitProcess(int iExitCode);
}
[Guid("00000100-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IEnumUnknown
{
HRESULT Next([In, MarshalAs(UnmanagedType.U4)] uint celt, [Out, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.IUnknown, SizeParamIndex = 0)] object[] rgelt,
[MarshalAs(UnmanagedType.U4)] out uint pceltFetched);
HRESULT Skip(uint celt);
HRESULT Reset();
HRESULT Clone(out IEnumUnknown ppenum);
}
[Guid("BD39D1D2-BA2F-486a-89B0-B4B0CB466891")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface ICLRRuntimeInfo
{
HRESULT GetVersionString(StringBuilder pwzBuffer, ref int pcchBuffer);
HRESULT GetRuntimeDirectory(StringBuilder pwzBuffer, ref int pcchBuffer);
HRESULT IsLoaded(IntPtr hndProcess, out bool pbLoaded);
HRESULT LoadErrorString(uint iResourceID, StringBuilder pwzBuffer, out int pcchBuffer, int iLocaleID);
HRESULT LoadLibrary(string pwzDllName, out IntPtr phndModule);
HRESULT GetProcAddress(string pszProcName, out IntPtr ppProc);
HRESULT GetInterface([In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppUnk);
HRESULT IsLoadable(out bool pbLoadable);
HRESULT SetDefaultStartupFlags(int dwStartupFlags, string pwzHostConfigFile);
HRESULT GetDefaultStartupFlags(out int pdwStartupFlags, StringBuilder pwzHostConfigFile, ref int pcchHostConfigFile);
HRESULT BindAsLegacyV2Runtime();
HRESULT IsStarted(out bool pbStarted, out int pdwStartupFlags);
}
[DllImport("MSCorEE.dll", SetLastError = true)]
public static extern HRESULT CLRCreateInstance([In, MarshalAs(UnmanagedType.LPStruct)] Guid clsid, [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppInterface);
static Guid CLSID_CLRMetaHost = new Guid("{9280188d-0e8e-4867-b30c-7fa83884e8de}");