tags:

views:

92

answers:

4

I have a DLL that I wrote in C# and I want to use it both with C# applications and applications written in unmanaged VC++. Is this possible?

+1  A: 

You can make the C# assembly visible to COM, and use it that way.

On your C# project properties, under the "Assembly Information" button, select "Make COM Visible".

There are numerous ways to access COM objects from Native C++, the easiest/best way depends on what your doing and how you're doing it.

Binary Worrier
+1 - that pretty much is it. COM interop is pretty much the only sensible way in this scenario.
TomTom
-1, with no comment . . . how can this be? If there's something wrong with my answer can you let me know please? We're all here to learn, thanks.
Binary Worrier
Thanks for your response, I will be using this approach. :)
Matthew Bowen
+4  A: 

Hi there.

To supplement other answers here, here's the MS support article which describes your scenario.

http://support.microsoft.com/kb/828736

Cheers. Jas.

Jason Evans
+1: Excellent google-fu! That link is exactly what Matthew needs.
Binary Worrier
@Binary Worrier - I was worried I'd be stepping on your toes with the above link, since it looked like you were about to edit your answer.
Jason Evans
@Jason: Kudos dude, fastest fingers win :)
Binary Worrier
Hi, thanks, this link looks very helpful.
Matthew Bowen
+2  A: 

There is more than just COM interop, the MSDN FAQ also lists lesser known methods:

2.2 How do I call .NET assembly from native Visual C++?

There are basically four methods to call .NET assembly from native VC++ code. Microsoft All-In-One Code Framework has working examples that demonstrate the methods.

  1. Native VC++ module calls CLR Hosting APIs to host CLR, load and call the .NET assembly. (All-In-One Code Framework Sample Code: CppHostCLR)

  2. If the .NET assembly can be exposed as a COM component, native VC++ module can call into the .NET assembly through .NET – COM interop. (All-In-One Code Framework Sample Code: CppCOMClient)

  3. Reverse PInvoke: the managed code call native passing a delegate the native code can call back. (All-In-One Code Framework Sample Code: CSPInvokeDll)

  4. If the module containing native VC++ code is allowed to enable CLR, the native VC++ code can call .NET assembly directly through the “It Just Works”, or “IJW”, mechanism. (All-In-One Code Framework Sample Code: CppCLIWrapLib)

0xA3
+3  A: 

Well, seems I have to bring up my unmanaged exports again. ;-)

Just answered a similar question 2 days ago. This totally works in C#, and it even creates a .lib & .exp file for your C# assembly to be consumed by C++:

internal class Sample
{
  [DllExport("_export_test", CallingConvention.Cdecl)]
  static int Test(int a)
  {
     return a + 1;
  }
}
Robert Giesecke
WOW! Really? I'm assuming the C# needs to be marked as unsafe? I had no frackin idea you could do this.
Binary Worrier
The assembly will contain unsafe bits, but will not be considered unsafe when used as .Net assembly, though. You can still use it as ordinary x86 or x64 (not AnyCPU) assembly with all your classes and stuff. However, if you load it as a real DLL, it will behave like one. This is where the unsafe bit kicks in.
Robert Giesecke