简体   繁体   中英

C++ project reference in a C# project in visual studio 2008

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"

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM