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.
lapuccaAsked:
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.

vs1784Commented:
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

Thanks
0
jkrCommented:
You can use COM interop for that purpose. See http://msdn2.microsoft.com/en-us/library/zsfww439(VS.80).aspx ("Exposing .NET Framework Components to COM")
0
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.
0
Cloud Class® Course: C++ 11 Fundamentals

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

dstanley9Commented:
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:

http://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-.net-com-assembly/
0
jkrCommented:
That should work, yoet you might have to add some code. See the tutorial at http://msdn2.microsoft.com/en-us/library/aa645738(VS.71).aspx ("COM Interop Part 2: C# Server Tutorial")
0
primeMover2004Commented:
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")

http://msdn.microsoft.com/msdnmag/issues/01/05/com/

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.Start
  rt.GetDefaultDomain unk
  Set ad = unk
  Set s = ad.CreateInstance("mscorlib", _
                            "System.Collections.Stack").Unwrap
  s.Push "Hello"
  s.Push "Goodbye"
  s.Push 42
  MsgBox s.Pop()
  MsgBox s.Pop()
  MsgBox s.Pop()
End Sub

Open in new window

0

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
primeMover2004Commented:
0
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
C++

From novice to tech pro — start learning today.