Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creating a COM DLL with VS 2005: Advanced Walkthrough

0.00/5 (No votes)
7 Jul 2009 1  
This article demonstrates how to manually create a COM DLL with VS 2005.

Introduction

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

 

Background 

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.
 
1_-_NewProjectMenu.jpg

 

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.

2_-_ChangeClassName.jpg 

  • 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.)
3_-_ChangeClassName.jpg 

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

5_-_DisplayMsgSub.jpg 

 

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:

6_-_Interface.jpg 

 

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.

7_-_GUID.jpg 

 

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:

8_-_AddGUID.jpg 

 

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.

9_-_Signing.jpg 

 

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.

10_-_RegisterForComInterop.jpg 

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 

11_-_GacInstall.jpg  

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

12_-_RegAsm.jpg 

  • 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.

13_-_Vb6Ref.jpg 

 

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:

14_-_Vb6Output.jpg

 

Cleanup

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

15_-_Unregister.jpg

 

Conclusion

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.

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here