Giving Your DLL a Strong Name

Summary

In this post I’m going to talk a little about strong naming a dll and some of the gotchas associated with strong naming.

About Strong Names

If you’ve written .Net programs as long as I have, you’ve run into a problem where a dll does not have a strong name (also known as a signed dll).  Most dlls produced by Microsoft or downloaded using NuGet are already strong named and you don’t have to worry about them.  If you are creating a .Net program without using COM then you probably will also not have to worry about this problem.  If, however, you are creating a COM interface in order to use .Net code for your legacy non-.Net code, then you’ll need to strong-name your COM module as well as any dll that the COM library is dependent on (that is not already strong named).

How to Strong Name or Sign a DLL

Making your project create a strong-named dll is easy.  First you’ll need to generate a snk file.  There is an sn utility that is used to create this file and you can probably use the following syntax to generate the file (I have tested this on Windows 7, 10 and 2008R2):

"C:Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sn" -k YourProjectName.snk

Technically, you can name this file anything you want, but I like to make it the project name to keep the file naming consistent with each project.  Now you need to link this to your project.  Right-click on your project, select “Add”, “Existing Item…”.

You’ll need to change your file type filter list to show all files (All files *.*).  Click on the snk file you created (don’t double-click).  The trick at this point is to click on the little down triangle next to the “Add” button and choose “Add as Link”. Then your snk file will be added as a link.

Now you need to right-click on the project again and select properties.  In the list of tabs on the left of the properties screen, select “Signing”.

Now click on the check box labeled “Sign the assembly”.  Then select your snk file from the drop-down list box and click on the save button (the floppy icon, or ctrl-s).

Now you can re-build your library and a strong-named dll will be created.

Checking DLL Strong Name

After you add a project or dll to another project, you can look in the references to find your dll.  Click on that dll and check the properties window.  You’ll see a property named “Strong Name”.  This should be set to “True”:

As I mentioned before, most dlls that ship with Visual Studio are already strong-named, but you might run into a situation where a package from NuGet is not strong-named.  In that instance you might have to find the source (probably GitHub or BitBucket) and download the source and perform the steps above to make it strong-named.

 

Leave a Reply