Microsoft KB Archive/815807

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

Article ID: 815807

Article Last Modified on 5/21/2007

-

APPLIES TO


 * 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

-







For a Microsoft Visual Basic .NET version of this article, see 315682.



For a Microsoft Visual C# .NET version of this article, see 815808.



SUMMARY
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 \Assembly folder, where   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.

Requirements
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 &quot;[DriveLetter]:\[DirectoryToPlaceKey]\[KeyName].snk&quot;

 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 .</li> Right-click in the command prompt window to paste the path.</li> Press ENTER to change to the directory where Sn.exe is located.</li></ol> </li> At the command prompt, run the following command:

sn -k &quot;C:\GACKey\GACkey.snk&quot;

</li> 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.</li>  Modify the AssemblyKeyFileAttribute assembly attribute in the code as follows: [assembly:AssemblyKeyFileAttribute(&quot;C:\\GACKey\\GACKey.snk&quot;)]; </li> 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.</li> You can install the built .dll file by using the Global Assembly Cache Tool (Gacutil.exe) or by using Microsoft Windows Explorer: <ul> If you use the Global Assembly Cache Tool, you can use the following command:

gacutil -I &quot;[DriveLetter]:\[PathToDebugDirectoryInVSProject]\GACDemo.dll&quot;

</li> If you use Windows Explorer, follow these steps: <ol style="list-style-type: lower-alpha;"> Open two instances of Windows Explorer.</li> In one instance of Windows Explorer, locate the .dll file output for your class library project.</li> In the other instance of Windows Explorer, locate the following folder:

c:\[SystemRoot]\Assembly

</li> Drag the .dll file to the Assembly folder.</li></ol>

Note You cannot install an assembly in the global assembly cache on a remote computer by dragging the .dll file to the \\ \c$\[SystemRoot]\assembly folder.</li></ul> </li></ol>

Complete code listing (AssemblyInfo.cpp)

 * 1) include &quot;stdafx.h&quot;

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(&quot;&quot;)]; [assembly:AssemblyDescriptionAttribute(&quot;&quot;)]; [assembly:AssemblyConfigurationAttribute(&quot;&quot;)]; [assembly:AssemblyCompanyAttribute(&quot;&quot;)]; [assembly:AssemblyProductAttribute(&quot;&quot;)]; [assembly:AssemblyCopyrightAttribute(&quot;&quot;)]; [assembly:AssemblyTrademarkAttribute(&quot;&quot;)]; [assembly:AssemblyCultureAttribute(&quot;&quot;)];

// // 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(&quot;1.0.*&quot;)];

// // 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(&quot;C:\\GACKey\\GACKey.snk&quot;)]; [assembly:AssemblyKeyNameAttribute(&quot;&quot;)]; 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.
 * 1) Expand Configuration Properties, and then click General.
 * 2) 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)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

Verify that it works
<ol> Start Windows Explorer.</li> Locate the following folder:

C:\[SystemRoot]\Assembly

Notice that GACDemo is in the list of installed .dll files.</li></ol>

<div class="references_section">