views:

68

answers:

1

I'm working on a .NET library that is intended to be consumed in a VB6 project. To set up the dll to be used on my developer machine, I use regasm and gacutil, like so:

regasm /tlb:MyDll.tlb Mydll.dll 

gacutil /i Mydll.dll 

On each subsequent call, I also unregister/uninstall the assembly:

regasm /u /tlb:MyDll.tlb Mydll.dll
regasm /tlb:MyDll.tlb Mydll.dll 

gacutil /u Mydll
gacutil /i Mydll.dll 

Currently, I'm doing this from the Visual Studio Tools command prompt. Is there a way to script these commands into one execution path?

NOTE: This script is intended only for my developer machine where regasm and gacutil are both readily available.

A: 

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.

Ben McCormack