Register a managed .NET DLL in the GAC

From NSIS Wiki
Jump to navigationJump to search
Author: pixolut (talk, contrib)


The Function

; Function: AddManagedDLL
; Author: Joe Cincotta ( joe@pixolut.com )
; Date: 27/7/2004
; Bug fix by Alessio Saltarin ( http://www.littlelite.net/ )
; Register a .NET managed DLL in the Global Assembly Cache (GAC).
; And make it appear in the VS IDE 'Add References' dialog.
;
; Rewrite by Joe Cincotta 7/6/2005
;
; NOTE: This version expects .NET 1.1, if you require older 1.0 
; then ensure you change the framework path...
; 
; Use by passing the full path of the DLL and the Assembly Name.
; Make sure you remove the RegKey when performing uninstall!
;
; Usage:
;   Push $SYSDIR\myDll.dll
;   Push "MyAssemblyName"
;   Call AddManagedDLL
;
 
Function AddManagedDLL
  Exch $R0
  Exch
  Exch $R1
 
  nsExec::Exec '"$WINDIR\Microsoft.NET\Framework\v1.1.432\GacUtil.exe" /i "$R0" /f'
  WriteRegStr HKLM "SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\$R1" "" "$R0"
  WriteRegStr HKCU "SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\$R1" "" "$R0"
  WriteRegStr HKLM "SOFTWARE\Microsoft\VisualStudio\7.1\AssemblyFolders\$R1" "" "$R0"
 
FinishReg:
  Pop $R1
  Pop $R0
FunctionEnd

Issues

The location of executables such as 'GACUtil.exe' may vary, depending on the version(s) of the .Net runtime installed on the target machine. Instead of hard-coding a path to such utilities as above, you could use a function to determine this information at install-time, such as Get directory of installed .NET runtime.

Alternatively, if the target machine not have gacutil.exe (for example if SDK is not installed) doing a .NET script to do the same is very easy, just add (C# sample):

- Add reference to System.EnterpriseServices assembly and

using System.EnterpriseServices.Internal;
...
Publish p = new Publish();
// for GAC Installation
p.GacInstall(file); // for gac installation
...
p.GacRemove(file); // for gac removing
...
p.RegisterAssembly(file); // for registering assembly for interop
...
p.UnRegisterAssembly(file); // to unregister assembly

- The above will not work in most cases on Vista, since you need permissions to do it. So we need some way to do the above from within NSIS. I have no idea how.

- A proper way to install Assemblies to the GAC is documented by Microsoft here http://support.microsoft.com/kb/317540/en-us