Click here to Skip to main content
Click here to Skip to main content

Creating a COM DLL with VS 2005: A Walkthrough

, 14 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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
    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)

Share

About the Author

CS Rocks
Software Developer DataPrint, LLC
United States United States

Comments and Discussions

 
QuestionThank you so much PinmemberWillytedyanto30-Jun-14 17:48 
QuestionError building COM DLL in visual studio 2010 PinmemberSuvarna_879-May-13 0:16 
AnswerRe: Error building COM DLL in visual studio 2010 PinmemberVB Rocks9-May-13 6:58 
GeneralRe: Error building COM DLL in visual studio 2010 PinmemberSuvarna_8710-May-13 2:02 
QuestionCom Class dll in VB.Net PinmemberOfir G.9-Aug-12 0:17 
GeneralError when include the others reference Pinmemberari.ribeiro3-Sep-09 5:56 
QuestionOK, What am I doing wrong? [modified] Pinmembertharrell9-Feb-09 11:09 
QuestionWhy i am not getting Com class in C# dll ? Pinmemberchaitanya Shenolikar12-Dec-08 1:37 
GeneralEverytime i try to add the com/dll reference to my VB6 project - get compile error... Pinmemberkingslane22-Sep-08 9:19 
AnswerRe: Everytime i try to add the com/dll reference to my VB6 project - get compile error... Pinmemberkingslane23-Sep-08 6:31 
GeneralNevermind.. I'm an idiot Pinmembermjnikkar22-Aug-08 4:25 
QuestionBut How to Deploy? Pinmembermjnikkar22-Aug-08 4:17 
AnswerRe: But How to Deploy? PinmemberRupeshV14-Sep-08 23:39 
General0 comments... PinmemberCristian Amarie18-Aug-08 10:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 14 Aug 2008
Article Copyright 2008 by CS Rocks
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid