Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

GAC Installer

3.72/5 (10 votes)
14 Mar 2008CPOL1 min read 2   563  
A command line GAC installer that removes assemblies even if they are installed by an MSI.

Introduction

The Gacutil application is the most common tool for installing/uninstalling items to/from the Global Assembly Cache. However, it has some problems:

  1. The application is not distributed with the .NET framework.
  2. Inability to uninstall the assembly if it is installed by an MSI installer.
  3. Difficult to use if you uninstall the assembly and do not know the fully qualified assembly name (including the PublicToken).

An attempt to uninstall the assembly installed by an MSI will generate an error message like the one below:

D:\temp\aa>gacutil /u "MySDKDll, Version=1.0.0.0, 
Culture=neutral, PublicKey Token=da66bde628dcedb8, 
processorArchitecture=MSIL" 
Microsoft (R) .NET Global Assembly Cache Utility. Version 2.0.50727.42 
Copyright (c) Microsoft Corporation. All rights reserved. Assembly: MySDKDll, 
Version=1.0.0.0, Culture=neutral, PublicKeyToken=da66bde 628dcedb8, 
processorArchitecture=MSIL 
Unable to uninstall: assembly is required 
by one or more applications Pending references: 
SCHEME: <windows_installer />ID: <msi />DESCRIPTION : <windows installer="" />
Number of assemblies uninstalled = 0 
Number of failures = 0

There are utilities that can do the job, like GacView, but it is not command-line driven, and therefore can not be used as a part of the installer (apart from licensing issues).

The presented utility can be controlled from a command line. It removes the assemblies even if they are installed by a MSI. In order to remove such an assembly, Registry entries have to be cleared:

LocalMachine\Software\Microsoft\Installer\Assemblies\Global
   CurrentUser\Software\Microsoft\Installer\Assemblies\Global

The utility identifies the entries in these keys and removes the content.

C#
private void ClearRegKey(string AssemblyShortName, RegistryKey BaseKey) 
{ 
    RegistryKey key = BaseKey.OpenSubKey(@"Software\Microsoft\" + 
                                         @"Installer\Assemblies\Global", true); 
    
    if (key != null) 
    { 
        string[] names = key.GetValueNames(); 
        foreach (string Name in names) 
        { 
            string[] Words = Name.Split(&#39;,&#39;); 
            string nn = Words[0]; 
            string nn2 = Words[4]; 
            if (AssemblyShortName == nn) 
            { 
                key.SetValue(Name, "", RegistryValueKind.String); key.Close(); 
            }
            return; 
        } 
    }
}

How to use the utility

Examples:

  • Installing the assembly:
  • GacInstaller.exe i "C:/Program Files/ABCD/MySDKDll.DLL"
  • Uninstalling the assembly:
  • GacInstaller.exe u MySDKDll - just the name
    GacInstaller.exe u MySDKDll da66bde628dcedb8 - using name and public token

If installation/uninstallation is unsuccessful, the application form turns red and the error information is written to the log file.

Credits

The utility uses the GAC API wrapper created by Junfeng Zhang. The original wrapper code is available here.

Version

The current version is 1.0.0. The most recent version can be downloaded from www.dotnetremoting.com along with other useful components.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)