Exposing .NET Components to COM Clients

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay Katre, Prashant Halari, Narayana Rao Surapaneni, Manu Gupta, Meghana Deshpande

Table of Contents
Chapter 10.  Dealing with Legacy Components in .NET


We will first create a Microsoft .NET component using Visual Studio .NET. The application type will be a class library. When you want to expose a Microsoft .NET component to the COM world, there are a few things that you need to take care of in the code:

  • All managed types, methods , properties, fields, and events that you want to expose to COM must be public.

  • Types must have a public default constructor, which is the only constructor that can be invoked through COM.

  • The Microsoft .NET assembly that you create must be added into the global assembly cache.

  • Before an assembly can be added into the global assembly cache, it must be built with a strong key.

Figure 10-7 shows what exactly happens when you use a .NET component in COM.

Figure 10-7. Using a .NET component in COM.

graphics/10fig07.gif

Approach 1 (Using the Command Line)

Follow these steps:

  1. Build an assembly key file as follows using the sn.exe tool provided by Microsoft .NET Framework. Create the file somekey.snk in the C:\ directory. See Figure 10-8.

    Figure 10-8. Building an assembly key file.

    graphics/10fig08.gif

  2. Code the Microsoft .NET component in the usual manner. Note, however, that you need to put the AssemblyKeyFile attribute before the namespace as shown in the following code. This code can be found in the DotNETDll folder for this chapter. The component name is displaylibrary.dll .

     using System;  using System.Reflection;  [assembly: AssemblyKeyFile("C:\somekey.snk")]  namespace displaylibrary  {     /// <summary>     /// Summary description for Class1.     /// </summary>     public class Class1     {        public Class1()        {           //           // TODO: Add constructor logic here           //        }        public string ReturnName()        {           return "Name";        }     }  } 
  3. Use the regasm.exe utility provided by the Microsoft .NET Framework to register the assembly into the registry and also generate the type library with the command shown in Figure 10-9. This will generate a type library file named DisplayLibrary.tlb .

    Figure 10-9. Register an Assembly using the regasm.exe utility.

    graphics/10fig09.gif

  4. Add the Microsoft .NET assembly to the global assembly cache by using the gacutil.exe utility provided by the Microsoft .NET framework. Type the following command:

     Gacutil /i displaylibrary.dll 
  5. Now create a Visual Basic application and you can add and use the Microsoft .NET component the way you used to use the COM components.

Approach 2 (Using Visual Studio .NET)

Using Visual Studio .NET greatly simplifies the process of exposing Microsoft .NET components to the COM world. Create a Microsoft .NET component in Visual Studio .NET. Here the project library would be of type classlibrary . Now when you build the project, first right-click on the project from the solution explorer and select the property option. Figure 10-10 shows the property screen.

Figure 10-10. Property screen.

graphics/10fig10.gif

As shown in the figure, select the build option from the configuration properties. In the outputs option on the right side of the screen, check for the option Register for COM interop. If the value is made True, the Microsoft .NET component will be registered and it will be available as a COM component for normal Visual Basic applications.

Let us consider the Visual Basic application that will consume the preceding Microsoft .NET component. Figure 10-11 shows that the Microsoft .NET component is now available in the references for the project. It is selected to add to the project and can then be used as a normal COM component.

Figure 10-11. Adding a reference to the project.

graphics/10fig11.gif

The following code snippet shows how the component can be used in a Visual Basic application. This code can be found in the VBclient folder for this chapter.

 Dim objDisplay As Class1  Set objDisplay = New Class1  MsgBox objDisplay.returnName() 

Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net