Exemple #1
0
        public static IClrRuntimeInfo GetRuntime(IEnumUnknown runtimes, String version)
        {
            Object[] temparr = new Object[3];

            UInt32 fetchedNum;

            do
            {
                runtimes.Next(Convert.ToUInt32(temparr.Length), temparr, out fetchedNum);

                for (Int32 i = 0; i < fetchedNum; i++)
                {
                    IClrRuntimeInfo t = (IClrRuntimeInfo)temparr[i];

                    // version not specified we return the first one
                    if (String.IsNullOrEmpty(version))
                    {
                        return(t);
                    }

                    // initialize buffer for the runtime version string
                    StringBuilder sb  = new StringBuilder(16);
                    UInt32        len = Convert.ToUInt32(sb.Capacity);
                    t.GetVersionString(sb, ref len);
                    "version: {0}".info(sb.str());
                    if (sb.ToString().StartsWith(version, StringComparison.Ordinal))
                    {
                        return(t);
                    }
                }
            } while (fetchedNum == temparr.Length);

            return(null);
        }
        bool IsCLRVersionInstalled(Version clrVersion)
        {
            object pCLRMetaHost;
            bool   isRuntimeVersionDetermined = false;

            if (SafeNativeMethods.ERROR_SUCCESS == SafeNativeMethods.CLRCreateInstance(CLSID_CLRMetaHost, typeof(IClrMetaHost).GUID, out pCLRMetaHost))
            {
                IClrMetaHost metaHost = (IClrMetaHost)pCLRMetaHost;
                IEnumUnknown enumUnknownPtr;

                enumUnknownPtr = metaHost.EnumerateInstalledRuntimes();

                object[] pUnk = new object[1];
                int      pceltFetched;

                while (SafeNativeMethods.ERROR_SUCCESS == enumUnknownPtr.Next(1, pUnk, out pceltFetched) && !isRuntimeVersionDetermined)
                {
                    int           bufferSize = 256;
                    StringBuilder builder    = new StringBuilder(256);

                    IClrRuntimeInfo runtimeInfo = (IClrRuntimeInfo)pUnk[0];
                    runtimeInfo.GetVersionString(builder, ref bufferSize);
                    Version installedClrVersion;

                    if (TryGetVersionFromString(builder, out installedClrVersion))
                    {
                        if (clrVersion.Major == installedClrVersion.Major && clrVersion.Minor == installedClrVersion.Minor)
                        {
                            isRuntimeVersionDetermined = true;
                        }
                    }
                }
            }

            return(isRuntimeVersionDetermined);
        }