How to install an assembly in the global assembly cache in Visual C++ .NET and in Visual C++ 2005

This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual Basic .NET version of this article, see 315682.
For a Microsoft Visual C# .NET version of this article, see 815808.
This article describes how to generate a strong name for an assembly, and how to install a DLL in the global assembly cache (GAC). With the global assembly cache, you can share assemblies across multiple applications. The .NET runtime automatically installs the global assembly cache. Components are typically stored in the WINDIR\Assembly folder, where WINDIR is the name of your windows folder.

To install an assembly in the global assembly cache, you must give the assembly a strong name. A strong name is a cryptographic hash-key, or signature. This strong name makes sure that the component versioning is correct. This behavior helps to prevent components that have the same name from conflicting with each other or from incorrect usage by a consuming application.


The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Administrator rights to the computer where the shared assembly is installed.
This article assumes that you are familiar with the following topics:
  • General familiarity with assemblies in the Microsoft .NET Framework.
  • General familiarity with the use of tools from the command prompt.

Global assembly cache

Create a small class library project by using Microsoft Visual C++ .NET, generate a strong name, and then install the .dll file of the project in the global assembly cache. To do this, follow these steps:
  1. Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
  2. In Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates.

    In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Class Library (.NET) under Templates.

    Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Class Library under Templates.
  3. Name the project GACDemo.
  4. You must use a strong name. To generate this cryptographic key-pair, use the Strong Name command-line tool (Sn.exe). This tool is located in the \bin subdirectory where the .NET Framework Solution Developer Kit (SDK) is installed. Sn.exe is easy to use. The command-line statement takes the following form:
    sn -k "[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk"
  5. Create a folder, and then name it GACKey in C:\ so that you can easily locate the key, and so that you can easily access the key from the command prompt.

    Note For most users, the .NET Framework tools are located in the following folder:
    C:\Program Files\Microsoft.NET\FrameworkSDK\Bin
    Before you type the Sn.exe command in the next step, you may want to copy this similar path on your computer to the .NET bin folder. To do this, follow these steps:
    1. At the command prompt, type cd.
    2. Right-click in the command prompt window to paste the path.
    3. Press ENTER to change to the directory where Sn.exe is located.
  6. At the command prompt, run the following command:
    sn -k "C:\GACKey\GACkey.snk"
  7. This command generates a key that is not yet associated with the assembly of the project. To create this association, double-click AssemblyInfo.cpp in Solution Explorer. This file has the list of assembly attributes that are included by default when a project is created in Visual Studio .NET.
  8. Modify the AssemblyKeyFileAttribute assembly attribute in the code as follows:
  9. Press CTRL+SHIFT+B to compile the project. You do not have to add any additional code to install a .dll file in the global assembly cache.
  10. You can install the built .dll file by using the Global Assembly Cache Tool (Gacutil.exe) or by using Microsoft Windows Explorer:
    • If you use the Global Assembly Cache Tool, you can use the following command:
      gacutil -I "[DriveLetter]:\[PathToDebugDirectoryInVSProject]\GACDemo.dll"
    • If you use Windows Explorer, follow these steps:
      1. Open two instances of Windows Explorer.
      2. In one instance of Windows Explorer, locate the .dll file output for your class library project.
      3. In the other instance of Windows Explorer, locate the following folder:
      4. Drag the .dll file to the Assembly folder.
      Note You cannot install an assembly in the global assembly cache on a remote computer by dragging the .dll file to the \\ComputerName\c$\[SystemRoot]\assembly folder.

Complete code listing (AssemblyInfo.cpp)

#include "stdafx.h"using namespace System::Reflection;using namespace System::Runtime::CompilerServices;//// General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information// associated with an assembly.//[assembly:AssemblyTitleAttribute("")];[assembly:AssemblyDescriptionAttribute("")];[assembly:AssemblyConfigurationAttribute("")];[assembly:AssemblyCompanyAttribute("")];[assembly:AssemblyProductAttribute("")];[assembly:AssemblyCopyrightAttribute("")];[assembly:AssemblyTrademarkAttribute("")];[assembly:AssemblyCultureAttribute("")];		//// Version information for an assembly includes the following four values:////      Major Version//      Minor Version //      Build Number//      Revision//// You can specify all the value or you can default the Revision and Build Numbers // by using the '*' as shown below:[assembly:AssemblyVersionAttribute("1.0.*")];//// To sign your assembly, you must specify a key to use. For more information about // assembly signing, see the Microsoft .NET Framework documentation.//// Use the attributes below to control which key is used for signing. //// Notes: //   (*) If no key is specified, the assembly is not signed.//   (*) KeyName refers to a key that has been installed in the Crypto Service//       Provider (CSP) on your computer. KeyFile refers to a file that contains//       a key.//   (*) If the KeyFile and the KeyName values are both specified, the //       following processing occurs://       (1) If the KeyName can be found in the CSP, that key is used.//       (2) If the KeyName does not exist and the KeyFile does exist, the key //           in the KeyFile is installed in the CSP and used.//   (*) To create a KeyFile, you can use the sn.exe (Strong Name) utility.//        When specifying the KeyFile, the location of the KeyFile should be//        relative to the project directory.//   (*) Delay Signing is an advanced option - see the Microsoft .NET Framework//       documentation for more information about this.//[assembly:AssemblyDelaySignAttribute(false)];[assembly:AssemblyKeyFileAttribute("C:\\GACKey\\GACKey.snk")];[assembly:AssemblyKeyNameAttribute("")];
Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile this code sample.To do this, follow these steps:
  1. Click Project, and then click ProjectName Properties.

    Note ProjectName represents the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting on the right pane, click Apply, and then click OK.
For more information about the common language runtime support compiler options, visit the following Microsoft Web site:
/clr (Common Language Runtime Compilation)

Verify that it works

  1. Start Windows Explorer.
  2. Locate the following folder:
    Notice that GACDemo is in the list of installed .dll files.
For more information about the global assembly cache, visit the following Microsoft Developer Network (MSDN) Web sites:
GACUtil AssemblyKeyFileAttribute

Article ID: 815807 - Last Review: 01/17/2015 06:49:47 - Revision: 3.4

  • Microsoft Visual C++ .NET 2003 Standard Edition
  • Microsoft Visual C++ .NET 2002 Standard Edition
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C++ 2005 Express Edition
  • kbnosurvey kbarchive kbhowtomaster kbcodesign kbcommandline kbmanaged KB815807