views:

29

answers:

1

In my C# code, I need to call a function from a C++ Dll that I wrote.The function is generic. So , should I just import it like this:

[DllImport("myDll.dll")]
private static extern TypeName functionName<TypeName>( int arg1, int arg2 );

Is this correct syntax? Thanks.

+2  A: 

This cannot work, there is no main-stream C++ compiler that makes templates exportable. Furthermore, templates are instantiated by the C++ compiler through type erasure, similar to the way Java generics works. In other words, the concrete callable functions have to be embedded in the DLL by the C++ compiler. They are no longer generic.

As an alternative, you can write a ref class in the C++/CLI language. That produces a true .NET generic class, usable by any .NET language that supports generics.

Hans Passant