I recently created a DLL and I wanted to reference it from a project I was developing in Visual Studio.  In previous versions of Windows, doing so was simply a matter of dropping the DLL file in the C:\Windows\assembly folder.  That would add the DLL to the Global Assembly Cache (GAC) and make it accessible in Visual Studio.  However, as is often the case, Window 7 is different.  Even if you have Administrator privileges on your machine, you still do not have permission to drop a file in the assembly folder. 

Undaunted, I thought about using the old DOS command line utility gacutil.exe.  Microsoft developed the tool as part of the .Net framework, and it is available in the Windows SDK Framework Tools.  If you have never used gacutil.exe before, you can find out everything you ever wanted to know but were afraid to ask here: http://msdn.microsoft.com/en-us/library/ex0ss12c(v=vs.80).aspx .  Unfortunately, if you do not have the Windows SDK loaded on your development machine, you will need to install it to use gacutil, but it is relatively quick and painless, and the framework tools are very useful.  Look here for your latest SDK: http://www.microsoft.com/download/en/search.aspx?q=Windows%20SDK .  

After installing the SDK, I tried installing my DLL to the GAC by running gacutil from a DOS command line:

image

That’s odd.  Microsoft is shipping a tool that cannot be executed even with Administrator rights?  Let me stop here and say that I am by no means a Windows security expert, so I actually did contact my system administrators, and they were not sure how to fix the problem….there must be a super administrator access level, but it isn’t available to your average developer in my company.  The solution outlined here is working within the boundaries of a normal windows Administrator.

So, now the hacker in me bubbles to the surface.  What if I were to create a simple BAT file containing the gacutil command?  It’s so crazy it just might work! 

image

image

Ugh!  I was starting to think this would never work, but then I realized that simply executing a batch program did not change my level of access.  Typically in Windows 7, you would select the “Run As Administrator” option to temporarily act as an administrator for the purpose of executing a process.  However, that option is not available for BAT files run from the command line.  SOLUTION: Create a desktop shortcut to execute the BAT file, which in turn will execute the line command…..are you still with me? 

I created a shortcut and pointed it to my batch file.  Theoretically, all I need to do now is right-click on the shortcut and select “Run As Administrator” and we’re good, right?  Well, kinda.  If you notice the syntax of my BAT file, the name of the DLL is passed in as a parameter.  Therefore, I either have to hard-code the file name in the BAT program (YUCK!!), or I can leave the parameter and drag the DLL file to the shortcut and drop it.  Sweet, drag-and-drop works for me…..but if I use the drag-and-drop method, there is no way for me to right-click and select “Run As Administrator”.  That is not a problem…..I simply have to adjust the properties of the shortcut I created and I am in business.  I Right-clicked on the shortcut and select “Properties”.  Under the “Shortcut” tab there is an “Advanced” button…..I clicked it.

image

All I needed to do was check the “Run As Administrator” box:

image

In summary, what I have done is create a BAT file to execute a command line utility, gacutil.exe.  Then, rather than executing the BAT file from the command line, I created a desktop shortcut to run it and set the shortcut properties to “Run As Administrator”.  This will effectively mean I am executing the command line utility with Administrator privileges.  Pretty sneaky.

Now, when I drag the DLL file  over to the shortcut, it starts the BAT file and adds the DLL to the assembly cache.  I created another BAT file to remove a DLL from the GAC in case the need should arise.  The code for that is:

image

Give it a try.  I can’t imagine why updating the GAC has been made into such a chore in Windows 7.  Hopefully there is a service pack in the works that will give developers the functionality they had in Windows XP, but in the meantime, this workaround is extremely useful.


Feedback

# re: Adding a DLL to the GAC in Windows 7

Very interesting article, great debate!
http://hostcarioca.com.br 4/14/2012 12:22 PM | Antony

# re: Adding a DLL to the GAC in Windows 7

Same solution.... You can right click VS Commandline option and select "Run as Administrator" and from there you can do all this stuff.

9/14/2012 12:07 PM | Mahboob

# re: Adding a DLL to the GAC in Windows 7

There' much easier, similar to previous manner listed here:

http://www.thespgeek.com/2010/11/how-to-add-dll-to-gac-in-windows-7.html

And this works. I've had this tried. 11/16/2012 5:50 AM | Rocky

# re: Adding a DLL to the GAC in Windows 7

Dear Jim
You only have to run the explorer as Admin and You can drop Your dlls in the assembly directory. I just did it with PublicDomain!
1/8/2013 11:07 AM | Wolfgang Medlitsch

# re: Adding a DLL to the GAC in Windows 7

This was totally awesome - I salute you! Been trying to figure this one out for a while, is awesome you were able to do it and write about it. You've done your industry a great service. Thank you!

@Jim, I don't know what you were doing, but I was in as an account with local Admin privileges and could not drag/drop DLLs in the GAC running Explorer as an Admin. This is disabled. I tried that before finding this blog. 2/12/2013 2:51 PM | Tom

# re: Adding a DLL to the GAC in Windows 7

Exactly what I needed, the solution worked like a charm for Win2k8 R2 64bit.
Thanks! 5/29/2013 10:46 AM | Appreciate it

# re: Adding a DLL to the GAC in Windows 7

Run Command Prompt as administrator, then run GACUTIL inside your administrator: command prompt. 11/22/2013 2:35 PM | Kristopher_D

Post a comment