August 27, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

COM Interoperability and .NET

  • February 27, 2003
  • By Mark Strawmyer
  • Send Email »
  • More Articles »

Strong Naming

It is important to understand strong naming because it plays a role in COM Interop. A strong name is one that is globally unique to the particular assembly. This helps to avoid common DLL conflicts, a.k.a. DLL hell, such as naming conflicts and versioning issues. They also provide a security check that the contents of the assembly have not changed since it was last compiled. If the .NET object calling a COM component is strong named, the COM component needs to be strong named as well; otherwise, the advantages of strong naming are lost. If it is a COM client calling a .NET object, the .NET object needs to be strong named so that the CLR can resolve the name to the appropriate assembly.

How to Make an Assembly Strong Named

  1. Generate a key file at a command prompt: sn.exe -k key.snk.
  2. Add an attribute to the AssemblyInfo.cs file that references the generated key file: <Assembly: AssemblyKeyFile("..\..\key.snk")>. The path in the attribute is relative to the project output directory. A further explanation can be found in the default AssemblyInfo.cs file itself.

Use an Unmanaged COM Object in a Managed .NET Application

If you are doing new development using .NET and you need to utilize existing COM investments, this is the section for you. In my opinion, this is the most likely case in which you will need to use COM Interop. The process is different if you are planning to use strong naming for your assemblies or not. Both scenarios are outlined below, followed by an example scenario with some supporting code.

Option 1: .NET Client Object is not Strong Named

  1. Create a runtime callable wrapper (RCW) for the COM component so that the CLR can interact with the object as a managed type. There are a number of ways to generate the RCW. A couple of the more common ways are listed below.
    • Use the Type Library Importer utility (tlbimp.exe). It is a manual command line driven utility that accepts different arguments. It converts a COM-specific type definition from a COM type library into equivalent definitions for .NET using a wrapper assembly. Example: tlbimp <TLB name>.tlb.
    • An easier way is to let Visual Studio .NET do the work for you by clicking the Project menu, Add Reference menu item, COM tab, and then double-click the desired COM component from the list of registered components. Click OK to close the dialog, and the wrapper is now generated.
  2. Reference the wrapper assembly DLL in the project. This will have been done for you automatically if you used Visual Studio .NET back in Step 1.

Option 2: .NET Client Object is Strong Named

Because the .NET client is strong named, we must make sure that all of the components utilized are also strong named; otherwise, we lose the benefits of strong naming.

  1. Generate a key file at the command line. Example: sn.exe -k <Key File name>.snk.
  2. Use the Type Library Importer (tlbimp.exe) to generate a strong named wrapper assembly. Example: tlbimp /keyfile:<Key File name>.snk <TLB name>.tlb.
  3. Reference the wrapper assembly DLL in the .NET project.

Sample COM Object

For the sake of this example, we have a COM object that contains a function that will pad the left side of a string with a specified string until it reaches a desired length. We'll pretend this is the greatest version of a pad function ever written and that we are compelled to reuse it in its current form. This function has been compiled in a Visual Basic 6.0 class called clsCommon that is part of an ActiveX dll called SampleUtil.dll when compiled. The code for the COM component is located below, and the client .NET code is located in the trailing section.

'****************************************************************' Description:  Left pad the given string with the given string'               until it reaches a string of the desired length.'' Parameters:   v_strInput  - string to pad'               v_strPad    - string to pad with'               v_intLength - desired string length'' Return Val:   String - string left padded with given char'****************************************************************Function leftPad(ByVal v_strInput As String, _                 ByVal v_strPad As String, _                 ByVal v_intLength As Integer) As StringOn Error GoTo ErrorCode         Dim intCount  As Integer            ' Loop controlDim intLenPad As Integer            ' Length of the pad stringDim strOutput As String             ' Output string        intCount  = Len(v_strInput)    intLenPad = Len(v_strPad)    strOutput = v_strInput        While (intCount < v_intLength)        strOutput = v_strPad & strOutput        intCount  = intCount + intLenPad    Wend        leftPad = strOutput    ErrorCode:    If (Err.Number <> 0) Then        leftPad = v_strInput    End IfEnd Function

Sample .NET Application

I did not use a strong name for this example, so the only thing required to reference and use the COM component was to simply go through the Visual Studio .NET menus and add a reference to the SampleUtil COM component. The sample .NET client is below.

/// <remarks>/// Sample client to use the SampleUtil COM component./// </remarks>public class SampleUtilClient{  public SampleUtilClient()  {    SampleUtil.clsCommon utility = new SampleUtil.clsCommon();    string test = utility.leftPad("testing", "0", 50);  }}




Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel