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?
 
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")

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
 
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
The new generation of project management tools

With monday.com’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.
0
 
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:

http://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-.net-com-assembly/
0
 
jkrConnect With a Mentor Commented:
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:
0
All Courses

From novice to tech pro — start learning today.