Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / Visual Basic

Registering a COM DLL with VS 2005: A Walk-Through

Rate me:
Please Sign up or sign in to vote.
3.71/5 (11 votes)
18 Aug 2008CPOL5 min read 68.7K   619   30   6
This article demonstrates how to register a COM DLL with Visual Studio 2005.

Introduction

In my previous article "Creating a COM DLL with VS 2005, A Walk-Through", we walked-through creating a COM DLL with Visual Studio 2005, using Visual Basic .NET.

In this article, we are going to walk-through registering our COM DLL.

Background

If you are an experienced developer, and you've worked with DLLs before, you've probably already tried using RegSvr32, and have experienced some difficulties. If you're an experienced .NET developer, you may have tried using RegAsm, and have encountered some challenges there as well.

In this article, we are not going to look at the more complex approaches of registering a COM DLL. Instead, we are going to walk-through creating a VS 2005 Installer, which will both install and register our COM DLL for us, while providing a simple, professional tool to accomplish this task.

As a professional software developer, and an experienced COM DLL developer, I personally prefer using an Installer. Additionally, my clients appreciate it, because whenever they need to install a COM DLL, they are able to.

Creating the Setup Project

If you followed along with my previous article "Creating a COM DLL with VS 2005, A Walk-Through", then open that project now. If you did not follow my previous article, but have your own COM DLL that you have created, then open the project that contains that COM DLL now.

To begin with, let's add a new "Setup and Deployment" project to our solution:

  • Click on the File menu | Add | New Project...
  • In the Add New Project window, make the following selections:
    • Under "Project types":
      • Click the plus (+) sign next to "Other Project Types" to expand it.
      • Click on "Setup and Deployment" to select it.
    • Under "Templates":
      • Click "Setup Project" to select it.
    • Change the Name to "Library Installer".
    • Click the OK button when finished.

The Add New Project window:

AddNewProject.jpg

Now that the new "Library Installer" setup project has been added to the Solution, your Solution Explorer should look like the picture below. If Solution Explorer is not open, then open it by clicking on View menu | Solution Explorer.

SolutionExplorer.jpg

Configure Installer Properties

Now that we have the installer added to the Solution, let's configure its properties.

  • In Solution Explorer, left-click on the installer ("Library Installer") to select it.
  • Now, go to the Properties window (F4 or View menu | Properties window).
  • Set the following properties in the Properties window:
    • Author: set this to your name.
    • InstallAllUsers: set this to True.
    • Manufacturer: set this to "Common Files".
    • ProductName: set this to "My Libraries".
    • Title: set this to "Library Installer".

Here's the Properties window:

LibraryInstallerProperties4.jpg

Add Project Output to Installer

The next thing we need to do is add the files that we want to the installer to install, which in this case is our COM DLL. The installer will not only install our COM DLL, but it will take care of registering it for us as well!

First, make sure your "File System" tab is visible. If it's not, follow these steps:

  • Go to Solution Explorer (View menu | Solution Explorer).
  • Right-click on the installer project ("Library Installer") menu | View | File System.

Next, configure the file system:

  • Add the "Program Files Folder": In the pane on the left, right-click on "File System on Target Machine" | Add Special Folder | Program Files Folder.
  • AddProgramFilesFolder.gif

  • Add a "Common Files" folder: Right-click on the "Program Files Folder" that we just added, select Add | Folder from the popup menu.
    • Rename the folder to "Common Files".

    AddFolder.jpg

  • Add a "My Libraries" folder: Right-click on the "Common Files" folder we just created, select Add | Folder from the popup menu.
    • Rename the folder to "My Libraries".

When you've added all of the folders, the file system should look like this:

MyLibrariesFolder.gif

Just a brief explanation: What we are doing is, we are telling the installer to install our COM DLL in the following location: C:\Program Files\Common Files\My Libraries.

  • Add our COM DLL:
    • Click on the "My Libraries" folder to select it.
    • In the pane on the right, right-click to bring up the popup menu, select Add | Project Output.
    • AddProjectOutputMenu.jpg

      • The "Add Project Output Group" window will open.
      • Select Project: "MyComDll".
      • Select "Primary Output".
      • Select Configuration: "(Active)".
      • Click the OK button when you are finished.
      • AddProjectOutputGroup.jpg

When you're finished, it should look like this:

MyLibrariesFolderWithTlb.jpg

Finally Save and Build both projects.

  • View Solution Explorer (View menu | Solution Explorer).
  • Right-click on the installer ("Library Installer").
    • Select Build from the popup menu.

    BuildInstaller.jpg

Run the Installer from Within Visual Studio

We have finished our installer! Now we are ready to test it out!

  • In Solution Explorer, right-click on the installer ("Library Installer").
    • Select "Install" from the popup menu.

    InstallInstaller.jpg

When you run the Installer, observe the following:

  • On the "Select Installation Folder" window:
    • The installation path is: C:\Program Files\Common Files\My Libraries\
    • "Everyone" is selected.

installer.jpg

Using Our Registered COM DLL in VB6

We now have a registered COM DLL ready to be used in VB6. Let's go into VB6 and see how to use it.

Create a new VB6 Project:

  • Start VB6.
  • When the "New Project" window opens, select "Standard Exe".
  • Click the "Open" button.
  • Add a Button to the Form, and double-click it to create the Click() event, and open the code window.

Add a reference to our COM DLL:

  • Click the Project menu | References.
  • Click the "Browse" button to open the "Add Reference" window.
  • Navigate to the "My Libraries" folder ("C:\Program Files\Common Files\My Libraries").
  • Select the "MyComDll.tlb" file (notice the ".tlb" - not ".dll").
  • Click the "Open" button to close the "Add Reference" window.
  • Click the "OK" button to close the "References" window and add our COM DLL as a reference.

Finally, in the Click() event for our Command Button, add code to utilize our COM DLL:

VB
Private Sub Command1_Click()

    Dim mcc As New MyComClass
    mcc.DisplayMessage
        
End Sub
  • Run the VB6 application (F8, or Debug menu | Step Into).
  • Click the button on the form.

Here's the result of our test:

VB6Test.jpg

Conclusion

As you can see, creating a setup project to install and register your COM DLL is very easy with VS 2005! Additionally, it is very easy for anyone to use, and looks very professional!

I hope this article is helpful to you!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

 
QuestionRegistro DLL vb.net no vb6 Pin
Member 152347327-Jun-21 5:11
Member 152347327-Jun-21 5:11 
QuestionNice tutorial Pin
mysystme19-Jun-14 2:16
mysystme19-Jun-14 2:16 
GeneralProblem with mscoree.dll after doing unistall Pin
rafaelnenninger30-Jul-09 6:42
rafaelnenninger30-Jul-09 6:42 
GeneralRe: Problem with mscoree.dll after doing unistall Pin
SafetyManNew31-Aug-09 6:17
SafetyManNew31-Aug-09 6:17 
GeneralKnow bug in VS2005 & VS2008 [modified] Pin
Jamie Clayton25-Aug-08 13:22
Jamie Clayton25-Aug-08 13:22 
I'll give the heads up to save you grief, but this approach is easy to demo, very painful in reality for production applications and long term MSI & COM tlb registation upgrades over time. If you implement this for production code please watch your build version numbers for the application and MSI. If you just use the MSI to register the COM objects your likely to consume significant developer trouble shooting time, if you don't implement another registration technique besides the Microsoft Visual Studio generated MSI installation(VS2005 or VS2008). Try a *.bat file or Installer classes which can automate Regasm.exe command line options to manually register your COM.net objects.

See the following Microsoft article which includes the following quote:

Registering assemblies with COM interfaces does not work
This is a known RegAsm bug. If your assembly has a dependency, for example on another class library, RegisterCOM might not work because RegAsm is called to obtain the registration information. Because RegAsm is called in the \obj directory, the dependency is not found, and RegAsm fails without notification. The best workaround is to add the assembly by hand from the \bin directory. Another workaround is to use RegisterSelfReg.
Also, make sure that you manually register using RegAsm/Codebase. If your assembly is not in a shared location, it is not found unless it is in the same directory as the calling code. /Codebase enters the directory into the registry.


I've got 3-4 weeks of a developer resource identifying and resolving this issue. It caused significant pain to our services and our client where we used this approach. Learn from our pain!

Jamie Clayton

Senior Application Developer
Jenasys Design Pty Ltd Australia

modified on Wednesday, August 27, 2008 12:46 AM

GeneralVery Nice Pin
ianhunt0118-Aug-08 19:52
ianhunt0118-Aug-08 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.