Jim Lahman
Jim Lahman

Reputation: 2757

How do I convert a COM assembly to a CLR Assembly?

How Can I convert a COM server to a CLR Assembly so that I don't have to initially rewrite anything.

Upvotes: 3

Views: 185

Answers (1)

Toby Allen
Toby Allen

Reputation: 11213

I posted this here for the OP, as they posted it origionally as an edit to question.

Rather than rewriting a COM server (written in 1992 using C++/MFC) in .Net, I have decided to convert it to a CLR assembly. To take a COM assembly (add32.exe) and use it from a .Net client, we need to create a callable wrapper. Run all tools with the Visual Studio Command Prompt (as Administrator).

Step 1: Sign a COM assembly with a strong name

enter image description here

Step 2: Convert definitions found in a COM type library into a CLR assembly

Convert the definitions found in a COM type library into a CLR assembly using the tool Tlbimp.exe. The output of Tlbimp.exe is a binary file (an assembly) that contains runtime metadata for the types defined within the original type library. The output is a DLL file. I specify a namespace so that we can easily include the metadata in the .Net COM client.

enter image description here

Step 3: Use ILDASM.EXE to view the assembly.

enter image description here

To use the CLR assembly, we to create a reference for it in the solution. Browse for the dll file and add it as a reference.

enter image description here

Clients that use COM objects should import metadata using the namespace created in Step 2.

#using "Add32Pkg";

Then, to use the COM functionality:

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

namespace TestAdd32
{
    
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {

            Add32Server Add32 = new Add32Server();
            Add32.Init(201);
        }
    }
}

Upvotes: 1

Related Questions