Creating a COM DLL with VS 2005: A Walkthrough
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
- Adds “
Sub New()” with no parameters to our new
Here’s what it looks like:
Adding code to MyComClass
Now that we have our COM class created, let’s add a
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
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:
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!