C#代码不能“看到”我的C ++ DLL中的方法 [英] C# code can't "see" the methods in my C++ dll

查看:131
本文介绍了C#代码不能“看到”我的C ++ DLL中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用C ++编写的代码(我没有写),并希望在C#中使用它,所以我决定做一个dll,并从那里使用这个类。


$ b $我很少知道C ++,并且在C#项目中引用了这个类的方法。



C ++代码是这样的: p>

  #ifndef BeamAn_class 
#define BeamAn_class
#define DllExport __declspec(dllexport)
#include<载体(vector)GT;
#include< cmath>

使用namespace std;

public class DllExport BeamAn
{
public:
BeamAn();
〜BeamAn();
bool SetGeometry(vector< double>); //我没有把DllExport放在这里,因为我已经为整个班级做了。可以这样做吗?
//其他公共方法一个东西
私人:
//私人方法一个东西
}
#endif
/ pre>

在我的C#项目中,我正常添加了对C ++ DLL的引用(右键单击项目,添加引用.lib和.h文件在dll的同一个文件夹)。



但是看起来像Visual Studio不能看到我的类的方法。我可以创建一个BeamAn类型的对象,但不能使用其任何方法。
例如,我可以这样做:
BeamAn contBeam = new BeamAn();
但是不能使用类中的任何方法或属性,如下所示:contBeam.SetLoadFactors(1.0,1.2);



Visual Studio说BeamAn不包含'SetLoadFactors'的定义,没有扩展方法'SetLoadFactors'接受第一个参数类型'BeamAn'可以找到你缺少一个使用指令或程序集引用?)



有更多的东西我应该用C ++代码编写,使dll正常工作,或者我做错了当我引用它时,我知道如果我是明确的链接,我必须在C#代码中使用DllImport,但这不是我想做的。



非常感谢!

解决方案

有几种方法可以使C ++库在C#中可用。


  1. 使用PInvoke - 直接调用您的C ++代码,请参阅这篇文章获取更多信息。另请参阅 pinvoke.net 一般frince。

  2. 使用interop语言在您的非托管代码周围创建托管包装。如果你在微软的世界,我建议C ++ / CLI来创建混合模式库(包含托管和非托管代码的dll)。 这里是介绍C ++ / CLI的简短(及旧版)文章。

  3. 使您的C ++ COM可访问。由于代码不是您的代码,所以这可能对您没有用。有关C ++和COM的更多信息,请参阅此帖

对于简单的场景,PInvoke可能是最简单的。对于管理代码和非托管代码的更复杂的交互,我建议使用interop语言编写混合模式dll。如果这些不适合您,请使用COM。



要解决您收到的错误:您不能从C#项目添加对C ++库的引用 - 它们是完全不同的语言,具有不同的内存管理系统,不同类型的系统,不同的一切(很多东西)。此外,您不能将数据结构(如向量,映射或集合)从非托管代码传递到托管代码。


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!

解决方案

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.

这篇关于C#代码不能“看到”我的C ++ DLL中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆