如何在c#中使用c ++类和函数? [英] How to use c++ classes and functions in c#?

查看:98
本文介绍了如何在c#中使用c ++类和函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1:

我想使用c ++到C#代码的类和函数,所以我导出了类并使用c ++成功创建了dll。如何将类从dll导入到c#。

这是我的c ++代码(导出类):

  //   MomentOfInertia.h  
#ifdef COMPILE_MYLIBRARY
#defineExport __declspec(dllexport)
#else
#define DllExport __declspec(dllimport)
#endif

class DllExport a
{
public
double MOIRectangle( double b, double h);
double MOISemiCircle( double r);
double MOITriangle( double b, double h, int );
};



  //   MomentOfInertia.cpp: 
#include stdafx.h
// 转动惯量的计算
#include MomentOfInertia.h
double a :: MOIRectangle( double b, double h)
{
return b * h * h * h / 3 ;
}
double a :: MOISemiCircle( double r)
{
const double PI = 3 14159 ;
return r * r * r * r * PI / 8 ;
}
double a :: MOITriangle( double b, double h, int
{
return b * h * h * h / 12 ;
}

解决方案

它会帮助你:

如何制作C ++类 [ ^ ]



和ya take a请看这里:

http:// stackoverflow .com / questions / 1170892 / net-call-c-functions-from-c-sharp [ ^ ]


试着查看这篇文章 - 它应该会有所帮助。

如何制作C ++类 [ ^ ]

从相关的源文件创建混合模式C ++ DLL。最好创建2个DLL。



通常情况下,根据您正在构建的DLL,您在源代码中会有很多差异。例如,您可以在一个案例中使用CString,在另一个案例中使用System :: String。在一种情况下,您希望一切都是原生的,而在另一种情况下,您可能更愿意拥有大部分(如果不是全部)代码(在这种情况下,单独使用/ clr选项在混合模式下编译可能更容易)。



如果你有很多课程,混合模式(C ++#CLI)可能是最简单的方法。



否则P / Invoke或COM(Active / X)可能就足够了。


Question 1:
I want to use the class and functions of c++ to C# code so i exported the class and created dll successfully using c++. How to import the class from dll to c#.
Here is my c++ code(Exporting the class):

//MomentOfInertia.h
#ifdef COMPILE_MYLIBRARY
 #define DllExport   __declspec( dllexport )
#else
 #define DllExport   __declspec( dllimport )
#endif

class DllExport a
{
public:
double MOIRectangle(double b, double h);
double MOISemiCircle(double r);
double MOITriangle(double b, double h, int);
};


// MomentOfInertia.cpp : 
#include "stdafx.h"
// Calculation of the moment of inertia
#include "MomentOfInertia.h"
double a::MOIRectangle(double b, double h)
{
return b * h * h * h / 3;
}
double a::MOISemiCircle(double r)
{
const double PI = 3.14159;
return r * r * r * r * PI / 8;
}
double a::MOITriangle(double b, double h, int)
{
return b * h * h * h / 12;
}

解决方案

It will help you :
How to Marshal a C++ Class[^]

and ya take a look here as well :
http://stackoverflow.com/questions/1170892/net-call-c-functions-from-c-sharp[^]


Try to check this article - it should help.
How to Marshal a C++ Class[^]


Create a mixed mode C++ DLL from pertinent source files. It might be preferable to create 2 DLLs.

Typically you would have a bunch of difference in source depending on which DLL you are building. For example, you might use CString in one case and System::String in the other. In one case, you want everything native and in the other case, you might prefer to have most if not all code managed (in that case, it might be easier to compile in mixed mode with /clr option alone).

Mixed mode (C++#CLI) is probably the easiest way to go if you have a lot of classes.

Otherwise P/Invoke or COM (Active/X) might be adequate.


这篇关于如何在c#中使用c ++类和函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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