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
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
- General familiarity with assemblies in the Microsoft .NET Framework.
- General familiarity with the use of tools from the command
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
- 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
- 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:
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
- Press ENTER to change to the
directory where Sn.exe is located.
- At the command prompt, run the following
- 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
- 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:
Complete code listing (AssemblyInfo.cpp)
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.
// Version information for an assembly includes the following four values:
// Major Version
// Minor Version
// Build Number
// You can specify all the value or you can default the Revision and Build Numbers
// by using the '*' as shown below:
// 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.
// (*) 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.
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
For more information about the global assembly cache, visit the following Microsoft Developer Network (MSDN) Web
Article ID: 815807 - Last Review: May 21, 2007 - 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
|kbhowtomaster kbcodesign kbcommandline kbmanaged KB815807|