Click here to Skip to main content
14,034,935 members
Click here to Skip to main content
Add your own
alternative version


22 bookmarked
Posted 7 Jul 2009
Licenced CPOL

Creating a COM DLL with VS 2005: Advanced Walkthrough

, 7 Jul 2009
Rate this:
Please Sign up or sign in to vote.
This article demonstrates how to manually create a COM DLL with VS 2005.


This article demonstrates how to create a COM DLL in Visual Studio 2005 the manual way.  It also demonstrates how to install it into the GAC with gacutil.exe, and how to register the library using the Assembly Registration Tool (regasm.exe).

To learn how to create a COM DLL the easy way, see my article "Creating a COM DLL with VS 2005:  A Walkthrough"

To learn how to register a COM DLL the easy way, see my article "Registering COM DLL with VS 2005 Walk-Through



After almost a year from posting my super simple article "Creating a COM DLL with VS 2005: A Walkthrough", I was surprised to find quite a few ratings lower than a 5. I figured there are probably a lot of developers that want to know the answer to the question: "How can I do this the hard way?" Well, for those sadistic sophisticated inquiring minds, here you go…

Just as a note, I'm thinking about writing an article about "Creating a COM DLL with the Visual Basic Command-line Compiler", which will use Notepad and Vbc.exe, so if you're really hard-core you may look for that. 


Creating the COM DLL Project

*** Just as a note, please follow these instructions closely.
*** If your COM DLL doesn't work when you're done, then you may have missed something.

To begin with, let's create a new Visual Basic.NET Class Library

  • Open Visual Studio 2005
  • Click on the File menu | New | Project…
  • In the New Project window, select:
    • "Visual Basic" from the Project Type.   
    • "Class Library" for the Templates.
    • Enter "MyComDemo" for the Name.
    • Check "Create directory for solution".
    • Click the "OK" button when you are finished.


Programming the Class

First of all, notice that when Visual Studio created the project, it automatically added a class named "Class1".  Let's start our programming by changing the name of "Class1" to something more meaningful, like "MyCalculator".

  • View the Solution Explorer (View menu | Solution Explorer)
  • Right-click on Class1.vb, and select Rename from the drop down menu.


  • Change the  file name from "Class1.vb" to " MyCalculator.vb". (This will change both the name of the file and the name of the class at the same time.)

Let's add a method to our class that we can later call from VB6:



Making it COM Interoperable

Now that we have our class programmed, we're ready to make our class COM Interoperable.

The first thing we need to do is Import the System.Runtime.InteropServices namespace.  This is required to obtain necessary interop attributes. 

Add the following to the top of the class:

            Imports System.Runtime.InteropServices


Define the Interface

The next thing we need to do is define an interface for our class. This is necessary, because this is what a COM client uses to communicate with a COM object.

To define an interface for our class, we will add the <ClassInterface()> attribute to our class, which will automatically generate an interface for us. This will ensure that each public member of our class will be exposed to COM.

  • Although it is technically optional, adding this attribute to our class allows the COM caller to take advantage of early binding, which is more type safe than late biding, and results in better performance.
  • Each class in our project that we want to expose to COM needs to have this attribute added to it.
  • There are 3 options available for the ClassInterface() constructor, we will use AutoDual:
    • AutoDispatch:  Interface will only support late binding
    • AutoDual:  Interface will support early binding and late binding.
    • None:  Interface will not support early binding or late binding.  (This option can be used if you have created your own strongly-type interface)

Our class should now look like this:



Add a GUID

After our Interface has been added, we will need to add a specific GUID, which is a 128 bit number that gets added to the registry, and is used to identify a COM type.  (GUID is a Globally Unique Identifier)

To generate a GUID:

  • Click on the Tools menu | Create GUID
  • Select the Registry Format Option
  • Click the "Copy" button to copy the new GUID to the Windows Clipboard.
  • Click the "Exit" button to close the window.



Now that we have our GUID, let's add the Guid() attribute between the ClassInterface() attribute and our class declaration.  Then, paste in the GUID. * Be sure to remove the curly braces {}.

Your class should now look like this:



Define a Strong Name 

All Com Interop assemblies should be Signed, and installed into the GAC (Global Assembly Cache).  This is not technically required, if the assembly is not installed into the GAC, then it needs to be copied into the same folder as the COM program that is using it.

To sign the assembly:

  • Click on the Project Menu | MyComDemo Properties…
  • Click on the Signing tab.
  • Check the "Sign the assembly" checkbox.
  • Select "New…" from the "Choose a strong name key file:" drop-down list.
  • Create a Strong Name Key window:
    • Create a file name (eg. "MyComDemoKey")
    • Enter and confirm the password (optional)
    • Press the OK button.
    • Save and Close the Properties.



Register for Com Interop 

Since we have the Properties window open, click on the Compile Tab, then check "Register for COM interop" at the bottom of the window.


Finally, Save and close the Properties window.


Register our COM Interop Library 

Now that we have everything setup, change the solution configuration from Debug to Release, then save and compile the project.

Next, open the Visual Studio Command Prompt:

  • From the XP Start Menu, navigate to the Microsoft Visual Studio menu | Visual Studio Tools | Visual Studio 2005 Command Prompt.
  • Navigate to the directory where our compiled .dll is (bin\Release folder).

GAC: Installing our library into the Global Assembly Cache using the "gacutil.exe" utility makes our .dll globally available to all .Net assemblies, and prevents them from being copied into the application directory of the referencing assembly.

  • We must have Defined a Strong Name for our assembly before we can register it into the GAC (see Define A Strong Name above).
  • From the command prompt, type the following, then press enter: 

            gacutil –i MyComDll.dll 


Registry: Registering our library into the Windows Registry using the "Regasm.exe" utility makes our COM type library .tlb (.dll) globally available to all VB6 assemblies.

  • From the command prompt, type the following, then press enter:

            regasm MyComDll.dll /tlb:MyComDll.tlb


  • Our COM Type library (.tlb) has now been created, and is ready for use with VB6.

Note: "TlbExp.exe" is another utility that can be used to generate a COM type library from a .NET assembly, however it does not register it with the Windows Registry.


Using our new COM DLL in VB6 

We now have a functioning 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 new COM DLL:

  • Click the Project menu | References…
  • In the "Available References:" listbox, scroll down until you find our new library "MyComDemo", and then check it.
  • Click the "OK" button to close the "References" window.



Finally, in the Click() event of our Command Button, add the following code to utilize our new library:  

Private Sub Command1_Click()

    Dim calc As New MyCalculator
    Dim result As Integer

    result = calc.Add(5, 5)

    MsgBox result

End Sub

To test our new COM DLL:

  • Run the VB6 application (F8 or Debug menu | Step Into).
  • Click the button on the form.

Here's the result of our test:




When you are finished working through this article, you may want to remove the library we created from the registry. Here's how:

  • Open the Visual Studio Command Prompt.
  • Navigate to the directory where our compiled .dll is (bin\Release folder)
  • From the command prompt, type the following, then press enter:

            gacutil –u MyComDll.dll




As you can see, it's quite a bit more work to create a COM DLL the manual way, including installing it into the GAC and registering it into the registry.

One thing to keep in mind is that if you try to install your COM DLL into the GAC, and register it in the registry on a computer other than your own development PC, you may find that the gacutil and regasm are not installed. If that is the case, you will need to install the .NET Framework Tools. Here is a link that has more information about them:

.NET Framework Tools.



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


About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

You may also be interested in...


Comments and Discussions

GeneralThanks!! Pin
Hernan.NET15-Sep-11 8:53
memberHernan.NET15-Sep-11 8:53 
GeneralExcellent article Pin
mr20011-Nov-10 7:45
membermr20011-Nov-10 7:45 
GeneralMy vote of 5 Pin
mr20011-Nov-10 7:40
membermr20011-Nov-10 7:40 
GeneralCOM Rules! Pin
Vitaly Tomilov3-May-10 8:52
memberVitaly Tomilov3-May-10 8:52 
GeneralHallucinant Pin
Cristian Amarie10-Jul-09 7:03
memberCristian Amarie10-Jul-09 7:03 
GeneralRe: Hallucinant Pin
Mike Klimentiev14-Jul-09 8:21
memberMike Klimentiev14-Jul-09 8:21 
GeneralRe: Hallucinant Pin
Cristian Amarie15-Jul-09 18:46
memberCristian Amarie15-Jul-09 18:46 
GeneralRe: Hallucinant Pin
Vitaly Tomilov3-May-10 8:44
memberVitaly Tomilov3-May-10 8:44 
GeneralRe: Hallucinant Pin
Cristian Amarie7-May-11 8:47
memberCristian Amarie7-May-11 8:47 
General[My vote of 1] Nothing new Pin
Md. Marufuzzaman7-Jul-09 22:23
mentorMd. Marufuzzaman7-Jul-09 22:23 
GeneralRe: [My vote of 1] Nothing new Pin
CS Rocks8-Jul-09 6:43
memberCS Rocks8-Jul-09 6:43 
GeneralRe: [My vote of 1] Nothing new Pin
VMykyt8-Jul-09 21:02
memberVMykyt8-Jul-09 21:02 
GeneralRe: [My vote of 1] Nothing new Pin
jpaulino9-Jul-09 2:32
memberjpaulino9-Jul-09 2:32 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190424.1 | Last Updated 7 Jul 2009
Article Copyright 2009 by CS Rocks
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid