Exemplo n.º 1
0
        public static IAssemblyCache GetIAssemblyCache(String assemblyName, FUSION_INSTALL_REFERENCE reference)
        {
            if (reference != null)
            {
                if (!InstallReferenceGuid.IsValidUninstallGuidScheme(reference.GuidScheme))
                {
                    throw new ArgumentException("Invalid reference guid.", "guid");
                }
            }

            //  Create an assembly cache objet.
            IAssemblyCache ac = null;
            int            hr = FusionImports.CreateAssemblyCache(out ac, 0);

            if (hr < 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            return(ac);

            //////////  Uninstall the assembly.
            ////////hr = ac.UninstallAssembly(0, assemblyName, reference, out disp);
            ////////if (hr < 0)
            ////////    Marshal.ThrowExceptionForHR(hr);
        }
Exemplo n.º 2
0
        public FUSION_INSTALL_REFERENCE GetNextReference()
        {
            IInstallReferenceItem item = null;
            int hr = _refEnum.GetNextInstallReferenceItem(out item, 0, IntPtr.Zero);

            if ((uint)hr == 0x80070103)
            {
                // ERROR_NO_MORE_ITEMS
                return(null);
            }

            if (hr < 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            IntPtr refData;
            FUSION_INSTALL_REFERENCE instRef = new FUSION_INSTALL_REFERENCE(Guid.Empty, String.Empty, String.Empty);

            hr = item.GetReference(out refData, 0, IntPtr.Zero);
            if (hr < 0)
            {
                Marshal.ThrowExceptionForHR(hr);
            }

            Marshal.PtrToStructure(refData, instRef);
            return(instRef);
        }
Exemplo n.º 3
0
        public static void InstallAssembly(String assemblyPath, FUSION_INSTALL_REFERENCE reference, AssemblyCommitFlags flags)
        {
            if (reference != null)
            {
                if (!InstallReferenceGuid.IsValidInstallGuidScheme(reference.GuidScheme))
                {
                    throw new ArgumentException("Invalid reference guid.", "guid");
                }
            }

            IAssemblyCache ac = null;

            int hr = 0;

            hr = FusionImports.CreateAssemblyCache(out ac, 0);
            if (hr >= 0)
            {
                hr = ac.InstallAssembly((int)flags, assemblyPath, reference);
            }

            if (hr < 0)
            {
                if (hr == -2146234300 /*0x80131044*/)
                {
                    throw new AssemblyMustBeStronglyNamedException();
                }
                else
                {
                    Marshal.ThrowExceptionForHR(hr);
                }
            }
        }
Exemplo n.º 4
0
        static int Main(string[] args)
        {
            // Process arguments

            bool   install  = true; // Initialise variables
            string filename = "";
            bool   help     = false;
            bool   versions = false;
            bool   list     = false;

            if (args.Length == 0)
            {
                help = true;      // No arguments will print a help message
            }
            if (args.Length == 1) // One argument could be /H /V or /L a filename
            {
                if (string.Compare(args[0], "/H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;                                                                         // Test for help switch
                }
                if (string.Compare(args[0], "-H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;
                }
                if (string.Compare(args[0], "/V", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    versions = true;                                                                         // Test for help switch
                }
                if (string.Compare(args[0], "-V", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    versions = true;
                }
                if (string.Compare(args[0], "/L", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    list = true;                                                                         // Test for list switch
                }
                if (string.Compare(args[0], "-L", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    list = true;
                }
                filename = args[0];
            }

            if (args.Length == 2)
            {
                if (string.Compare(args[0], "/H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;                                                                         // Test for help switch
                }
                if (string.Compare(args[0], "-H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;
                }
                if (string.Compare(args[1], "/H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;
                }
                if (string.Compare(args[1], "-H", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    help = true;
                }

                if ((string.Compare(args[0], "/U", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(args[0], "-U", StringComparison.OrdinalIgnoreCase) == 0))
                {                       // Found uninstall switch in first argument
                    install  = false;
                    filename = args[1]; // Filename must be second argument
                }
                else
                {
                    filename = args[0]; // Filename must be first argument
                    if ((string.Compare(args[1], "/U", StringComparison.OrdinalIgnoreCase) == 0) || (string.Compare(args[1], "-U", StringComparison.OrdinalIgnoreCase) == 0))
                    {
                        install = false; // Uninstall switch found as second argument
                    }
                }
            }

            // help, versions, install and filename now contain the supplied parameters
            if (help)
            {
                // Help requested so print message
                Console.WriteLine("GACInstall adds and removes assemblies from the cache");
                Console.WriteLine("");
                Console.WriteLine("Usage: GACInstall AssemblyFileName");
                Console.WriteLine("       GACInstall /U AssemblyName");
                return(0);
            }

            if (versions)
            {
                // versions requested so list the required information
                Console.WriteLine(System.Reflection.Assembly.GetExecutingAssembly().FullName);
                Console.WriteLine(System.IO.Directory.Exists("fusion.dll"));
                Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
                Console.WriteLine("Full path to fusion.dll: " + System.IO.Path.GetFullPath("fusion.dll") + "#");
                Console.WriteLine("Path: " + System.Environment.GetEnvironmentVariable("PATH"));
                return(0);
            }

            if (list)
            {
                // List ASCOM assemblies
                SortedList <String, String> assemblyNames = new SortedList <String, String>();
                IAssemblyEnum assemblyEnumerator          = AssemblyCache.CreateGACEnum(); // Get an enumerator for the GAC assemblies
                IAssemblyName iAssemblyName = null;

                while ((AssemblyCache.GetNextAssembly(assemblyEnumerator, out iAssemblyName) == 0))
                {
                    try
                    {
                        AssemblyName assemblyName = new AssemblyName();
                        try
                        {
                            assemblyName.Name        = AssemblyCache.GetName(iAssemblyName);
                            assemblyName.Version     = AssemblyCache.GetVersion(iAssemblyName);
                            assemblyName.CultureInfo = AssemblyCache.GetCulture(iAssemblyName);
                            assemblyName.SetPublicKeyToken(AssemblyCache.GetPublicKeyToken(iAssemblyName));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("GetAssemblyName Exception: " + ex.ToString());
                        }

                        assemblyNames.Add(assemblyName.FullName, assemblyName.Name);
                    }
                    catch
                    {
                        // Ignore an exceptions here due to duplicate names, these are all MS assemblies
                    }
                }

                Console.WriteLine("\r\nInstalled ASCOM assemblies: ");

                foreach (KeyValuePair <string, string> assemblyName in assemblyNames)
                {
                    //  Process each assembly in turn
                    try
                    {
                        if (((assemblyName.Key.IndexOf("ASCOM", StringComparison.OrdinalIgnoreCase) + 1) > 0) || (assemblyName.Key.Contains("565de7938946fba7")))
                        {
                            Console.WriteLine(assemblyName.Key);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("AssemblyName.Key Exception: " + ex.ToString());
                    }
                }
                return(0);
            }

            // Must be install or uninstall
            int result = 0; // Initialise return code to OK

            // Create an FUSION_INSTALL_REFERENCE struct and fill it with data
            // We use opaque scheme here
            FUSION_INSTALL_REFERENCE[] installReference = new FUSION_INSTALL_REFERENCE[1];
            installReference[0].dwFlags             = 0;
            installReference[0].guidScheme          = AssemblyCache.FUSION_REFCOUNT_OPAQUE_STRING_GUID;
            installReference[0].szIdentifier        = "GACInstall";
            installReference[0].szNonCannonicalData = "Installed by ASCOM's GACInstall program";
            installReference[0].cbSize = 40;

            // Get an IAssemblyCache interface
            IAssemblyCache pCache = AssemblyCache.CreateAssemblyCache();

            if (install)
            { // We are going to install an assembly
                try
                {
                    if (System.IO.File.Exists(filename))
                    {
                        result = pCache.InstallAssembly(0, filename, null);
                        if (result == 0)
                        {
                            Console.WriteLine("Installed OK!");
                        }
                        else
                        {
                            Console.WriteLine("Install returned error code: " + result);
                        }
                        return(result);
                    }
                    else
                    {
                        Console.WriteLine("Unable to find file: " + filename);
                        return(1);
                    }
                }
                catch (Exception ex)
                {
                    result = 2;
                    Console.WriteLine("Exception: " + ex.ToString());
                    return(result);
                }
            }
            else // We are going to uninstall an assembly
            {
                try
                {
                    {                                                                                                                                            // We are going to uninstall an assembly
                        IASSEMBLYCACHE_UNINSTALL_DISPOSITION puldisposition = IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNKNOWN; // Initialise variable
                        result = pCache.UninstallAssembly(0, filename, null, out puldisposition);
                        if (result == 0)
                        {
                            Console.WriteLine("Uninstalled with no error!");
                        }
                        else
                        {
                            Console.WriteLine("Uninstall returned error code: " + result);
                        }

                        switch (puldisposition)
                        {
                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_ALREADY_UNINSTALLED:
                            Console.WriteLine("Outcome: Assembly already uninstalled"); break;

                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_DELETE_PENDING:
                            Console.WriteLine("Outcome: Delete currently pending"); break;

                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_HAS_INSTALL_REFERENCES:
                            Console.WriteLine("Outcome: Assembly has remaining install references"); break;

                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_REFERENCE_NOT_FOUND:
                            Console.WriteLine("Outcome: Unable to find assembly - " + filename); break;

                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_STILL_IN_USE:
                            Console.WriteLine("Outcome: Assembly still in use"); break;

                        case IASSEMBLYCACHE_UNINSTALL_DISPOSITION.IASSEMBLYCACHE_UNINSTALL_DISPOSITION_UNINSTALLED:
                            Console.WriteLine("Outcome: Assembly uninstalled"); break;

                        default:
                            Console.WriteLine("Unknown uninstall outcome code: " + puldisposition); break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    result = 2;
                    Console.WriteLine("Exception: " + ex.ToString());
                }

                return(result);
            }
        }