C# code can't “see” the methods in my C++ dll

2019-06-02 10:57发布

问题:

I have a code written in C++ (that I did not write) and want to use it in C#, so I decided to make a dll and use this class from there.

I have very little knowledge of C++ and am having problems referencing the methods of this class in my C# project.

The C++ code is like this:

#ifndef BeamAn_class
#define BeamAn_class
#define DllExport   __declspec( dllexport ) 
#include <vector>
#include <cmath>

using namespace std;

public class  DllExport BeamAn
{
  public:
    BeamAn();
    ~BeamAn();  
    bool SetGeometry(vector<double>); //I didn't put the DllExport here because I already did it for the whole class. It's okay to do this, right?
    //other public methods an stuff
  private:
    //private methods an stuff
}
#endif

In my C# project I added the reference to the C++ dll normally (right click on the project, add reference. The .lib and .h files are in the same folder of the dll).

But looks like Visual Studio can't "see" the methods of my class. I can create a object of the BeamAn type, but can't use any of its methods. For example, I can do this: BeamAn contBeam = new BeamAn(); But can't use any of the methods or atributes inside the class, like this: contBeam.SetLoadFactors(1.0,1.2);

Visual Studio says that "BeamAn does not contain a definition for 'SetLoadFactors' and no extension method 'SetLoadFactors' accepting a first argument type 'BeamAn' could be found (are you missing a using directive or an assembly reference?)

Is there more something I should write in the C++ code to make the dll work properly, or am I doing something wrong when referencing it? I know that I'd have to use "DllImport" in my C# code if I was explicit linking, but that's not what I want to do.

Thank you very much!

回答1:

There are several ways to make C++ library available in C#.

  1. Use PInvoke - directly call your C++ code. See this article for more information. Also see pinvoke.net for general reference.
  2. Use an interop language to create a managed wrapper around your unmanaged code. If you're in the Microsoft world I would recommend C++/CLI to create mixed mode libraries (dlls that contain both managed and unmanaged code). Here is a brief (and old) article introducing C++/CLI.
  3. Make your C++ COM accessible. Since the code isn't yours this one might not be useful to you. See this post for more information about C++ and COM.

For simple scenarios PInvoke is probably the easiest. For more complicated interactions of the managed and unmanaged code I would recommend writing a mixed mode dll with an interop language. And if those don't work for you, use COM.

To address the error you receive: you can't add a reference to a C++ library from a C# project - they are completely different languages with different memory management systems, different type systems, different everything (well, a lot of things). Also, you can't pass data structures (like vectors, maps or sets) from unmanaged code to managed code.



回答2:

to use c++ code in a c# project, you have 2 options:

  1. wrap your c++ class in a managed c++ class
  2. use a library called swig

a link that might interest you: Wrapping Visual C++ in C#