tags:

views:

79

answers:

3

I need to write a couple DLLs that will both be accessed from a C# application and a C++ application. Initially, I was thinking that I could save time/effort by writing the DLLs in C# and linking to them from the C# and C++ applications. Is this approach wise, or should the DLLs be written using C++?

+3  A: 

My advice would be to implement the code where you are most comfortable, but don't forget the interop requirement as you go. Know upfront what your interface is and what glue is required in the end product. Write the interface and test the interop before you fill out the implementation.

If the complexity of your requirement makes comfort level irrelevant, I would write it in C++ in such a way that you can access via P/Invoke.

One important factor is are you using C++/CLI or native C++. It's a lot easier to interop between C++/CLI and C#, in either direction.

Steve Townsend
Thanks, I'm going to get it all working in C# first. When I'll worry about linking the C# DLL into the C++ application.
Jim Fell
After spending 2 days trying to link to a C# DLL, I've decided to backtrack and write my DLLs in C++. Linking to unmanaged code seems to be a lot easier, and I'd like to finish this project with a at least a few hairs left in my head.
Jim Fell
@Jim Fell - sorry to hear that, I hope my advice has not caused you a lot of wasted time. btw I share your follicular sparseness and sympathize with desire to reduce stress level.
Steve Townsend
+1  A: 

I suggest you write your DLLs in C++, expose their public classes to COM using ATL or similar, then have tlbimp generate a thin C# wrapper around the COM interfaces.

Frédéric Hamidi
@Frédéric - so COM interop from C# to C++ no longer works?
Steve Townsend
@Steve, I don't know what I was thinking, thanks for the heads-up :)
Frédéric Hamidi
@Frédéric - np, I live in the native world so these things mean a lot to me..
Steve Townsend
A: 

If the DLL is to be used in other .Net applications, then you are pretty safe writing it in C# or C++, whichever you are more comfortable in.

If the DLL is to be used elsewhere, you will find it easier to write it in C++.

Dekker500