How to create a COM object using VS 2008 and consume it from VB 6.0 client application: Part II

Posted by Bechir Bejaoui Articles | COM Interop November 04, 2008
In this article, we will see how to create a simple COM application in Visual Studio 2008 and how to consume it from a VB6 client application.
Reader Level:
Download Files:

In order to perform that, I invite you to follow this walkthrough.


Use case: Given that serialization process is not possible under Visual Basic 6.0, you will create a COM object that could be consumed locally by a Visual Basic 6.0 client application by exposing an initial .Net assembly to COM+. To follow this walkthrough, Visual Studio 2008 and Visual Basic 6.0 have to be installed within your machine and/or Microsoft Office 2003/2007.

A. Build the COM serviced component

  1. First, let's start by creating a new (*.dll) application under Visual Studio 2005/2008 by selecting File>New> Project>Class library then name it COM.

  2. Then create a new object called Person that looks like bellow and don't forget to add reference to System.IO and System.Xml.Serialization in order to use the StreamWriter and the XmlSerializer objects.

    public class Person
            public string FirstName { get;set;}
            public string LastName { get; set; }
            public bool IsMale { get; set; }
            public void Persist(string FilePath)
                StreamWriter oFile = new StreamWriter(FilePath);
                XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));
                oXmlSerializer.Serialize(oFile, this);

  3. Add a reference to the System.Runtime.InteropServices that contains some useful classes used for exposing the initial .Net assembly to COM.

  4. In order to be exposed to COM, an assembly has to be strongly named. It means that it must be signed before. To sign the given assembly, go to Project menu> <Project name> property .

    Figure 1

    As your project is named COM then you will find exactly COM Properties… at the bottom of the Project menu.

  5. Select the signing tab

    Figure 2

  6. Check the "Sign the assembly checkbox"

  7. A combo box just as in the figure 3 will invite you either to create a new key pair for the assembly and store them in a *.snk file or to use an already existing *.snk file.

  8. Choose the first alternative.

  9. Then set the file name in the above text box and the password in the other text boxes then click OK

    Figure 3

  10. As you create a key pair, the assembly could be strongly named.

  11. In the solution explorer, expand the properties node of the project and edit the AssemblyInfo.cs then add those three attributes, if you can't find ApplicationName, ApplicationActivation and AssemblyKeyFile elements then add a reference to System.EnterpriseServices

        //Those are additional attributes that have to be added to the project
        [assembly: ApplicationName("COM")]
        [assembly: ApplicationActivation(ActivationOption.Library)]
        [assembly: AssemblyKeyFile(@"C:\COM\COM\mySignature.snk")]

    The first attribute indicates the application name. The second one indicates the activation option, by the way, the activation option could have one among two values either library or server.

    Library indicates that the component runs in the creator process. In the other hand, server option indicates that component runs in the system process or in remoting context.

  12. Always within the AssemblyInfo.cs, do change the ComVisible attribute to true in order to be visible to the COM client application

        // Setting ComVisible to false makes the types in this assembly not visible
        // to COM components. If you need to access a type in this assembly from
        // COM, set the ComVisible attribute to true on that type.
        [assembly: ComVisible(true)]

    Remarque about the GUID:

    At the contrast of the .Net serviced component that is identified by the IP address and a given serial port. The COM serviced component is identified by the GUID that stands for globally unique identifier. It is a 128-bit integer (16 bytes) that could be used across all computers and networks as a COM assembly unique identifier.

    // The following GUID is for the ID of the typelib if this project is exposed to COM

    [assembly: Guid("6383a29e-fbf7-4c95-bbea-eb929e027f6a")]

  13. Now, turn back to the Person class because there are some modifications and tasks that one should do before exposing the assembly to COM.

  14. In fact when you expose the assembly to COM, the client application will interact with your serviced component via interface. Therefore you have to extract an interface from your class as follow.

    a) Go to Refactor menu then select extract interface, but first be sure to put the cursor inside the class.

    Figure 4

    b) Choose all members of the Person class then click OK

    Figure 5

  15. Decorate the class with [ClassInterface( ClassInterfaceType.None)]. Indeed, ClassInterfaceType enumeration could take one among three values.
    Value  Description
    None Using ClassInterfaceType.None is the only way to expose functionality through interfaces implemented explicitly by the class therefore it is recommended in our case.
    Auto dispatch Indicates that the class only supports late binding for COM clients.
    Auto Dual Indicates that a dual class interface is automatically generated for the class and exposed to COM. Type information is produced for the class interface and published in the type library. Using AutoDual is strongly discouraged because of the versioning limitations.


  16. In the other side, Person class inherits from System.EnterpriseServices.ServicedComponent as an obligation, because all the classes exposed to COM must inherits from this class , the resulted class must look like this

        [ClassInterface( ClassInterfaceType.None)]
        public class Person : System.EnterpriseServices.ServicedComponent, COM.IPerson
            public string FirstName{get;set;}
            public string LastName { get; set; }
            public bool IsMale { get; set; }
            public void Persist(string FilePath)
                StreamWriter oFile = new StreamWriter(FilePath);
                XmlSerializer oXmlSerializer = new XmlSerializer(typeof(Person));
                oXmlSerializer.Serialize(oFile, this);

    static public Person Retrieve(string FilePath)
            StreamReader oFile = new StreamReader(FilePath);
            XmlSerializer oXmlSerilizer = new XmlSerializer(typeof(Person));
            Person oPerson = oXmlSerilizer.Deserialize(oFile) as Person;
            return oPerson;


  17. Don't forget to change the accessibility of the interface to public

    using System;
    namespace COM
    public interface IPerson
            string FirstName { get; set; }
            bool IsMale { get; set; }
            string LastName { get; set; }
            void Persist(string FilePath);

  18. Finally, build the project, and verify if there aren't any error within the application

  19. Now it is the ultimate step, so open the SDK command prompt Start>All programs>Accessories>Command prompt then type this.

Remarque 1: For this tutorial, I use the Visual Studio 2008 therefore I taped Visual Studio 9.0, but if you use other version then indicate your version, like Visual Studio 8.0 for the 2005 version for example.

Remarque2: In this case, I used regsvcs.exe as a tool to expose the .Net assembly to COM but you still can use tlbexp.exe to export the assembly to the *.tlb form and regsvr32.exe to register the product within the registry entry. But me personally I prefer the first alternative because it is like the shampoo two in one!!

B. Consume the COM serviced component

Before exposing the method of how to consume this COM object let's take a look at the bin directory of your project

Figure 6

As you remark, there is the nostalgic *.tlb file in addition to the COM.dll. In the other hand, if you move to the Configuration panel>Administration tool>Serviced component and you expand the serviced component tree node as follow:

Figure 7

You will remark the new registered COM serviced component. The serviced component management console is somewhat like the global assembly cache one but it is provided for the COM objects and others.

Now, in order to consume the serviced component from within VB6.0, I invite you to follow those steps
  1. Create a new visual basic windows application as a client within Visual studio 6.0.

  2. Add a reference to your serviced component named COM and add a button to the project.

Figure 8

Implement the button event handler as follow:

Private Sub Command1_Click()
Dim proxy As Person
Set proxy = New Person
proxy.FirstName = "Bejaoui"
proxy.LastName = "Bechir"
proxy.Persist ("C:\myFile.xml")
End Sub

Execute the project, and you will see that a Visual studio 6.0 project can profit of this COM serviced component to serialize a Person object. This is wonderful, is it?

By the way, you can use this serviced component even from within OFFICE VBA editor by adding a reference to the first one as bellow:

Figure 9

Try to use the object explorer.

Remarque: If you ship your COM component in order to be used in other machines other than the development machine, the .Net framework should be installed and the if the COM object depends on other components you should ship them all together, else the COM will not work.

Figure 10

As you remark, the Person object is ready to use. In the next article, I will demonstrate how to consume this COM serviced component from within an unmanaged C++ client code.

Good Dotneting!!!

Employers - Post Free Jobs