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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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")
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.
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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:
That should work, yoet you might have to add some code. See the tutorial at ("COM Interop Part 2: C# Server Tutorial")
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.