chester89
chester89

Reputation: 8617

Can I use DLL written in C++ in my C# project?

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++

Upvotes: 3

Views: 1390

Answers (5)

Maciek
Maciek

Reputation: 19893

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

Upvotes: 0

leafnode
leafnode

Reputation: 1380

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);
}

Upvotes: 1

Kirill V. Lyadvinsky
Kirill V. Lyadvinsky

Reputation: 99525

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.

Upvotes: 5

Simon Steele
Simon Steele

Reputation: 11608

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.

Upvotes: 1

James
James

Reputation: 82096

I think this question may help you out:

Use C++ CLI template class in C#

Upvotes: 1

Related Questions