COM Interop using Csharp

COM Interop using Csharp

COM Interop is a technology included in the .NET Framework Common Language Runtime (CLR) that enables Component Object Model (COM) objects to interact with .NET objects, and vice versa. COM Interop aims to provide access to the existing COM components without requiring that the original component be modified and vice-versa.

Create following project whose output is a dll in following manner :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Security.Cryptography;

namespace CreateHash
public interface IManagedClass
void GenerateHash(string inputString, out string outp);
// string GetHashString(string inputString);
public class ManagedClass : IManagedClass

public void GenerateHash(string inputString , out string outp)
// step 1, calculate MD5 hash from input

MD5 md5 = System.Security.Cryptography.MD5.Create();

byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(inputString);

byte[] hash = md5.ComputeHash(inputBytes);

// step 2, convert byte array to hex string

StringBuilder sb = new StringBuilder();

for (int i = 0; i < hash.Length; i++)




Generate dll with following project setting:((Project -> Properties ->Application))

COM Interop using Csharp

Also make api visible with following setting:(Project -> Properties ->Application->Assembly Information)

COM Interop using Csharp

Now open command prompt as an Administrator. Use following command:

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
regasm /u <DLL_PATH>

regasm /tlb <DLL_PATH> /codebase

This would generate a .tlb file. Make sure , regasm utility path is corrrect and in sync with dll type . If dll is 64 bit , then in path replace Framework with Framework64.

Import that file in COM application code using below code :

#import “Debug/GenerateHash.tlb” no_namespace named_guids

You can pass different arguments like “no_namespace ” and “named_guids” . Please go through the options available on MSDN site.

This would generate two files with extensions .tlh and .tli .Make sure both extensions files are generated before adding following code in your sample COM application:

IManagedClassPtr m_ptrManagedInterface;
HRESULT hr = CoCreateInstance(CLSID_ManagedClass,
IID_IManagedClass, reinterpret_cast<void**>(&m_ptrManagedInterface));

m_ptrManagedInterface = NULL;

CComBSTR text(“dummyString”);
CComBSTR ResponseCode;
CString csOut = CW2CT(ResponseCode);

So  , you get the return value in CString csout.

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.