Esempio n. 1
0
        private static string GetLocationImpl(IAssemblyCache assemblyCache, string strongName, string targetProcessorArchitecture)
        {
            ASSEMBLY_INFO pAsmInfo = new ASSEMBLY_INFO
            {
                cbAssemblyInfo = (uint)Marshal.SizeOf(typeof(ASSEMBLY_INFO))
            };

            if (targetProcessorArchitecture != null)
            {
                strongName = strongName + ", ProcessorArchitecture=" + targetProcessorArchitecture;
            }
            int hr = assemblyCache.QueryAssemblyInfo((uint)AssemblyCacheEnum.QUERYASMINFO_FLAG_VALIDATE, strongName, ref pAsmInfo);

            if ((Win32NTMethods.Failed(hr) && (hr != Win32NTMethods.E_INSUFFICIENT_BUFFER)) || (pAsmInfo.cbAssemblyInfo == 0))
            {
                return(string.Empty);
            }
            pAsmInfo.pszCurrentAssemblyPathBuf = new string(new char[pAsmInfo.cchBuf]);
            return(!Win32NTMethods.Failed(assemblyCache.QueryAssemblyInfo(3, strongName, ref pAsmInfo)) ? pAsmInfo.pszCurrentAssemblyPathBuf : string.Empty);
        }
Esempio n. 2
0
        public static string GetLocation(string strongName)
        {
            string location = strongName ?? throw new ArgumentNullException(nameof(strongName));

            if (IsStrongName(strongName))
            {
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    IAssemblyCache cache;

                    AssemblyName name = null;
                    try
                    {
                        name = new AssemblyName(strongName);
                    }
                    catch (FileLoadException)
                    {
                        return(location);
                    }

                    if (!Win32NTMethods.Failed(Win32NTMethods.CreateAssemblyCache(out cache, 0)) && (cache != null))
                    {
                        try
                        {
                            if (name.ProcessorArchitecture != ProcessorArchitecture.None)
                            {
                                location = GetLocationImpl(cache, strongName, null);
                            }
                            else
                            {
                                string targetProcessorArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
                                if (!string.IsNullOrEmpty(targetProcessorArchitecture))
                                {
                                    location = GetLocationImpl(cache, strongName, targetProcessorArchitecture);
                                }
                                if (string.IsNullOrEmpty(location))
                                {   // let's try the MSIL Architecture
                                    location = GetLocationImpl(cache, strongName, "MSIL");

                                    if ((location == null) || (location.Length <= 0))
                                    {
                                        location = GetLocationImpl(cache, strongName, null);
                                    }
                                }
                            }
                        }
                        finally
                        {
                            Marshal.FinalReleaseComObject(cache);
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(location) && string.Equals(Path.GetExtension(strongName), ".dll", StringComparison.OrdinalIgnoreCase))
                        {
                            location = GetLocation(Path.ChangeExtension(strongName, null));
                        }
                    }

                    return(location);
                }
                else
                {
                    throw new PlatformNotSupportedException();
                }
            }

            return(location);
        }