My question is closely related to how a DLL exports C++ classes and generic methods (wrt C++ language features without a C# parallel).
I believe you can call functions inside an extern "C"
block from C# by just referencing the DLL and using DLLImport. But can you instantiate a templated C++ type? What if the C++ type does somethin' crazy that's not supported in C#? Is there an RFC or relevant part of the C# specification?
Thanks...
EDIT: I now stumbled across P/Invoke which should be valuable, but I'm still looking for a specification or standard regarding this.
Wrong, you have to use
DllImport
or so-called PInvoke (Platform Invoke) to call a native C function from managed code. Referencing works only for .NET assemblies or COM with an automatically generated interop dll.Using C++ from C# becomes a real nightmare because of name mangling among other things.
If you can you could compile a managed C++ Dll as a wrapper to make both worlds meet. This has also the advantage that you can mark your assembly as
ComVisible
, thus making it available to plenty of tools capable of handling COM.An alternative is to write a C wrapper around the C++ API, what can be tedious and ugly.
EDIT:
Help to decide which approach to use:
What the DLL does internally is irrelevant, as far as C# is concerned. Keep in mind that C++ templates do not generate any code until the template is instantiated. If the template is simply defined somewhere in the DLL header files, then there won't be any generated code for that template in the DLL. On the other hand, if the DLL explicitly instantiates and exports some templated types, then you could theoretically call them from C#.
There are two issues you'd have to overcome. The first is that C++ compilers mangle their method names, so you'd have to figure out exactly what the proper name to PInvoke is. The second problem is that there's no way to create CRT objects in C# directly. You'd have to define some factory methods and export those as well.
Frankly, I think this is all way more trouble than its worth. You'd be better off creating a C-style API for the DLL and calling those functions from C#. Internally, the functions could create and manipulate the relevant C++ objects.
The sane way to do it is to use managed c++ to access unmanaged c++ and compile into an assembly.