Visual Studio 2005 To COM and .NET 1.1

Making your Visual Studio 2005 code work with COM and .NET 1.1 This article explains: * how you can make code you develop in Visual Studio 2005 visible to COM applications * how you can make code you develop in Visual Studio 2005 run in a .NET 1.1 environment

Context

Some parts of SDL Tridion software require you to write code that a COM-based application (such as the Tridion Event System) needs to see. In addition, you may be developing code for a .NET 1.1 environment. 

If you work in Visual Studio 2005, you need to take some steps to meet both of these requirements. 


Making Visual Studio 2005 code visible to COM

To make your Visual Studio 2005 code visible to COM, perform the following steps: 

  • Make your project COM-visible 
  • Create an interface and class for COM visibility 
  • Register the resulting .NET DLL 
  • Test the DLL         

Making your project COM-visible 

To make your project visible to COM applications, do one of the following: 

  1. Open your project in Visual Studio 2005. 
  2. Select Project|<name> Properties... from the main menu. The project properties open in a new tab. 
  3. Select the Application tab on the left, if not already selected. 
  4. Click Assembly Information.... An Assembly Information dialog opens. 
  5. Select the option Make assembly COM-Visible.         

Project  Properties and Com Visibility

Alternatively, do the following: 

  • Open AssemblyInfo.cs, located in the Properties folder of your C# project, in Visual Studio 2005. 
  • Find the ComVisible attribute parameter and set it to 'true':         
// 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)] 

Creating an interface and class for COM visibility

Next, create an interface that defines the methods and properties of your class. This interface uses System.Runtime.InteropServices so that you can use the attributes COM requires, such as the 'Guid' attribute, which gives your interface a unique identifier. 

(For more information about interfaces, refer to Understanding Interfaces in .NET.) 

Here is an example of this type of interface, defining the DoSomething method: 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMVisible
{
    [Guid("C905F966-7355-442a-8892-916E90E7AE54")]
    interface IVisible 
    {    
     
String DoSomething();
    }
}


Next, create a class that implements this interface: 

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMVisible
{
     [ProgId("COMVisible.MyVisibleClass"),
     ClassInterface(ClassInterfaceType.AutoDual), 
     Guid("C905F966-7355-442a-8892-916E90E7AE54")] 
  
     
public class MyVisibleClass : IVisible 
     {
        #region IVisible Members 
        public string DoSomething() 
        {
            return "I did something!";
        }
        #endregion
     }
}

This class uses the following System.Runtime.InteropServices attributes: 

ProgId makes sure that our class has a string identifier, as is required for a COM class. 
Typically, this ID consists of the name of the container or project, followed by the class name. 
ClassInterface makes sure that your methods are visible to your COM application, for example to the Intellisense functionality of the Visual Studio 6 IDE. 

Set the parameter of this attribute to ClassInterfaceType.AutoDual. 
Note: that because of versioning issues with AutoDual, you need to reregister your DLL if the interface changes, then recompile your COM application. 

Guid identifies the class. 
Note: that this is a different identifier from your Interface's Guid. 

You can obtain a GUID as follows: 

  • Select Tools | Create GUID from the main menu to create a new GUID. The Create GUID dialog opens.        

  • Select the option Registry format and click Copy to copy the value to your clipboard. Then click Exit to close the dialog.  
  • Makign sure to remove the curly braces around the GUID, paste the GUID in the desired location.        

After creating the interface and class, build your assembly and note its location. 

Registering the .NET DLL

Next, use the Visual Studio regasm.exe tool to register the DLL. 

In the Visual Studio command prompt, Windows command prompt, or in a Windows batch file, enter the following command: 

C:\>regasm "C:\WINDOWS\System32\COMVisible.dll" /tlb:"C:\WINDOWS\System32\COMVisible.tlb" /codebase 

where COMVisible is the name of your assembly. The .tlb (type library) file contains the information about the interface (the methods and properties) of your object. 

Testing the DLL

To verify that the DLL was successfully registered and works as intended, create objects from it and call its methods from a COM application. 

For example, to test the DLL from a Windows application in Visual Basic, created in Visual Studio 6, do the following: 

  1. Create a VB Windows application in Visual Studio 6. 
  2. Select Project | References... from the main menu. The References dialog opens.       

 

    3. In the list of references, find the name of your assembly and select it.  

    4. Use the Object Browser to examine the classes and methods in this assembly. In addition to built- in .NET methods such as GetHashCode and ToString, you should see specific methods in your class. 

    5. Now, create a new instance of your class and call the method. 


Private Sub Form_Load()

  Dim myVisible As New MyVisibleClass 
  Dim result As String 
  
  
result = myVisible.DoSomething()  
  CallMsgBox(result)
End Sub 

This should result in the following: 

Targeting the .NET 1.1 Framework

Because Visual Studio 2005 targets only the .NET 2.0 framework, to target the .NET 1.1 Framework you must do the following: 

  • Only use classes and methods that are available in .NET 1.1. Refer to the .NET 1.1 Class Library
  • Build your 2.0 project against the 1.1 framework using a special build tool called MSBEE (download link). This build tool will alert you if your code contains 2.0-only code (and refuse to build).        

Installing MSBEE

To install MSBEE, do the following: 

  • Download and install the .NET 1.1 SDK
  • Download and install the MSBEE 1.0 build tool. 
  • Open your project file (.csproj or .vbproj) and add one of the following lines directly below the existing Import element, depending on the language you use:        

C# project

<Import Project="$(MSBuildExtensionsPath)\MSBee\MSBuildExtras.FX1_1.CSharp.targets"Condition=" '$(BuildingInsideVisualStudio)' == '' AND '$(TargetFX1_1)'=='true'"/> 

VB.NET project

<Import Project="$(MSBuildExtensionsPath)\MSBee\MSBuildExtras.FX1_1.VisualBasic.targets"Condition=" '$(BuildingInsideVisualStudio)' == '' AND '$(TargetFX1_1)'=='true'"/> 

Run the following command: 

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msbuild "C:\My Project\COMVisible.csproj" /t:Rebuild /p:TargetFX1_1=true 

where C:\My Project\COMVisibile.csproj points to the location and name of your project.
This will build the project and produce the desired DLL or EXE file. 


Note: For more information about using the MSBEE tool, refer to the Readme file included with the tool.

Further Reading

About the Author
Yoav Niran
Senior Technical Consultant

Yoav joined Tridion in 2006 and is working in the Professional Services Maidenhead office as a senior technical consultant. 

He has been involved in numerous implementations, integrations and development of packaged services.

Yoav has much experience in .NET technologies and ASP.NET web development in particular.

He has also been the technical lead on the reconstruction of the SDL Tridion World website.

For more technical articles from Yoav go to: http://yoavniran.wordpress.com

SDL CMT division