Click here to Skip to main content
14,038,727 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


47 bookmarked
Posted 14 Aug 2008
Licenced CPOL

Creating a COM DLL with VS 2005: A Walkthrough

, 14 Aug 2008
Rate this:
Please Sign up or sign in to vote.
The article demonstrates how to create a COM DLL in VS2005.


This article demonstrates how to create a COM DLL in Visual Studio 2005 the easy way.

To learn how to register your COM DLLs, see my article "Registering COM DLL with VS 2005 Walk-Through"


As the only developer for a printing company, I get the privilege of maintaining lots of VB6 programs written by my predecessors. Every once in a while, I have to make a change to these programs - like adding emailing functionality, or incorporating suppression lists, or database updates.

Because I'm such a huge, die-hard fan of VB.NET, there's no way I'm going back to VB6 and muddling around with obsolete VB object models. Therefore, I have made it a practice of just creating COM DLLs with VS 2005 to work with these applications. That way, I get to utilize the robust .NET Framework instead! And that rocks!

When I first started developing COM DLLs, I saw quite a few articles that walked you through how to do it... The hard way. Why? I have no clue. But after I went through them a couple of times, I figured out a much easier way to do it... And life is good! So, why not share it?

Here's an example of how to create a simple COM DLL. Check the attachments for this example, plus a COM DLL library for emailing.

Creating the COM DLL project

Just as a note, please follow these instructions closely. Some have (thought they) followed them closely, but have found their COM DLLs not working. As it turns out, they missed something.

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

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


Modifying the COM DLL project

Now that we've created our DLL, let's look at what Visual Studio automatically did.

  • Open Solution Explorer (View menu | Solution Explorer).
  • Notice "Class1.vb". Visual Studio automatically added this class for us.

Let's remove "Class1.vb":

  • Right-click on "Class1.vb" in the Solution Explorer and select Delete from the menu.
    • Select "OK" to permanently delete when prompted.


Now, we can add a COM class:

  • Click the Project menu | Add New Item...
  • In the Add New Item window, select:
    • "COM Class" template.
    • Enter "MyComClass" for the Name.
    • Click the "Add" button when you are finished.


When we add our new COM class to our DLL, Visual Studio automatically performs a few things:

  • Checks "Register for COM interop" (Project menu | Properties | Compile tab).
  • Creates the "MyComClass.vb" code file and adds it to the project.
  • Inserts the required COM GUIDs in our new MyComClass code window.
  • Adds "Sub New()" with no parameters to our new MyComClass code window.

Here's what it looks like:


Adding code to MyComClass

Now that we have our COM class created, let's add a DisplayMessage() subroutine:

Public Sub DisplayMessage()
    MsgBox("Hello from MyComClass!")
End Sub

When you're finished adding the DisplayMessage() subroutine, Save and Build the project.

Using our new COM DLL in VB6

We now have a functioning COM DLL ready to be use 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...
  • Click the "Browse" button to open the "Add Reference" window.
  • Navigate to the Debug folder ("MyComDll\MyComDll\bin\Debug").
  • Select the MyComDll.tlb file (note 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 new COM DLL as a reference.

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

Private Sub Command1_Click()
    Dim mcc As New MyComClass
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:



I've included the COM DLL source code for you. If your new COM DLL is not working, take a look at my source code.

I hope it's helpful to you!


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

QuestionNice share. Thank you! Pin
Member 1054785919-Sep-17 12:47
memberMember 1054785919-Sep-17 12:47 
QuestionStandard Module? Pin
anemos_783-Apr-17 14:04
memberanemos_783-Apr-17 14:04 
QuestionThank you so much Pin
Willytedyanto30-Jun-14 16:48
memberWillytedyanto30-Jun-14 16:48 
QuestionError building COM DLL in visual studio 2010 Pin
Suvarna_878-May-13 23:16
memberSuvarna_878-May-13 23:16 
AnswerRe: Error building COM DLL in visual studio 2010 Pin
CS Rocks9-May-13 5:58
memberCS Rocks9-May-13 5:58 
GeneralRe: Error building COM DLL in visual studio 2010 Pin
Suvarna_8710-May-13 1:02
memberSuvarna_8710-May-13 1:02 
QuestionCom Class dll in VB.Net Pin
Ofir G.8-Aug-12 23:17
memberOfir G.8-Aug-12 23:17 
GeneralError when include the others reference Pin
ari.ribeiro3-Sep-09 4:56
memberari.ribeiro3-Sep-09 4:56 
QuestionOK, What am I doing wrong? [modified] Pin
KartRacer039-Feb-09 10:09
memberKartRacer039-Feb-09 10:09 
QuestionWhy i am not getting Com class in C# dll ? Pin
chaitanya Shenolikar12-Dec-08 0:37
memberchaitanya Shenolikar12-Dec-08 0:37 
GeneralEverytime i try to add the com/dll reference to my VB6 project - get compile error... Pin
kingslane22-Sep-08 8:19
memberkingslane22-Sep-08 8:19 
AnswerRe: Everytime i try to add the com/dll reference to my VB6 project - get compile error... Pin
kingslane23-Sep-08 5:31
memberkingslane23-Sep-08 5:31 
GeneralNevermind.. I'm an idiot Pin
mjnikkar22-Aug-08 3:25
membermjnikkar22-Aug-08 3:25 
QuestionBut How to Deploy? Pin
mjnikkar22-Aug-08 3:17
membermjnikkar22-Aug-08 3:17 
AnswerRe: But How to Deploy? Pin
RupeshV14-Sep-08 22:39
memberRupeshV14-Sep-08 22:39 
GeneralRe: But How to Deploy? Pin
Ly Long Hai13-Sep-15 19:23
memberLy Long Hai13-Sep-15 19:23 
GeneralGreat articles - COM and deploy are importent subjects Pin
sambuccus19-Aug-08 8:44
membersambuccus19-Aug-08 8:44 
General0 comments... Pin
Cristian Amarie18-Aug-08 9:22
memberCristian Amarie18-Aug-08 9:22 

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
Web06 | 2.8.190425.1 | Last Updated 14 Aug 2008
Article Copyright 2008 by CS Rocks
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid