views:

57

answers:

3

Is is possible to reference a C++ project in a C# Project? I've tried adding a reference in the c# project to that C++ one but I get an error saying "A reference to could not be added"

+4  A: 

You cannot reference native DLLs directly. You could only if you compiled it for the CLI (targetting .NET CLR) or had build a COM component (in which case VS builds an interop DLL automatically). Otherwise no way, you would have to write a wrapper DLL.

jdehaan
+1  A: 

If your C++ project is a native (standard C++) project, then no. If it's a managed project, you can add a reference to it.

For native code, you'll need to use P/Invoke to access functions within the C++ DLL.

Reed Copsey
A: 

Take a look at this webpage. It's a very good article on the mixing of C, C++, C# and Lisp code with short examples. Unfortunatelly the C++ -> C# is possible mostly when you use managed C++. Short example (from earlier mentioned webpage) which shows how to call the C++ managed Adder class from C#:

using System;
using System.Collections.Generic;
using System.Text;

namespace Dllcaller
{
    class Program
    {
        static void Main(string[] args)
        {
            Adder a = new Adder();
            Console.WriteLine(a.add(1, 7));
            while (true) ;
        }
    }
}
virious