将C#对象的引用传递给C ++ DLL,反之亦然 [英] Passing C# Objects' References to C++ DLL and vice versa
问题描述
我想知道如何将C#对象的引用传递给C ++并用C ++操作它。
For例如,我有一个C#课,
class StudentScore
{
受保护:
int 数学;
int 科学;
int 社交;
public :
int GetAverageScore()
{
return ((数学+科学+社会)/ 3 )
}
void SetMaths( int 得分)
{
数学=得分;
}
void SetScience( int 得分)
{
Science =得分;
}
void SetSocial( int 得分)
{
社交=得分;
}
}
StudentScore的对象将在C#中创建和值将按如下方式分配
refObjStudentScore = new StudentScore();
SetMaths( 85 );
SetScience( 80 );
SetSocial( 90 );
现在我希望将对象
refObjStudentScore
的引用传递给C ++ DLL并调用函数
GetAverageScore
以获得平均分数C ++。
能告诉我如何实现这个目标。
另外,作为下一个步骤,我希望将StudentScore的集合传递给C ++。
这方面的任何提示对我都有很大的帮助。
你不能以任何简单的方式做到这一点。通常,对于.NET程序集和本机DLL之间的接口,仅使用基本类型,结构以及一些选择类。这些类,例如System.String
,可能有一些预定义的封送到许多非托管类型。 interfaced方法本身应该是某个.NET类的静态方法,它相当于常规(不是面向对象的)C ++方法。
同时,如果它们是方法的参数,您可以使用一些托管类。对于某些人,定义了默认编组,对于其他人,您可以创建自定义编组。我称之为一个非常高级的话题。请从这里开始:
http:// msdn.microsoft.com/en-us/library/d3cxf9f0%28v=vs.90%29.aspx [ ^ ]。
- SA
您要做的是使用C ++ / CLI而不是普通的C ++。这将使您可以访问CLI中的所有托管类型和库。为了简单起见,C#和C ++之间通用的任何东西都应该写在C#端和C ++的#import'(这可以通过在C ++端定义来完成,但在这里要解释起来要复杂得多)。
最简单的路由:管理可执行文件(如果可能的话,C#),每个DLL都可以是C#或C ++ / CLI(或VB.NET,yecch!)。
I wish to know how to to pass the reference of an object of C# to C++ and Manipulate it in C++.
For example, I have a class in C#,
class StudentScore
{
protected:
int Maths;
int Science;
int Social;
public:
int GetAverageScore()
{
return ((Maths+Science+Social)/3)
}
void SetMaths(int score)
{
Maths = score;
}
void SetScience(int score)
{
Science= score;
}
void SetSocial(int score)
{
Social= score;
}
}
The object of StudentScore will be created in C# and values will be assigned as follows
refObjStudentScore = new StudentScore();
SetMaths(85);
SetScience(80);
SetSocial(90);
Now I wish to pass the reference of object
refObjStudentScore
to a C++ DLL and invoke the function
GetAverageScore
to get average score in C++.
Could you please let me know how to achieve this.
Also, as a next step, I wish to pass collection of StudentScore to C++.
Any hint on this front will be of great help to me.
You cannot do it in any simple way. Typically, for interfacing between .NET assemblies and native DLLs, only primitive types, structures, as well as some select classes are used. Such classes, such asSystem.String
, may have some predefined marshaling to a number of unmanaged types. The interfaced method itself should be a static method of some .NET class, which is equivalent to a "regular" (not object-oriented) C++ methods.
At the same time, you can use some managed classes, if they are the parameters of methods. For some, default marshaling is defined, for others, you can create custom marshaling. I would call it a pretty advanced topic. Please start here:
http://msdn.microsoft.com/en-us/library/d3cxf9f0%28v=vs.90%29.aspx[^].
—SA
What you want to do is use C++/CLI instead of plain C++. This will give you access to all of the managed types and libraries in the CLI. To make it easy, anything that is to be common between C# and C++ should be written on the C# side and #import'ed by C++ (this can be done by defining on the C++ side but is much more complicated to explain here).
Easiest route: make your executable managed (C# if possible) and each of the DLLs can be C# or C++/CLI (or VB.NET, yecch!).
这篇关于将C#对象的引用传递给C ++ DLL,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!