How do I use a C# dll in C++ COM application?

Hi, I have a C# dll that has different pulic static functions that i would like to use them in my C++ Com (unmanaged with only Win32 library) appliation.  Can someone outline the steps I need to do to use these C# function?

Thank you.
Who is Participating?
primeMover2004Connect With a Mentor Commented:
There's an easier alternative to COM interop. You can access your C# class via hosting the CLR.

Here's a VB sample (Fig. 13) taken from Don Box' House Of COM ("Migrating Native Code to the .NET CLR")

This approach is easier than all the deployment hassle you have with signing/strongnameing and registering your assembly using COM interop.

' needs to reference mscorlib.tlb and mscoree.tlb
Dim rt As mscoree.CorRuntimeHost
Dim unk As IUnknown
Dim ad As ComRuntimeLibrary.AppDomain
Dim s As ComRuntimeLibrary.Stack
Private Sub Form_Load()
  Set rt = New mscoree.CorRuntimeHost
  rt.GetDefaultDomain unk
  Set ad = unk
  Set s = ad.CreateInstance("mscorlib", _
  s.Push "Hello"
  s.Push "Goodbye"
  s.Push 42
  MsgBox s.Pop()
  MsgBox s.Pop()
  MsgBox s.Pop()
End Sub

Open in new window

You have to generate type library for that C# DLL using a command like below

tlbexp MyApp.dll /out:mytypelib.tlb

It will generate tlb file. You can use that tlb file in VC++. You can find tlbexp in .NET Framework direcotry i think.

Hope it helps

You can use COM interop for that purpose. See ("Exposing .NET Framework Components to COM")
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

lapuccaAuthor Commented:
My C# dll is a library project.  Most of the functions and properties in it are public and static.  I've been using this in my C# application without creating an instance of this library class.  Is this a qualified candidate for COM interop?  The url seems to be talking about creating an instance of the library class.  Thank you.
dstanley9Connect With a Mentor Commented:
The classes and methods need to be com-compatible, meaning the methosds should all use basic types, (ints, strings, etc.).  Technically you can use other types as well, such as arrays of basic types, and even custom classes, so long as those classes are com-visible.  For simplicity, though, stick to basic types as much as possible.  Also statci functions cannot be com-visible as COM has no concept of static methods.

You'll also need to:
* Add the ComVisible(true) attribute to your classes.  
* Use regasm to register the .NET dll on client machines

You can then add a reference to the COM Library to your C++ project and use it like any other COM library.

More details can be found here:
jkrConnect With a Mentor Commented:
That should work, yoet you might have to add some code. See the tutorial at ("COM Interop Part 2: C# Server Tutorial")
All Courses

From novice to tech pro — start learning today.