How to create a COM application and consume it from within VC++6.0 - .Net and COM Part III

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

I invite you to follow this walkthrough.

Walkthrough:


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

A. Build the COM serviced component

As same as the previous article, you will follow some identical steps but not all ones because there is another trick of how to generate a COM without using the command prompt of the .Net SDK. This one is more clean and elegant.
  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);
            oFile.Flush();
            oFile.Close();
        }
    }

  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 bellow 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-eb929e027f1")]

  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

    namespace
    COM
    {
        [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);
                oFile.Flush();
                oFile.Close();
            }
    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 begins the difference with the previous article, in fact, instead of using the tlbexp.exe and the regsvr32.exe to export the assembly to COM and register it, you simply go to the solution explorer then you right click on the project.

  20. Select the properties menu item and the bellow widow apprears.

  21. Select the build tab and check the Register for COM interop.



    Figure 1

  22. Now, build the application.

  23. If you browse to the application directory, you will find a folder named Release, so open it and you will find something like this under view:



    Figure 2


  24. In addition to the (*.dll) file, you will find a (*.tlb) one that will be effectively consumed, I mean its services Of Corse, by your VC++6.0 client application and a (*.pdb) one. In fact, this last one is a secondary file, because during a compilation, the information about debugging will be stored in this one. So it will not play a role concerning our purpose.
B. Consume the COM serviced component

To consume a (*.tlb) services from within Visual C++ 6.0 is also easy, you will use for that a simple VC++6.0 windows application, you just follow those under steps.
  1. First, do create a simple Win32 application.



    Figure 3

  2. Then select the Win32 application and click OK.



    Figure 4

  3. Select a simple win32 application as bellow:



    Figure 5

  4. Afterward, click finish and the under window appears.



    Figure 6

  5. Click OK and then copy and paste the bellow code in the main method.



    Figure7

  6. Now run the application and observe



    Figure 8

  7. And finally, browse to C:\File.xml and observe



    Figure 9


That's it.
 

 

Up Next
    Ebook Download
    View all
    Learn
    View all