tags:

views:

413

answers:

5

The problem is - I want to write a template class in C++, put it in a DLL, and then call it from my C# project. Is it possible? Can you please provide any references or articles on about how to do it?

EDIT
I want DLL to be written in unmanaged C++

+1  A: 

I think this question may help you out:

http://stackoverflow.com/questions/681590/use-c-cli-template-class-in-c

James
+6  A: 

Template class could not be exported. It does not exist until someone instantiate it. You should explicitly instantiate it and then export it as usual class.

Kirill V. Lyadvinsky
+1  A: 

You create it just as with any other DLLs - the main idea behind DLLs is that it can be created in any programming language, and be used with every other. Just remember that C++ is unmanaged, so it has to be treated carefully. Look for instance here (MSDN forum).

One more link.

In general, use DllImport decorator to import functions from DLL file you've created in C++. Example from MSDN:

using System.Runtime.InteropServices; // DllImport
public class Win32 {
  [DllImport("User32.Dll")]
  public static extern void SetWindowText(int h, String s);
}
leafnode
+1 for the link, which tells it all, but you should add a bit of code using DllImport
John Pirie
Sure thing, fixed.
leafnode
+1  A: 

By using C++/CLI you can expose your C++ classes as .NET classes where they use compatible features. You won't, however, be able to expose your template definition, but may be able to use a concrete class that specializes that template.

When you build a C++/CLI class you can reference it just like any other .NET assembly.

Simon Steele
A: 

As far as I recall there's a bit of a problem. It is possible to Have C# use a C++ Dll (managed and unmanaged) It is possible to have unmanaged C++ use a C# Dll (you need to do this via COM and an interface).

I'll see if I can find more detailed information

Maciek