Here's how I ended up automating the process of registering and GAC-ing my dll.
First, I created a directory in the root folder of the solution called "ScriptedDevDellRegistration". I copied the most recent versions of gacutil.exe
and RegAsm.exe
(with their corresponding .config files) into this directory.
Next, I added a file into that directory called RegDll.cmd
. This file will register the dll with its tlb and then install it in the GAC. This is useful when needing to consume an assembly in the VB6 development environment via COM interop. The file's contents are:
cd "%~dp0"
regasm MyDll.dll /tlb:MyDll.tlb /u
regasm MyDll.dll /tlb:MyDll.tlb
gacutil /u MyDll
gacutil /i MyDll.dll
I also added a file called UnGacDll.cmd
that will uninstall the assembly from the GAC. When running a console test application that uses an assembly in a different project, and that assembly has been added to the GAC, I've had trouble getting debug to work. This file simply removes the dll from the GAC so that I can debug more easily:
cd "%~dp0"
gacutil /u MyDll
Now that I've got my files set up, I need to edit the Build Events of my project.
In the project's properties (I'm using VS2010 with VB.NET) for the assembly being compiled and registered/GAC'd, click the compile tab and click the "Build Events" button. In the post-build events, add code that is similar to the following:
IF not "$(ConfigurationName)" == "Release Scripted Dev Dll Registration" GoTo elseIf1
:default
COPY "$(TargetPath)" "$(SolutionDir)ScriptedDevDllRegistration\$(TargetFileName)"
"$(SolutionDir)ScriptedDevDllRegistration\RegDll.cmd"
GOTO exit
:elseIf1
IF not "$(ConfigurationName)" == "Debug ConsoleTest" GoTo elseIf2
"$(SolutionDir)ScriptedDevDllRegistration\UnGacDll.cmd"
GOTO exit
:elseIf2
IF not "$(ConfigurationName)" == "Debug Console Web Library" GoTo exit
"$(SolutionDir)ScriptedDevDllRegistration\UnGacDll.cmd"
GOTO exit
:exit
The cmd scripting is kinda ugly, but very useful. I use different build configurations to drive which scripts get executed in the post-build events. For example, the first line looks for a build configuration named "Release Scripted Dev Dll Registration", which copies the DLL to the subdirectory I made and then executes RegDll.cmd
to register the assembly and its type libraries as well as GAC the assembly.
The other configurations, "Debug ConsoleTest" and "Debug Console Web Library," work best when the dll is not in the GAC, so the post-build event calls UnGacDll.cmd
for those configurations.
I'm sure it's possible to script some of this functionality without physically copying dll's or the regasm.exe or gacutil.exe utilities, but the solution I explained above worked well for me.