Click here to Skip to main content
15,885,898 members
Articles / Programming Languages / Visual Basic

Creating a COM DLL with VS 2005: A Walkthrough

Rate me:
Please Sign up or sign in to vote.
4.60/5 (17 votes)
14 Aug 2008CPOL4 min read 96.5K   2.6K   47   18
The article demonstrates how to create a COM DLL in VS2005.

Introduction

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"

Background

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.

NewProjectWindow.jpg

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.

DeleteComDll.jpg

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.

AddNewItem.jpg

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:

CodeWindow1.jpg

Adding code to MyComClass

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

VB
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:

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

VB6Test.jpg

Conclusion

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!

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

 
QuestionNice share. Thank you! Pin
Member 1054785919-Sep-17 12:47
Member 1054785919-Sep-17 12:47 
QuestionStandard Module? Pin
anemos_783-Apr-17 14:04
anemos_783-Apr-17 14:04 
QuestionThank you so much Pin
Willytedyanto30-Jun-14 16:48
Willytedyanto30-Jun-14 16:48 
QuestionError building COM DLL in visual studio 2010 Pin
Suvarna_878-May-13 23:16
Suvarna_878-May-13 23:16 
AnswerRe: Error building COM DLL in visual studio 2010 Pin
CS Rocks9-May-13 5:58
CS Rocks9-May-13 5:58 
Hi There,

I have to apologize, I am working in VS 2012 now myself, and haven't worked with COM for years, so I'm don't know the answer to your question. Frown | :(

My guess is that although you may be running Visual Studio as Administrator, perhaps you are not running the actual executable as administrator. Try going to the Debug or Release folder (whichever the exe is in), then shift right-clicking on it, and select Run As Administrator. Hope that helps.

Thank you for your feedback.

Gary
GeneralRe: Error building COM DLL in visual studio 2010 Pin
Suvarna_8710-May-13 1:02
Suvarna_8710-May-13 1:02 
QuestionCom Class dll in VB.Net Pin
Ofir G.8-Aug-12 23:17
Ofir G.8-Aug-12 23:17 
GeneralError when include the others reference Pin
ari.ribeiro3-Sep-09 4:56
ari.ribeiro3-Sep-09 4:56 
QuestionOK, What am I doing wrong? [modified] Pin
KartRacer039-Feb-09 10:09
KartRacer039-Feb-09 10:09 
QuestionWhy i am not getting Com class in C# dll ? Pin
chaitanya Shenolikar12-Dec-08 0:37
chaitanya 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
kingslane22-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
kingslane23-Sep-08 5:31 
GeneralNevermind.. I'm an idiot Pin
mjnikkar22-Aug-08 3:25
mjnikkar22-Aug-08 3:25 
QuestionBut How to Deploy? Pin
mjnikkar22-Aug-08 3:17
mjnikkar22-Aug-08 3:17 
AnswerRe: But How to Deploy? Pin
RupeshV14-Sep-08 22:39
RupeshV14-Sep-08 22:39 
GeneralRe: But How to Deploy? Pin
Ly Long Hai13-Sep-15 19:23
Ly Long Hai13-Sep-15 19:23 
GeneralGreat articles - COM and deploy are importent subjects Pin
sambuccus19-Aug-08 8:44
sambuccus19-Aug-08 8:44 
General0 comments... Pin
Cristian Amarie18-Aug-08 9:22
Cristian 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.