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:
- Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
- 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.
- Name the project GACDemo.
- 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"
- 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:
- At the command prompt, type cd.
- Right-click in the command prompt window to paste the path.
- Press ENTER to change to the directory where Sn.exe is located.
- At the command prompt, run the following command:
sn -k "C:\GACKey\GACkey.snk"
- 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.
- Modify the AssemblyKeyFileAttribute assembly attribute in the code as follows:
- 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.
- 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:
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.
- Open two instances of Windows Explorer.
- In one instance of Windows Explorer, locate the .dll file output for your class library project.
- In the other instance of Windows Explorer, locate the following folder:
- Drag the .dll file to the 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("")];
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:
- Click Project, and then click ProjectName Properties.
Note ProjectName represents the name of the project.
- Expand Configuration Properties, and then click General.
- 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:
Verify that it works
- Start Windows Explorer.
- Locate the following folder:
C:\[SystemRoot]\Assembly Notice that GACDemo is in the list of installed .dll files.