Andrew Connell [MVP MOSS]
1350 Posts |  39 Articles |  3385 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
PDCBling
SharePoint Quick Links
Article Categories
Archives
Post Categories

View Andrew Connell's profile on LinkedIn

Add to Technorati Favorites

All paths in the command line instructions below assume all files are in the c:\ root. Enter full paths where necessary. The commands themselves (sn, ildasm, ilasm) can be run from anywhere if you're in a VS.NET command prompt which is different from a regular command prompt. To launch a VS.NET command prompt, you'll find a shortcut in the tools subgroup in the VS.NET installation program group in your start menu.

When you're building assemblies, it's not hard to apply a strong name to them.

  1. Create a strong name assembly key file
    From a VS.NET command prompt, use the SN.EXE utility to create a file containing the public key/private key pair:
    c:\>sn -k c:\keypair001.snk
  2. Add reference to the assembly key file to your assembly project
    Open the AssemblyInfo.cs file in your project and add the following attribute:
    [assembly: AssemblyKeyFile(@"c:\keypair001.snk")]
  3. Compile your assembly
    Same way you would any other day of the week.
  4. Obtain the public key to your key file
    You may need this in the future. After you build your assembly, from a VS.NET command prompt, use the SN.EXE utility again to get the public key of your key pair:
    c:\>sn -T yourAssembly.dll
  5. (Added 12/16) Obtain the public key blob to your key file
    You'll need this in CAS policy files... it's the hex public key blob... use the SECUTIL.EXE utility:
    c:\>secutil -hex -s yourAssembly.dll

However, what if you need to apply a strong name to an assembly that was provided, already compiled, to you and you don't have the source? You first will need to produce the Microsoft intermediate language (MSIL) for the assembly using the ILDASM.EXE utility, then use an assembly key file to sign that MSIL into a new DLL using the ILASM.EXE utility.

  1. Obtain the MSIL for the provided assembly
    From a VS.NET command prompt, enter the following:
    c:\>ildasm providedAssembly.dll /out:providedAssembly.il
  2. Rename/move the original assembly
    I just tack on ".orig" to the filename.
  3. Create a new assembly from the MSIL output and your assembly keyfile
    Assuming you already have an assembly key pair file (if not, see #1 in previous steps), do the following from a VS.NET command prompt:
    c:\>ilasm providedAssembly.il /dll /key=keypair001.snk

You run into a problem when you get a failure when generating the DLL from the MSIL and. For example, I keep having a problem when using this method to make the Microsoft Data Application Block have a strong name for use in a SharePoint web part. I ~really~ don't want to build it with a strong name myself, I want to rebuild the one that's included from the downloadable MSI. Ideas anyone?

Update 12/16: Added links to the four .NET utilities (sn, secutil, ildasm, ilasm) referenced in this article.

posted on Wednesday, December 15, 2004 4:22 PM

Feedback

# HOWTO: Strongly name prebuilt assemblies (from: Andrew Connell) 12/15/2004 7:29 PM TOURNEY LOGIC LINK BLOG


 re: HOWTO: Strongly name prebuilt assemblies 12/16/2004 4:19 PM Jason
I'm not sure that I understand the problem that you are having. With the Microsoft building blocks, you are given the source code, so can't you just use the first method referenced above?

# re: HOWTO: Strongly name prebuilt assemblies 12/16/2004 4:23 PM Andrew Connell
Jason - You certainly can... in fact that's what I did. However, what about the cases when you didn't have the source code? Yes, you can get the source code for the MSAB but I was ideally trying to avoid that.

 re: HOWTO: Strongly name prebuilt assemblies 2/7/2005 2:17 AM Yu Chai
Hi,
I successful to compile the program and install it as an application proxy. However, what should i do to the application blocks' dlls? ( i use the second methods).

It can't use the regsvcs.exe command for the dlls created by the application proxy.

# re: HOWTO: Strongly name prebuilt assemblies 2/7/2005 7:50 AM Andrew Connell
I'm not sure what you're asking... you don't need to register the DLLs... just include them as references in your projects.

 re: HOWTO: Strongly name prebuilt assemblies 8/16/2005 8:13 AM Andy
ildasm.exe != ilasm.exe ;)
Worked like a charm, excellent and thanks!

Cheers!!

# re: HOWTO: Strongly name prebuilt assemblies 9/8/2005 10:41 PM Kui He
Help me please,

My dll come from the Matlab 7.0,i try to include it as references in a COM+ server project.

I had used your second method to add the strong name to my a dll ,but failed.

error:"c:\xmlReadWriteTest_1_0.dll" have not a CLR head,can't build to .il file.

how can i do it? the dll is not a .NET dll.

# re: HOWTO: Strongly name prebuilt assemblies 9/9/2005 7:47 AM AC
Gravatar Kui- You need to first wrap the COM object so it can be used in a .NET project... then you can sign the wrapped version. You can't strongly name a COM object using the methods in my post.

# re: HOWTO: Strongly name prebuilt assemblies 12/19/2005 7:11 PM Jeff

Microsoft (R) .NET Framework IL Assembler. Version 1.1.4322.2032
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.
Assembling 'Microsoft.Web.UI.WebControls.dll' , no listing file, to EXE --> 'Microsoft.Web.UI.WebControls.il'
Source file is UNICODE

Microsoft.Web.UI.WebControls.dll(1) : error : syntax error at token 'σ' in: MZ

***** FAILURE *****

How to use ildasm for unicode?

Thanks,
Jeff

jeffz80@gmail.com

 re: HOWTO: Strongly name prebuilt assemblies 2/7/2005 11:21 PM Yu Chai
but when i complied the project. It generated two dlls. one is the project's dll and another one is the one from application blocks (gotdotnet.applicationblocks.data that based on your second method).
I can use regsvcs.exe to reg. the project's dll but i can't do so for the applicationblocks' dll. It stated that it should use the regasm.exe.
However, i don't know what should i do for the applicationblocks' dll. Where should i put the dll on? how can the client call it as it is not included in the application proxy?

 re: HOWTO: Strongly name prebuilt assemblies 12/8/2005 8:30 AM Dave Parker
Everything OK till the ilasm command, and then I get lots of errors like...

DNSLookup.il(959) : error -- Cannot compile native/unmanaged method
DNSLookup.il(966) : error -- Local (embedded native) PInvoke method, the resulting PE file is unusable

And then, at the bottom, I get "***** FAILURE *****"

# re: HOWTO: Strongly name prebuilt assemblies 12/9/2005 6:35 AM AC [MVP MCMS]
Gravatar Dave - Assuming your assembly is DNSLookup.dll... not sure what's going on, but I'd check in the .NET Framework newsgroups on this one. Never seen that error before.

# re: HOWTO: Strongly name prebuilt assemblies 9/13/2005 3:32 AM Kui He
Thanks for the response.
i have solved it by myself with tlbimp.exe.

tlbimp c:\xmlReadWriteTest_1_0.dll /keyfile:c:\mykey.snk /out:c:\interop.xmlReadWriteTest.dll /namespace:xmlReadWriteTest

then,i can include the interop.xmlReadWriteTest.dll as references in my COM+ server project.
Thanks.

# Assigning a strong-name to an existing (pre-built) assembly 5/18/2005 7:17 AM Matt's Weblog
I really have started to hate libraries that are distributed without a
strong-name.  If you are...

 Edit file DLL in dotnet 3/1/2005 2:01 AM Do Van Vinh
help me
after edit file dll of component in dotnet and resign but when buid one program use this file dll ................. run Error
This Component is SharpUI for Net

 re: HOWTO: Strongly name prebuilt assemblies 9/20/2007 11:31 AM Isai
Gravatar I had the same problem of signing a .NET wrapped COM DLL with a Strong Name. I followed your instructions step by step and everything worked great. Now I have the .NET wrapped COM DLL that is also Strong Name signed.

Later I noticed two issues. First, the DLL lost its version number. If I go to the properties of the DLL in Windows Explorer, the Version tab is missing. Second, the GACUTIL is unable to register the DLL in GAC. It's throwing the message "Old version error."

Please advice on 1) how to assign version number for .NET wrapped, Strong Name signed COM DLL, 2) how to install this DLL in GAC

Thanks,
Isai

 re: HOWTO: Strongly name prebuilt assemblies 12/12/2007 9:45 AM ricky brusch
Gravatar just include /resource=<name from disasm.res>

# re: HOWTO: Strongly name prebuilt assemblies 3/19/2008 7:07 AM Vrushali
Gravatar execute below steps

1. create key.snk for the dll

sn.exe -k D:\MSI\key.snk

2.deasseble the dll

ildasm.exe "D:\MSI\Hello.dll" /OUTPUT="D:\MSI\Hello.il"

3. again assemble with the key.snk

ilasm.exe "D:\test\Hello.il" /DLL /OUTPUT="D:\MSI\Hello.dll" /KEY=D:\MSI\key.snk

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 6 and 5 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelivant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2008 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 4-day hands-on and 5-day online WCM classes for developers I offer through the Ted Pattison Group.

Get more information on the WCM courses!


Upcoming Classes
 Hands-on WCM:
 » Atlanta, GA
   Sept 22-25, 2008
 Online WCM:
 » July 21-25, 2008
 » December 8-12, 2008


» Register today!

JAX Office Geeks
Jacksonville Office Geeks (JOG)
JOG is a special interest group in Jacksonville, FL dedicated to bringing the local SharePoint commnity together to share tips, tricks, ideas and best practices for developing solutions on the SharePoint platform.

Next meeting details...
When:
Thur. Sept 18th, 2008
  6-8p EDT
Topic:
Enterprise Content Management - Document Retention

Speaker:
John Holliday, MVP MOSS

RSVP Today!


» Subscribe to the JOG newsletter